Make Selenium Easy

Why syntax is driver.manage().window().maximize()?

Introduction

We frequently used the below kinds of method chaining statements in Selenium WebDriver scripts. This is a frequently asked interview question as well and you may be asked to explain the syntax. We are going to learn the same here.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Method Chaining

The above syntaxes are written using Method Chaining concepts of Java. Let’s learn the method chaining concept with a simple Java example.

We have three class as below:-

Class NewDemo

Class AnotherDemo

This class has a method that returns a type reference of NewDemo class ( First one).

Class Demo

This class has a method that returns a type of AnotherDemo. (Second class).

Explanation

1. There is a total of three classes “Demo“, “AnotherDemo” and “NewDemo“.
2. Class “Demo” has a method “DemoMethod” that returns an object or type of class “AnotherDemo“.
3. Class “AnotherDemo” has a method “AnotherDemoMethod” that returns an object of class “NewDemo“.
4. Class “NewDemo” has a method “NewDemoMethod” that returns void.

5. You can understand from the above steps that all three classes are connected or a point of connection.
6. Class “Demo” has a main method and we will write call methods here.
7. We created an object of “Demo” that enables us to call its method “DemoMethod()” that returns an object of class “AnotherDemo“. We can store the return type of a method in an appropriate type and here we can use a reference variable of type “AnotherDemo“.

8. As we get an object of class “AnotherDemo” that enables us to call its method “AnotherDemoMethod()” that returns an object of class “NewDemo“. We can store the return type of a method in an appropriate type and here we can use a reference variable of type “NewDemo“. Since we get an object of “NewDemo” class then call its method as well.

If you understand the pattern from the above example then you must have understood that there are multiple method calls in parts. To call the next method we are storing reference and then using it to call the next method.

We can directly create a chain instead of storing reference variables. This is called method chaining and anonymous object. When we need to use an object once in a program, we can go for an anonymous object for better memory utilization.

The way of achieving method chaining is explained below the image.

IMG_8376

Understand driver.manage().window().maximize()

You must have got an idea about this syntax now. This is also written using the Method chaining concept.

There is a method named “manage()” and declared In WebDriver interface. The return type of this method is Options. The Options interface is a subinterface of WebDriver interface. The Options interface has a method “window()” that returns an Interface type Window and that is also a subinterface of WebDriver interface. The Window interface has a method “maximize()” that returns void.

By following the above coding approach you need to write multiple lines of code as below –

And if we follow method chaining then it can be written in fewer lines as –

See complete code below:

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

44 thoughts on “Why syntax is driver.manage().window().maximize()?

  1. Very nice Amod. But I dare asking you one silly question to your standard.

    With your nice explanation I reach to maximize() method but how selenium understands that it has to maximize the window and by what dimensions? I mean to say where do we have a code which say to Selenium to maximize the window by this rate??? Because Interface Window has got maximize() method but it does not have an implementation(for the right reason as it is an interface).

    Why just below line is sufficient to maximize the window??

    driver.manage().window().maximize();

    I told you I am being silly but this is bothering me mate.

    Thanks in advance
    Deepak

    1. Hey, Sorry I could not reply earlier, and maybe it is super duper late but still, it may help others.

      RemoteWebDriver is a fully implemented class of WebDriver interface and it contains logic to do so.

      @Override
      public void maximize() {
      execute(DriverCommand.MAXIMIZE_CURRENT_WINDOW);
      }

  2. Hi Amod,

    It’s a great explanation. Thanks for explaining this topic. But I have one doubt here in which class the option & Windows interface are implemented how the instance creation is happen for option & Windows methods.

    Thanks in adavance!

    1. Implementations are in RemoteWebDriver class which is a fully implemented class of WebDriver interface. As we have multiple subinterfaces or inner interfaces in the WebDriver interface similarly RemoteWebDriver class has multiple inner classes. I will suggest you to refer the source code of RemoteWebDriver class.

  3. Hello,

    Thanks to explain this topic, i am very happy after reading your blog.
    You just make my life easier. You know what kind of confusion a learner have and you explain all them very well.

    Thanks once again.

  4. Very Confusing topic but Amod you have explained in a very easy way that to step by step,very understanding,
    Thank you

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Why syntax is driver.manage().window().maximize()?

by Amod Mahajan time to read: 3 min
44
%d bloggers like this: