Friday, November 2, 2018

Selenium Firefox - use common functions like getButton(buttonText)

Selenium Experts recommend that those who use the tool and implement it to test their applications (like me) should create wrappers specific to their application for the general code snippets frequently used. This is a long way of saying that we need to create common functions used by many Test methods.

For example, I need a common function that clicks a button given the button text. I don't want to have to keep writing this long code snippet:

(new WebDriverWait(driver, waitSeconds)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(CssFormButtonBegin+buttonText+CssFormButtonEnd))).click();

Instead, I would like to use a common function like:

public void clickButton(String buttonText) {
  getButton(String buttonText);
}

public void getButton(String buttonText) {
// code that gets the WebElement of button type in CssSelector
// cssDoesNotWork = "button:contains('buttonText')"
// cssWorks = "form.className button#buttonId"
// or
// cssButtons = "form.className button"; // and loop through button list to find exact button by buttonText
}

Note that I use the CssSelector instead of the XPath to get the button element.

Useful Reference


No comments:

Post a Comment