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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Look how versatile this method is when the location strategies are decided outside of the method and passed in as arguments!
Leave a Reply