DYNAMIC BINDING AND UPCASTING IN SELENIUM


Dynamic Binding / Upcasting in selenium


Dynamic Binding


    • Dynamic Binding means object type can be determined at run time only 
    • The dynamic binding can be achieve by method overriding. JVM decides which method should be called for a particular call at runtime i.e. the method remains unknown until runtime.
    • Suppose, there are two classes and all override the same method which belongs to their parent class and an another class having main method
    • In Below program calling on run method depends on the type of object reference variable, i.e as per the below example calling of run method will give output as "RemoteWebDriver is running..." even though the object is created for ChromeDriver.


       class RemoteWebDriver

      {  

      void run()

                {
                System.out.println("RemoteWebDriver is running...");
                }  
     }  
      
      class ChromeDriver extends RemoteWebDriver
     {  
      void run()
                 {
                  System.out.println("ChromeDriver is running...");
                 }  
                     public static void main(String args[])
                  {  
                   RemoteWebDriver a=new ChromeDriver();  
                   a.run();  
                   }  
     }  


      • With the above example you must be curious why we are extending the ChromeDriver with RemoteWebDriver and interviewer can also asked these type of questions since as per the selenium Architecture ChromeDriver class extends the RemoteWebDriver then we upcast the browser classes to WebDriver instead of Parent class known as RemoteWebDriver


      Interview Questions 



      Question 1 :-  Why we generally upcast the browser related child class to WebDriver, and not RemoteWebDriver class (RemoteWebDriver being the super most class in SELENIUM)?

      Answers :-  We can upcast browser object to RemoteWebDriver, WebDriver,
      TakesScreenshot , JavascriptExecutor etc, but its a standard practice is to upcast to WebDriver interface. This is as per the SELENIUM coding standard set by the SELENIUM community.

      Question 2 :- Difference Between the WebDriver and RemoteWebDriver ?

      Answers :- If we are using Below line of code to start the browsers in selenium, then we are accessing the Chrome on our local machine directly which we generally do.

      Webdriver driver = new ChromeDriver();


      However if we use RemoteWebDriver instead of WebDriver as shown below, then it means we want to access and control the desired browsers in Node PC over the Grid using the Capability(settings) and Hostname or IP of the Selenium HUB running.

      Thus  If we want to use RemoteWebDriver, it requires us to tell it where the Selenium Server (Grid) is located and which webbrowser we want to use. example below:-


      WebDriver driver = new RemoteWebDriver(new URL("http://localhost:

      4444/wd/hub"), DesiredCapabilities.firefox());



      In above example assumes that Selenium Server is running on localhost with the default port of 4444. The nice thing about this is you can run Selenium Server on any machine, change the URL to point to the new machine and run the tests. 

      And one more difference, most of you guys already know that WebDriver is and interface and RemoteWebDriver is an class in selenium

      Post a Comment