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

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.

driver.manage().window().maximize(); // To maxmize
driver.navigate().to("some url"); // To open url
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);// To set implicit time

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.

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
{
        void NewDemoMethod()
        {
                System.out.println("NewDemoMethod");
        }
        
}

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

class AnotherDemo
{
        public NewDemo AnotherDemoMethod()
        {
                System.out.println("AnotherDemoMethod");
                return new NewDemo();
        }       
}

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

public class Demo {

  // A method with return type AnotherDemo
  public AnotherDemo DemoMethod() {
    System.out.println("DemoMethod");
    return new AnotherDemo();
  }

  public static void main(String[] args) {
    // Creating an object of Demo class
    Demo DemoObject = new Demo();
    // Calling DemoMethod() that returns a type of AnotherDemo and storing its return value 
    AnotherDemo AnotherDemoObject = DemoObject.DemoMethod();
    // Since we have a reference of AnotherDemo then we can call method of AnotherDemo
    NewDemo NewDemoObject = AnotherDemoObject.AnotherDemoMethod();
    NewDemoObject.NewDemoMethod();

    // method chaining
    Demo DemoObject1 = new Demo();
    DemoObject1.DemoMethod().AnotherDemoMethod().NewDemoMethod(); // Method chaining

  }
}

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“.