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.
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.
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 NewDemo { void NewDemoMethod() { System.out.println("NewDemoMethod"); } }
Class AnotherDemo
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(); } }
Class Demo
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 } }
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“.
Demo DemoObject = new Demo(); AnotherDemo AnotherDemoObject = DemoObject.DemoMethod();
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.
Demo DemoObject = new Demo(); AnotherDemo AnotherDemoObject = DemoObject.DemoMethod(); NewDemo NewDemoObject = AnotherDemoObject.AnotherDemoMethod(); NewDemoObject.NewDemoMethod();
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.

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 –
WebDriver driver= new ChromeDriver(); Options optnObj= driver.manage(); Window winObj= optnObj.window(); winObj.maximize();
And if we follow method chaining then it can be written in fewer lines as –
See complete code below:
WebDriver driver= new ChromeDriver(); driver.manage().window().maximize();
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.
Thanks Amod. You made my life easier
thanks Amod for the wonderful explanation.
Thanks Sapna.
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
Even I have the same doubt, did you get to know how maximize works without implementation?
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);
}
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!
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.
very good explaination ever…
Thanks Rajendra.
Very excellent explanation.
Thanks Nayeem.
Thanks Amod.
Your esoteric knowledge on selenium is outstanding. Thanks for the explanation.
Thanks Shashank.
Very helpful! Thank you for explaining it with examples.
Thanks Sid.
This is wonder 🙂 keep up the good work..
Thanks Neha.
Thanks a lot.
Thanks Ritu.
Great, thnx a lot
Thanks Abhijeet.
excellent..!
Thanks Farnaaz.
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.
Thanks Ankit.
outstanding explanation
Thanks Handra.
Great Explanation !!!
Thanks Sharmi.
Thanks Amod.I faced an interview and I could not explain this.Although I has the guess of this chaining.
Thanks Rahul. Hope you can answer in interview.
So nicely Explained and Yes ,now i can say that i know something different from others .Thank you so much AMOD.
Thanks Gaurav.
Thanks for this wonderful explanation..
Thanks Gokulnath.
Thanks for this wonderful explanation. i have never given a thought to this..
Thanks Gaurav.
Very Confusing topic but Amod you have explained in a very easy way that to step by step,very understanding,
Thank you
Thanks Saroja.
A very appropriate explanation
Thanks Garvita.
Wonderful topic. Definitely, I learnt a new thing. Thanks
Thanks Ganesh.
Thanks Amod. Very good explanation.