Selenium WebDriver - Defines a WebElement selector as a constant? A good idea?

I reworked my java project to define WebElement selectors as constants. This allows me to pass the By constant into my findElement method without requiring an evaluation of type By selector in the method. Is that a good idea? What problems can I encounter if I define By variables as public static finite constants?

The 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; } 
+7
source share
1 answer

This is a good practice.

You can use it with PageObject, see example:

https://code.google.com/p/selenium/wiki/PageObjects

+4
source

All Articles