Selenium WebDriver methods and how to use them in testing frameworks

Let's recap the WebDriver

WebDriver is one of the three components in Selenium that drives a browser. WebDriver launches a browser session natively (RemoteWebDriver does the same remotely). A WebDriver requires the path to the browser driver executable file, in order to successfully launch the browser, which is managed by the Selenium Manager internally.

WebDriver is an interface that inherits from the SearchContext interface. It has below methods :

  • void get(String url) : It launches the browser and open the web page for the url provided as the String parameter.

  • String getCurrentUrl() : It returns the current url loaded in the browser as a String value.

  • String getPageSource() : It returns the current page source as a String value.

  • String getTitle() : It returns the title of the current page loaded in the browser.

  • String getWindowHandle() : It returns the current browser window handle Id. Each open browser window has a different window handle Id.

  • Set<String> getWindowHandles() : It returns window handle Ids to all browser windows.

    💡
    Why Set<String> , not List<String> : In Java, List allows duplicate values, where in Set, duplicates are not allowed. The window handle Ids are always unique and cannot be duplicate.
  • WebDriver.Options manage() : Options is an inner static interface in WebDriver. Using manage() we can set timeouts, add or delete cookies, maximize window etc. It returns the Option interface.

  • WebDriver.Navigate navigate() : Navigate is an inner static interface in WebDriver. It provides methods like to(), back(), forward(), refresh().

  • WebDriver.TargetLocator switchTo() : TargetLocator is an inner static interface in WebDriver. switchTo() helps in switching to a different window or a frame from current window or frame.

  • WebElement getElement(By by) : (inherited from SearchContext interface) It takes By locator as parameter and returns the WebElement. It may happen that same web element is available multiple times on the current web page. This method will return the first located element.

  • List<WebElements> getElements(By by) : (inherited from SearchContext interface) It takes By locator as parameter and returns the List of WebElements. It will return all web elements identified by the locator on the current web page.

  • void close() : This method will close the current browser window.

  • void quit() : This method will quit the browser and close all windows.

How do we use them in a testing framework:

  1. Without the findElement() and findElements(), we cannot write a testing framework. These are the basic methods which will help us findingelements.

  2. getTitle() method can be used to verify the title of the current page. We can write a TestNG assertion as below :

@Test
public void getPageTitleTest(){
    Assert.assertEquals("actual_title", "expected_title");
} // The actual_title will be returned from a page class
  1. getWindowHandles() : When there are multiple windows available in the application and we want to get the titles of all the windows :
public void getAllWindowTitles(){
    //get all window handle addresses :
    Set<String> allWindowHandles = driver.getWindowHandles();
    //iterating through the list and printing titles :
    for(String windowHandle : allWindowHandles){
        System.out.println("Title of " + windowHandle + " " + 
                     driver.switchTo().window(windowHandle).getTitle());
      } 
}
  1. findElements() method doesn't throw any exceptions when element(s) is not found. So it can be used in cases where we want to verify that a user/item has been removed/deleted successfully from table/list. (It might not work for all scenario)
public boolean deleteUser(){
    //your code to delete an user :
    List<WebElements> user = driver.findElements(By locator) // locator to user
    //to confirm user has been deleted :
    if(user.size() == 0){
       return true;
    } else{
       return false;
    }
}
//test method :
@Test
public void deleteUserTest(){
  Assert.assertTrue(deleteUser());
}
//Note : These two methods will be in two different classes - page class and test class