Why syntax is “driver.manage().window().maximize();”?
Last updated on April 23rd, 2017 at 03:28 pm
Hello Folks,
In my last post, I explained about some useful methods of WebDriver. I got some comments to explain why do we write syntax as below:
1 2 3 4 |
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 |
Actually it’s JAVA concept. It is called as method chaining. We will learn about this now.
Some basic Concepts of JAVA:
1. There can be two types of members in a java class. Static and Non-static.
2. Static methods can be called using class name or by creating an object of that class but calling by class name is perfect practice.
3. Non-static members of class can be called only through object of class.
4. A method can return a type. It may be class or interface as well.
#JAVACODE to learn how to call static and non-static methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Demo { void NonStaticMethod() { System.out.println("Non static method"); } static void StaticMethod() { System.out.println("static method"); } public static void main(String[] args) { Demo.StaticMethod(); Demo object= new Demo(); object.NonStaticMethod(); object.StaticMethod(); // Not preferred } } |
Outout:
static method
Non static method
static method
#JavaCode to understand method chaining:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class AnotherDemo { public NewDemo AnotherDemoMethod() { System.out.println("AnotherDemoMethod"); return new NewDemo(); } } class NewDemo { void NewDemoMethod() { System.out.println("NewDemoMethod"); } } public class Demo { public AnotherDemo DemoMethod() { System.out.println("DemoMethod"); return new AnotherDemo(); } public static void main(String[] args) { Demo DemoObject= new Demo(); AnotherDemo AnotherDemoObject= DemoObject.DemoMethod(); NewDemo NewDemoObject= AnotherDemoObject.AnotherDemoMethod(); NewDemoObject.NewDemoMethod(); // method chaining Demo DemoObject1= new Demo(); DemoObject1.DemoMethod().AnotherDemoMethod().NewDemoMethod(); // Method chaining } } |
Explanation:
1. There are total three classes “Demo”, “AnotherDemo” and “NewDemo”.
2. Class “Demo” has a method “DemoMethod” which return an object of class “AnotherDemo”.
3. Class “AnotherDemo” has a method “AnotherDemoMethod” which return an object of class “NewDemo”.
4. Class “NewDemo” has a method “NewDemoMethod” which return void.
5. Only class “Demo” has main method and through that method we want to access “NewDemoMethod” of class “NewDemo”.
6. We created an object of “Demo” to access method present in class “Demo”.
Demo DemoObject= new Demo();
7. Now, we will call “DemoMethod” of class “Demo” which returns an object of class “AnotherDemo”, which we can store in a variable of type “AnotherDemo”.
1 |
AnotherDemo AnotherDemoObject= DemoObject.DemoMethod(); |
8. Now what we have?? An object of class “AnotherDemo” means we can access methods of class “AnotherDemo”.
9. We call method “AnotherDemoMethod1” of class “AnotherDemo” which returns an object of class “NewDemo”, which we can store in a variable of type “NewDemo”.
NewDemo NewDemoObject= AnotherDemoObject.AnotherDemoMethod();
10. Now, we can access method of “NewDemo”.
NewDemoObject.NewDemoMethod();
11. So, now we have following lines of code:
1 2 3 4 5 |
Demo DemoObject= new Demo(); AnotherDemo AnotherDemoObject= DemoObject.DemoMethod(); NewDemo NewDemoObject= AnotherDemoObject.AnotherDemoMethod(); NewDemoObject.NewDemoMethod(); |
12. Now, refer the below image:
13. Above code has concepts of method chaining and anonymous object. When we need to use object once in a program, we can go for anonymous object for better memory utilization.
I hope above java concept must be clear.
Same logic is applied for selenium. I will explain one statement:
driver.manage().window().maximize();
When we call manage() method, it returns a “Options” type. “Options” is an interface.
1 2 3 4 5 |
public Options manage() { return new RemoteWebDriver.RemoteWebDriverOptions(); } |
Interface “Options” has method “windows” which returns “Window” type.
1 2 3 4 5 6 |
public interface Options { WebDriver.Window window(); } |
Now, we can use any method of interface “Window”. it has following methods which can be called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public interface Window { void setSize(Dimension arg0); void setPosition(Point arg0); Dimension getSize(); Point getPosition(); void maximize(); void fullscreen(); } |
See complete code below:
1 2 3 4 5 6 7 8 9 |
WebDriver driver= new ChromeDriver(); Options optnObj= driver.manage(); Window winObj= optnObj.window(); winObj.maximize(); // We can write above lines of code in one line as driver.manage().window().maximize(); |
That’s it. I hope you must have learnt new thing. If still you have doubt,feel free to comment or do email.
I strongly believe when you know answer of both “how” and “Why”, you can write better scripts.
#HappyLearning
#Thanks
thanks Amod for the wonderful explanation.
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?
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!
very good explaination ever…
Very excellent explanation.
Thanks Amod.
Your esoteric knowledge on selenium is outstanding. Thanks for the explanation.
Thanks Shashank.
Very helpful! Thank you for explaining it with examples.
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 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.