如何在 webdriver 中使用具有 SearchContext 和 By 作为参数的方法
How do I use a method that has SearchContext , By as arguments in webdriver
我有一个用于 selenium webdriver 的页面对象框架。下面的包装器 class 具有以下方法。我试图理解代码。如果有人可以解释 waitElementIsVisible 方法以及如何使用它,那就太好了。
如何使用 waitElementVisible 方法或如何在我的页面对象设计中调用它。
public class WebDriverWrapper {
private static final Logger log = LoggerFactory.getLogger(WebDriverWrapper.class);
/**
* Default value of the wait timeout as obtained from configuration.
*/
public static final long IMPLICITLY_WAIT_TIMEOUT = EnvironmentProperties.IMPLICITLY_WAIT_TIMEOUT;
private static final long INTERVAL_SECONDS = 1;
private WebDriver driver;
public WebDriverWrapper(WebDriver driver) {
this.driver = driver;
}
public boolean waitElementIsVisible(SearchContext context, By by, long timeoutSeconds) {
boolean visible = false;
setTimeout(0);
FluentWait<SearchContext> wait = new FluentWait<SearchContext>(context).withTimeout(timeoutSeconds, TimeUnit.SECONDS).ignoring(NotFoundException.class);
try {
visible = wait.until(elementVisible(by));
} catch (TimeoutException e) {
visible = false;
} finally {
setTimeout();
}
return visible;
}
private static Function<SearchContext, Boolean> elementVisible(final By by) {
return new Function<SearchContext, Boolean>() {
public Boolean apply(@NotNull SearchContext context) {
return context != null ? context.findElement(by).isDisplayed() : false;
}
};
}
}
=================
How do i use the waitElementVisible method or how do i call in my page object design
public class CAPage extends WebDriverWrapper {
protected static final Logger log = LoggerFactory.getLogger(CheckoutLoginPage.class);
protected static final int SETUP_TIMEOUT = 600000;
protected CAPage caPage;
public String URL;
public CAPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
// TODO Auto-generated constructor stub
}
private static final String LOGIN="loginButton";
@FindBy(id = LOGIN )
private WebElement loginBUttonWebelement;
public boolean logInButtonElementPresent(){
return waitElementIsVisible(what should i put here?);
}
waitElementIsVisible(driver, By.id, 5)
SearchContext 是一个接口,用于定义您是要在整个页面中(使用驱动程序对象)还是在包含元素中(使用 webelement 对象)搜索您的元素。
在此处阅读更多内容 - https://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/SearchContext.html
我有一个用于 selenium webdriver 的页面对象框架。下面的包装器 class 具有以下方法。我试图理解代码。如果有人可以解释 waitElementIsVisible 方法以及如何使用它,那就太好了。 如何使用 waitElementVisible 方法或如何在我的页面对象设计中调用它。
public class WebDriverWrapper {
private static final Logger log = LoggerFactory.getLogger(WebDriverWrapper.class);
/**
* Default value of the wait timeout as obtained from configuration.
*/
public static final long IMPLICITLY_WAIT_TIMEOUT = EnvironmentProperties.IMPLICITLY_WAIT_TIMEOUT;
private static final long INTERVAL_SECONDS = 1;
private WebDriver driver;
public WebDriverWrapper(WebDriver driver) {
this.driver = driver;
}
public boolean waitElementIsVisible(SearchContext context, By by, long timeoutSeconds) {
boolean visible = false;
setTimeout(0);
FluentWait<SearchContext> wait = new FluentWait<SearchContext>(context).withTimeout(timeoutSeconds, TimeUnit.SECONDS).ignoring(NotFoundException.class);
try {
visible = wait.until(elementVisible(by));
} catch (TimeoutException e) {
visible = false;
} finally {
setTimeout();
}
return visible;
}
private static Function<SearchContext, Boolean> elementVisible(final By by) {
return new Function<SearchContext, Boolean>() {
public Boolean apply(@NotNull SearchContext context) {
return context != null ? context.findElement(by).isDisplayed() : false;
}
};
}
}
=================
How do i use the waitElementVisible method or how do i call in my page object design
public class CAPage extends WebDriverWrapper {
protected static final Logger log = LoggerFactory.getLogger(CheckoutLoginPage.class);
protected static final int SETUP_TIMEOUT = 600000;
protected CAPage caPage;
public String URL;
public CAPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
// TODO Auto-generated constructor stub
}
private static final String LOGIN="loginButton";
@FindBy(id = LOGIN )
private WebElement loginBUttonWebelement;
public boolean logInButtonElementPresent(){
return waitElementIsVisible(what should i put here?);
}
waitElementIsVisible(driver, By.id, 5)
SearchContext 是一个接口,用于定义您是要在整个页面中(使用驱动程序对象)还是在包含元素中(使用 webelement 对象)搜索您的元素。
在此处阅读更多内容 - https://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/SearchContext.html