Can't we use RemoteWebDriver = new ChromeDriver()? Selenium Interview Q&A - 5 [Short Answer Series]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is navinia welcome back to naveen automation labs so the next question in this series we are going to talk about that uh this question why can we not write a remote web driver driver equal to new chrome driver and this question is like a lot of people they always ask they always get confused that what is the relationship between these two entities and why can't we use remote web driver driver equal to new chrome driver in fact there are multiple combinations you can create if you understand java properly if you understand the object oriented programming properly then you can easily understand this question okay so i'll tell you how to answer for this question first of all we need to understand one thing that according to the web driver java documentation or the api documentation that they have implemented what exactly is on top of that we have the first search context interface is available okay and then after that we have one important interface that is called web driver interface so they both are interfaces and this is a parent and this is the child interface so that's why we are using extend keyword over here i'm talking about java point of view and there are a number of abstract methods are available without any method body these are the interface methods and then there is a class this class name is called remote web driver class remote web driver class is actually implementing web driver interface right so obviously whatever the different methods are available it has to be implemented over here by remote web driver because child class responsibility to implement those methods and then along with that remote webdriver is having its own separate method also this is fine and then we have in between number of classes are available let's see this is my chrome driver this is my firefox driver then this is my safari driver opeda driver internet explorer driver or edge driver is available like that and then let's see this is my chrome driver class and in between there is one more class that is called chromium driver so there is a relationship like that okay and this is my firefox driver this is directly extending remote web driver they all are the child of remote web driver let's see this is my safari driver this is my firefox driver and so on like that so the question here is that most of the time what exactly we do we always write webdriver our driver is equal to new chrome driver chrome driver is the grandchild of web driver if you see this relationship right so webdriver is the grand parent interface of new chrome driver so of course i can do a top casting over here so this is my you can say the child class object is referred by grand parent interface reference variable so this combination is absolutely allowed so with this dr i can access multiple methods which are actually implemented by the remote web driver i can access all these methods also so this is the first combination that i have covered second combination the actual question is that why can't we write the remote webdriver dr is equal to new chrome driver or new safari driver or something like this is this possible of course according to java which is absolutely fine this is your child class object that i have created this is my child class object and this is your parent class reference that i have created so this is a typical concept of the top casting the child class object can be referred by parent class reference variable this is absolutely allowed so this combination that i'm using it that with this so remote web driver and this combination i'm using it so of course i can use this without any problem same thing i can use between these two guys also same combination i can use new firefox driver a new surface of safari driver for other browser drivers but which one is more preferable and is this a right statement what happens when we write like this can we automate our scenarios can we use a driver properly with a remote web driver or not right so let's see i'll come over here and this is a selenium 4 library that i'm using it and i'm going to create i'm not writing web driver driver equal to new chrome driver i'm going to write remote web driver class okay so let's write a remote a web driver for example driver is equal to new chrome driver is it allowed see absolutely allowed here so first of all this statement is absolutely right and i'll show you the practical documentation or their source code also see this new chrome driver chrome driver is a class which is extending chrome a driver class that that's why i told you chrome driver class and the chrome driver is actually extending remote web driver class and remember remote web driver is not an abstract class guys it's a separate independent class which is actually implementing web driver interface so here you can see exactly same hierarchy that i told you and the web driver is extending the search context same thing you can see this diagram over here this flow you can check it over here like this right now come back to this guy uh over here and then i'm writing can i access all the methods of of what so let's see driver dot see i can access all the methods i can write driver.get also whatever the url that you want to write http and then i'm using driver.find element method and then i'm passing dot send keys and then after that i'm closing the browser so let's see with this combination the script is running or not my browser is getting launched or not of course it will be launched you can see that it's absolutely working fine it will enter again and then it will close the browser perfect right and then there is no exception or error over here on the console so this combination is absolutely fine and then if you see that okay with the driver what are different other methods that i can achieve see there are a number of methods find element i can achieve switch to also i can use that okay see this switch to dot alert or whatever frame everything i can use this with a driver i can achieve quit and close also you can see that so all the methods which are actually coming from see this class this chrome driver combination this chrome driver is actually extending chromium and extending remote web driver and then you press ctrl o here and then you will see all the methods available inside the remote web driver so all these remote webdriver method i can directly achieve directly access with within this particular with this combination with this particular driver at the time of interview you can also save or they might ask this question also that okay if you're using this driver can i use this driver for some other classes that we use for example let's see we use in actions class or this driver right generally we pass the web driver reference but here we are passing the reference of remote webdriver so actions class is saying if you go to the actions class constructor is saying you give me the webdriver reference variable so this is also allowed because you are passing the child class reference over here which is absolutely fine so without any problem you can use all the method from actions class also move to element or whatever the click send keys double click drag and drop whatever you want to use it you can use it here same thing you are using in web driver weight also for the explicitly weight let's see in web driver weight constructor it's saying you give me the web driver weight reference over here but i'm passing the driver which is a remote webdriver reference and the timeout let's see i'm writing the duration of seconds of let's see five seconds so which is also perfectly fine and let's see i'm writing weight dot until pi dot a name name is equal to q and then i'm directly passing dot send keys so let's see it is actually working or not it should not give you any error over here so let's run it and let's see so here the browser is getting launched and see naveed and auto both got entered so here with the web driver weight also you can access that so concept here is guys remote web driver driver equal to new chrome driver is absolutely fine you can use it without any problem you can achieve the cross browser testing also you can use let's see for example i want to use with safari so let's run it with safari now so here you can see that okay yeah safari is getting entered and navigate auto got entered over here perfect so this is absolutely fine but generally we don't prefer this we always prefer the top casting with the interface so what exactly we do if you write instead of remote web driver right this is what the most preferable way with this combination which is absolutely fine let's see i'm using once again the chrome driver here so with this driver also i can access all the methods so ultimately those methods are implemented where see this carefully if i'm writing driver equal to new chrome driver and this driver.get method is getting called so this driver.get method is available actually inside the webdriver and implement it over here inside the remote web driver so you can go to the remote web driver and here it's implemented so end of the day this method will be called same thing if you write it with this approach with the remote webdriver approach so let me just comment it out and then i'm writing a remote web driver approach and then again this method is actually getting called directly from the remote web driver right so this method will be called which is available inside the remote web driver only right guys so that's the only difference other than that nothing is there everything will remain same this is exactly what we we have to understand that okay this hierarchy if you understand this hierarchy absolutely you can create multiple combinations now can you achieve a web driver driver equal to new firefox driver yes that also you can do that can you achieve a remote web driver driver equal to new firefox driver that also you can do that can you achieve between these two guys yes of course that i can do that so if you understand the hierarchy between the web driver remote web driver and the respective browser of course you can do that so if you use this combination for example let's see this is my internet explorer driver or edge driver or whatever so in that case this combination can i use it of course when you create the object of this class and refer by this dr over here which is pointing to this object you can access all these methods available inside the remote web driver this is absolutely fine right guys now the third combination is also you can check for example i am writing chrome driver driver is equal to new chrome driver this combination also i can use it but this will applicable only and only for chrome it means i can access all those methods which are available inside the chrome driver plus i can achieve all the method which are available from the remote web driver but i cannot do that browser testing over here cross browser testing over here it means this driver cannot be point to the firefox driver now this is a major problem because this driver is actually created from the chrome driver the type of this driver is the chrome driver class not the remote web driver or the web driver so the first two combination this and this which is absolutely fine you can without any problem you can use it over here the most preferable way is this one because here the implementation is like that we should always use the top casting with the parent interface you can use the top casting with the remote web driver also with the parent class also which is also absolutely fine okay but whenever you're writing let's see for hashmap object that i want to create what exactly i do it map m is equal to new hashmap that we write it right like this same thing i'm list is the interface map is the interface l is equal to new error list that we write it right same thing so this is your top casting that you are using it with the interface so child class object that you are creating referred by parent interface reference variable this is what exactly you are doing it in the first approach in the second approach this remote web driver is actually the child of a driver that's why we don't have any problem to write the second combination also which is absolutely working fine that i've already shown you practically here you can use all the methods and everything all right please now one small difference is that this is my selenium 4 library okay i'm using selenium 3 library also selenium 3.141 the only difference is that if you write this remote web driver in remote web driver they have removed many methods see if i go to the selenium three remote webdriver class and then you press ctrl o and then you will see that find element there are find element by method is also available see by dot id link text partial link test everything is available like that so there are direct method also available you can use this but these method they have removed now in selenium 4 library see if i go to the remote web driver from the selenium 4 and then press ctrl o and then you search for find element now they have removed all those unnecessary methods like find element by id by name by xpath and all right so direct method find element is available then you have to pass the buy dot id by export like that but here earlier in selenium 3 you were having all these unnecessary methods over here that's the only difference they have implemented from with respect to remote web driver from selenium 3 to selenium 4. unnecessary these methods they have removed right guys so that's all for this particular question this is the answer for this absolutely fine you can use it let me know if you what you think about this approach do you really prefer remote drive driver or driver equal to new or you prefer driver equal to a new chrome driver with respect to web driver what is your observation if you have any other extra point to add feel free to add in the comment section i'll see in the next video till then take care and god bless you all
Info
Channel: Naveen AutomationLabs
Views: 2,296
Rating: 4.963964 out of 5
Keywords: Can't we use RemoteWebDriver = new ChromeDriver()?, selenium interview questions and answers, selenium interview questions and answers for experienced, selenium interview questions, why cant we use remotewebdriver, remotewebdriver selenium example, remotewebdriver, selenium interview, test automation interview questions, naveen automation labs, naveen automation labs selenium
Id: AT1RiiwuZho
Channel Id: undefined
Length: 12min 56sec (776 seconds)
Published: Tue Sep 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.