我重构了我的 java 项目以将 WebElement 选择器定义为 By 常量。这允许我将 By 常量传递到我的 findElement 方法中,而不需要在方法中评估 By 选择器类型。这是一个好主意吗?如果将 By 变量定义为 public static final 常量,我可能会遇到什么问题?
示例如下:
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;
}
最佳答案
关于java - Selenium WebDriver - 将 WebElement 选择器定义为 By 常量是个好主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16945834/