WebDriver Usage

Location Strategies

Choosing a Location Strategy

    Selenium webdriver uses 8 different locators to find the elements on a web page. The following are the list of object identifier or locators supported by selenium.
  • Id - Selcects element with the specified @id attribute.
  • Name Select first element with the specified @name attribute.
  • Linktext - Select link (anchor tag) element which contains text matching the specified link text
  • Partial Linktext - Select link (anchor tag) element which contains text matching the specified partial link text
  • Tag Name - Locate Element using a Tag Name .
  • Class name - Locate Element using a class Name ..
  • Css Select the element using css selectors. 
  • Xpath -  Locate an element using an XPath expression.

Navigate Back And Forward On The Browser

Navigating the history of browser can be easily done using below two methods. The names are self explanatory.

//Go back to the last visited page

driver.navigate().back();

//go forward to the next page

driver.navigate().forward();

Refresh Page

This may be required often. Just a simple refresh of the page equivalent to a browser refresh.

driver.navigate().refresh();

Elements

Check If An Element Exists

To check an specific web element being present on the web page you may want to use the id "element-id" method.

driver.findElements(By.id("element-id")).size() != 0

Check If Element is Visible 

The above method works well to check if an element exists on page. However, sometimes an element may not be visible and you cannot perform any action on it. Thus, to check whether an element is visible or not use the below code.

WebElement element = driver.findElement(By.id("element-id"));
if(element instanceof Rendered WebElement){
	System.out.println("Element Visible");
}
else{
	System.out.println("Element Not Visible");
}



Handling Files

Upload Image to Page

An approach to upload an image to a page is to copy the location of the file to the clipboard, entering it into the search bar and pressing enter.

StringSelection stringSelection = new StringSelection(documentLocation);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);


WebDriver Wait

Different Types of Wait 

  • Implicit Wait - Instructs the web driver to wait for some time by poll the DOM. Once declared, implicit wait will be available for the entire life of web driver instance.
  • Explicit Wait - With the use of ExpectedConditions, explicit wait is custom and can be adjusted. It will be used in the case that the tester wants the execution to wait for some time until some condition is achieved. Explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.


Note: Implicit Wait and Explicit wait will maximize wait for element for the time duration specified, If they find the element before the next step will be executed. If you want your test to wait for exact time duration, a better approach would be to implement Thread.sleep.

Thread.sleep - In sleep code it will always wait the mentioned seconds, even in case the page is ready to interact after 1 sec.

try{
  Thread.sleep(1000); //1 second
}catch(InterruptedException e){
	e.printStackTrace();
}