I have refactored my java project to define the WebElement selectors as By constants. This allows me to pass a By constant into my findElement method, without requiring an evaluation of the By selector type in the method. Is this a good idea? What issues am I likely to encounter if I define the By variables as public static final constants?
Following is an example:
public static final By LOGIN_BUTTON_SELECTOR = By
.cssSelector("input[name='logIn']");
/**
* click the Login button
*/
public void clickLoginButton() throws TimeoutException,
StaleElementReferenceException {
// click the Login button
clickElement(LoginPage.LOGIN_BUTTON_SELECTOR);
}
/**
*
* find an element
*
* click the element
*
*/
public void clickElement(By elementSelector) throws TimeoutException,
StaleElementReferenceException {
WebElement webElement = null;
// find the element by By selector type
webElement = getElement(elementSelector);
// click the element
webElement.click();
}
/**
*
* generic method to get a WebElement using a By selector
*
*/
public WebElement getElement(By elementSelector) throws TimeoutException {
WebElement webElement = null;
// find an element using a By selector
getDriverWait().until(
ExpectedConditions.presenceOfElementLocated(elementSelector));
webElement = getDriver().findElement(elementSelector);
return webElement;
}
It is a good practice.
You can use it with PageObject, see example:
https://code.google.com/p/selenium/wiki/PageObjects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With