Thursday, September 22, 2022

WebDriver: Tracing the Interface WebDriver - Part 2

 

In the previous post of this WebDriver series, I shared a gist about what WebDriver does and how.  In this blog post as Part 2 of this series, I'm sharing a bit more details on WebDriver and RemoteWebDriver.  

From there, we will see how AppiumDriver is related to WebDriver -- which extends the interface SearchContext.

This blog post is written as part of 21Days21Tips from The Test Chat.  The tip shared in this post is to know more about WebDriver internals and how it associates with RemoteWebDriver and AppiumDriver.

This should help in understanding the Selenium APIs better and from where it comes.  This helps in having a better mental model of the Selenium WebDriver and how we want to structure the instructions in the tests and utilities we write


SearchContext and WebDriver



Picture: Representation of SearchContext and hierarchy of WebDriver


  • The SearchContext is the parent interface in the WebDriver hierarchy
    • The subinterfaces of SearchContext are
      • WebDriver
      • WebElement
  • This SearchContext defines two methods
    • findElement(By by)
      • Modifier and Type is: WebElement
      • It finds the first WebElement using the given method
    • findElements(By by)
      • Modifier and Type is: java.util.List<WebElement>
      • It finds all elements within the current context using the given mechanism
        • NoteI'm referring to Java APIs of Selenium in this blog post
        • More details of this can be found here.

Note: Selenium's Ruby client describes the Interface SearchContext as this.  


The WebDriver provides the below methods:
  • close()
  • findElement(By by)
  • findElements(By by)
  • get(java.lang.String url)
  • getCurrentUrl()
  • getPageSource()
  • getTitle()
  • getWindowHandle()
  • getWindowHandles()
  • manage()
  • navigate()
  • quit()
  • switchTo()

More details of these methods can be found here.


RemoteWebDriver and AppiumDriver


Further, we see the class RemoteWebDriver implements the interface WebDriver.  Today, the WebDriver and RemoteWebDriver communicate using standard W3C specifications.

That way, all the modern browser which adheres to W3C specification should not have (much) trouble when using WebDriver and RemoteWebDriver to mimic the user action on them.  We see the ChromiumDriver(), ChromeDriver(), FirefoxDriver(), Edgedriver, SafariDriver(), and OperaDriver() extending the RemoteWebDriver.

This hints us to know and learn:
  1. Why do we initiate the WebDriver for first
  2. And, then we instantiate the browser's driver
  3. Later how we use WebDriver's instantiation to drive action (mimic the user action) on the browser using the respective browser's driver
When we want to automate using Selenium Grid, we make use of RemoteWebDriver to drive the action between the client and server.

The class AppiumDriver extends the WebElement and RemoteWebDriver from the project Selenium.  And further, it has its own methods to interact with the mobile elements.  More details about the Java Client of AppiumDriver can be found here.

The subclasses of AppiumDrivers are:

  • AndroidDriver
  • iOSDriver
  • WindowsDriver


21 Days 21 Tips -- #day17

Here are my pointers to fellow test engineers

  1. Interface SearchContext is top in the hierarchy of the WebDriver interface
  2. Interface SearchContext defines
    • Should I want to search for the element in the whole page
      • using WebDriver object
    • Or, should I search within a containing element
      • using WebElement object
        • We can notice methods returning the type WebElement
  3. RemoteWebDriver implements the interface WebDriver
  4. The modern browsers drivers extends the class RemoteWebDriver
  5. AppiumDriver extends the class RemoteWebDriver and interface WebElement
For more understanding of the SearchContext and WebDriver, refer to below git repository of SeleniumHQ:

The below understanding should give a mental model of how the call happens in Selenium's library:
  • WebDriver and browser's driver instantiation
  • The order in which it is instantiated and used in programming to automate actions on the  browser

If noticed, the automation we do is more of programming and not of Selenium's library.  We extend and implement the Selenium library in our programming to mimic the action on the browsers and mobile apps.


No comments:

Post a Comment

Please, do write your comment on the read information. Thank you.