[英]Selenium WebDriver - Is defining WebElement selectors as a By constant a good idea?
I have refactored my java project to define the WebElement selectors as By constants. 我重构了我的java项目,将WebElement选择器定义为By常量。 This allows me to pass a By constant into my findElement method, without requiring an evaluation of the By selector type in the method.
这允许我将By常量传递给我的findElement方法,而不需要在方法中评估By选择器类型。 Is this a good idea?
这是一个好主意吗? What issues am I likely to encounter if I define the By variables as public static final constants?
如果我将By变量定义为公共静态最终常量,我可能会遇到哪些问题?
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: 您可以将它与PageObject一起使用,请参阅示例:
https://code.google.com/p/selenium/wiki/PageObjects https://code.google.com/p/selenium/wiki/PageObjects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.