Simple Tip: Pass By Locators Into Methods For Maximum Versatility

This may seem like an obvious tip to many, but I’ve seen a few applications for automated tester roles at my company that fail to do this.  When creating methods that interact with elements on a web page, remember to use Selenium WebDriver’s By locators.

These By locators are super versatile, and will allow you to have one method that deals with any element that can be located.

So what happens when we stop using By and try to pass other things to our methods instead?  We get in situations like this:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BadExample {
private WebDriver driver;
public BadExample(WebDriver driver) {
this.driver = driver;
}
public String clickElementAndGetText(String elementToClickId, String elementToReadId) {
// Click the element we want to click
driver.findElement(By.id(elementToClickId)).click();
// Wait until the element we want to get the text from is visible
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(elementToReadId)));
// Get the text from our read element and return it
return driver.findElement(By.id(elementToReadId)).getText();
}
}

view raw

BadExample.java

hosted with ❤ by GitHub

Notice how each of the elements we interact with inside the clickElementAndGetText method is limited to being located via their ID?  This limits where we can use this method quite extremely.  The only application is where both elements we want to use can be located via an ID.  What if one has to be located via its html tag instead?  Well we can’t use this method to do that.  We could make another method that changes the last findElement call to use By.tagName instead, but think about how many methods you would have to create to accomplish all the possible combinations of element location strategies!

Instead, just make your methods pass By locators around.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Example {
private WebDriver driver;
public Example(WebDriver driver) {
this.driver = driver;
}
public String clickElementAndGetText(By elementToClick, By elementToRead) {
// Click the element we want to click
driver.findElement(elementToClick).click();
// Wait until the element we want to get the text from is visible
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(elementToRead));
// Get the text from our read element and return it
return driver.findElement(elementToRead).getText();
}
}

view raw

Example.java

hosted with ❤ by GitHub

Look how versatile this method is when the location strategies are decided outside of the method and passed in as arguments!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: