[0:00]Hey everyone, this is Mukesh Otwani once again from learn hyphen automation.com. Today in this video, we are going to talk about very important question again, explain constructor overloading in Selenium.
[0:11]If you come across this question, then again they can ask in multiple formats.
[0:16]One format they can ask explain constructor overloading and how you have used in Selenium WebDriver.
[0:23]Explain constructor overloading in your project, explain constructor overloading in your framework.
[0:30]So before we do that, I already published one video how you can explain method overloading.
[0:36]In the same way, you have to explain constructor overloading, but examples will be different.
[0:41]So quick recap. Polymorphism is one of the oops concept, which says one thing can have multiple forms.
[0:49]Again, divided into two overloading and overriding.
[0:53]Overloading, method overloading and constructor overloading.
[0:57]Method overloading already covered, I will give you the link in the description. So if you're completely new, I would highly recommend you to watch the previous video so that you will get a fair idea.
[1:09]Then we have constructor overloading, which we are going to see now.
[1:13]Apart from that, we have overriding, which is again, when you have same method in base class and child class, you are overriding the base class method.
[1:25]So we are going to talk about constructor overloading.
[1:29]Before we discuss constructor overloading, overloading means one method or constructor can have multiple forms, with different signature.
[1:37]Signature consists of three things, number of arguments, type of argument, order of argument, yeah.
[1:44]So, the moment you write first line of code in Selenium, you will see constructor overloading, and that is the best way to explain in interviews, that uh, while writing your first line of script, the moment you write WebDriver driver, new Chrome driver,
[1:58]Chrome Driver constructor is overloaded.
[2:01]So let me show you.
[2:05]Just write constructor overloading example.
[2:14]So first of all, what is constructor? Constructor is a special method which have the same name as the class name, and it will invoked automatically the moment you create object.
[2:24]If you're completely new to constructor, constructor overloading, I will also link my previous videos where I've explained what is constructor, how do we use, when do we use and so on.
[2:38]The moment I write web driver driver equal to new Chrome driver, can you see a constructor?
[2:50]Chrome driver is a class. What we are doing in this case, when I just say Chrome driver, we are actually creating object of Chrome driver, and since we are not passing any argument, it's a zero argument constructor.
[3:07]But notice here we have so many constructor.
[3:10]Constructor with zero argument, constructor with one argument,
[3:16]Constructor with one argument, but notice the type here. Here it is taking Chrome driver service, here it is taking Chrome options.
[3:24]Now notice the fourth and fifth Chrome driver constructor, right?
[3:29]Number of arguments two and two. But sorry, two and three. So number of argument changed two and three, and the third argument is one additional, which is client config.
[3:40]So this is how you can explain. You can say when we are starting with Selenium, if we want to invoke our browser without any settings, without any profile, if we directly use zero argument constructor, it invokes a browser without any setting.
[3:55]But let's say if we have to pass any proxy, any arguments, then we can use their parameterized constructor where depends on what argument we pass, our browser will start with that particular option.
[4:08]I will show you one example, then you will have more clarity. So to show you this, if I just execute this piece of code,
[4:15]It is going to start my Chrome, right?
[4:20]And since we have not passed any URL, any configuration, it's a fresh Chrome session with default settings.
[4:31]But let's say if I want to pass some arguments. First of all, let me pass one application URL.
[4:37]Let's say I want to open my blog.
[4:43]And simply, I want to get the title. So driver.get title and then I will close it.
[4:52]Again, execute this.
[4:56]You can see the browser is opening.
[5:00]It will load the URL, which is my blog.
[5:05]And once the page will load, it will print the title and it will close.
[5:10]So once it is done, once the page loaded completely.
[5:14]Now it is capturing the title, which is automation, Selenium web driver tutorials one step by step and it's closing.
[5:20]Now let me show you one parameterized constructor, so that you will have more clarity.
[5:26]See guys, we have options class for every browser. For Chrome, we have Chrome options, for Firefox, Firefox options, for Edge, Edge options, for Safari, Safari options.
[5:36]Basically, this options class will help you to customize the browser arguments, profiles, any settings that you want to change.
[5:43]Let's say for this example, I want to run my test in headless mode.
[5:48]Headless mode means I want to run in non GUI mode, which means you will not see the browser, but my test will be running and you will get the results.
[5:59]So for that, again, let's say if I create object of Chrome options, and to run in headless mode, we use one argument called hyphen hyphen headless.
[6:13]And with new version, you can also type hyphen hyphen headless equal to new.
[6:19]Now I can pass this options reference in the constructor.
[6:25]So now I'm using parameterized constructor of Chrome driver, which takes one argument, which is Chrome options. So constructor overloading.
[6:36]Now you will notice it, you will not see any UI. It will run. I will get the title and it will close.
[6:43]Execute this once again.
[6:47]You can see it started, but I'm not able to see anything on the UI. My test will run and I will get the results.
[6:56]So let it run. In the meanwhile, same thing you can explain for Firefox.
[7:02]Can you see? Firefox also have multiple constructor.
[7:05]Zero argument, one argument, one argument, two arguments, three arguments. Constructor overloading.
[7:12]Try the same thing for Edge browser.
[7:19]You can see we have six, no, again five.
[7:27]Zero argument, one argument, one argument, two argument, three arguments.
[7:32]So stick to this example.
[7:35]Okay, and you can see we got the result.
[7:38]Since it was taking some time, in the meanwhile I explained uh other browser constructor.
[7:45]If you want to see, Chrome driver is a separate class, and this is their constructor, zero argument constructor.
[7:52]Constructor with one argument, with Chrome driver service,
[7:56]Constructor one argument, but with Chrome options, which we use just now.
[8:02]In similar way, other constructor.
[8:07]If you want to explain more examples, then let me show you one more.
[8:13]See, the moment you start working with web driver wait,
[8:18]Okay. So let's say if I go ahead and create object of WebDriver wait.
[8:26]Can you see three different constructor?
[8:30]First constructor says, you give me web driver reference and give me duration.
[8:35]Second constructor says, you give me driver, give me duration, and duration, which is actually the sleep.
[8:41]Again, one more constructor, which says driver, timeout, sleep, clock, and sleeper. So same constructor,
[8:52]Basically three different constructor with the same name, but signature is different. Signature, number of arguments, type of argument and order of argument.
[9:00]Again, if you're completely new to what is explicit wait, fluent wait, then I would recommend my previous videos once again, and all these things that we are discussing, relevant videos I will be giving in the description of this video.
[9:15]So to summarize how you can explain constructor overloading in Selenium. You can say constructor overloading we have used in our current project.
[9:24]Every time we have to make some changes or customize some browser settings, we use Chrome options, and these Chrome options we pass in the constructor of respective browsers.
[9:35]So if I'm using Chrome, I will use Chrome options. If I use Firefox, I will use Firefox options. If I use Edge, I will use Edge driver, basically Edge options.
[9:44]So this is how you can explain. Second example, you can tell we have used constructor overloading again with explicit wait.
[9:50]Every time when we create object, WebDriver wait have multiple overloaded constructor. So depends on what argument we are passing, we are getting the wait reference, which is type of WebDriver wait, and then we are using.
[10:07]But in order to explain them, first you should understand what is constructor, what is constructor overloading, then only you can explain constructor overloading in Selenium web driver.
[10:17]I hope you got some clarity. If you still have any questions, let me know in the comment section. If you are looking for more videos or any question that you're not able to answer, let me know. And by any chance, if this video help you, then do share this video with your colleagues. Subscribe to this channel and I will see you in the next video. Till then, bye-bye. Take care.



