Java 如何使用在webdriver中作为参数的SearchContext方法
我有一个用于SeleniumWebDriver的页面对象框架。下面的这个包装类有以下方法。我正在努力理解代码。如果有人能解释waitElementIsVisible方法以及如何使用它,那就太好了。 如何使用waitElementVisible方法,或者如何在页面对象设计中调用它Java 如何使用在webdriver中作为参数的SearchContext方法,java,selenium,pageobjects,Java,Selenium,Pageobjects,我有一个用于SeleniumWebDriver的页面对象框架。下面的这个包装类有以下方法。我正在努力理解代码。如果有人能解释waitElementIsVisible方法以及如何使用它,那就太好了。 如何使用waitElementVisible方法,或者如何在页面对象设计中调用它 public class WebDriverWrapper { private static final Logger log = LoggerFactory.getLogger(WebDriverWrappe
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?);
}
公共类WebDriverRapper{
私有静态最终记录器log=LoggerFactory.getLogger(WebDriverRapper.class);
/**
*从配置中获得的等待超时的默认值。
*/
public static final long IMPLICITLY\u WAIT\u TIMEOUT=EnvironmentProperties.IMPLICITLY\u WAIT\u TIMEOUT;
专用静态最终长间隔_秒=1;
私有网络驱动程序;
公共WebDriverRapper(WebDriver驱动程序){
this.driver=driver;
}
公共布尔waitElementIsVisible(SearchContext上下文、By、long-timeoutSeconds){
布尔可见=假;
设置超时(0);
FluentWait wait=new FluentWait(context).withTimeout(timeoutSeconds,TimeUnit.SECONDS).忽略(NotFoundException.class);
试一试{
可见=等待直到(elementVisible(by));
}捕获(超时异常e){
可见=假;
}最后{
setTimeout();
}
返回可见;
}
私有静态函数元素可见(最终由){
返回新函数(){
公共布尔应用(@NotNull SearchContext){
返回上下文!=null?context.findElement(by).isDisplayed():false;
}
};
}
}
=================
如何使用waitElementVisible方法,或者如何在页面对象设计中调用
公共类CAPage扩展了WebDriverRapper{
受保护的静态最终记录器日志=LoggerFactory.getLogger(CheckoutLoginPage.class);
受保护的静态最终整数设置\u超时=600000;
保护容量;
公共字符串URL;
公共容量(网络驱动程序){
超级司机;
PageFactory.initElements(驱动程序,this);
//TODO自动生成的构造函数存根
}
私有静态最终字符串LOGIN=“loginButton”;
@FindBy(id=LOGIN)
私有WebElement登录按钮WebElement;
公共布尔logInButtonElementPresent(){
return waitElementIsVisible(我应该在这里放什么?);
}
SearchContext是一个接口,用于定义是在整个页面(使用驱动程序对象)中搜索元素,还是在包含元素(使用webelement对象)中搜索元素
请在此处阅读更多信息-谢谢您的回答。您提供的waitElementIsVisible示例(driver,By.id,5)据我所知,使用搜索上下文作为webdriver。是否可以在此处使用WebElement上下文。您可以使用该代码吗?@Indu而不是包含登录按钮的WebElement实例中的驱动程序实例传递…waitElementIsVisible(loginBUttonWebelement,By.id,5)是吗?这里loginBUttonWebelement和By.id实际上指向同一个webElement。因此,在loginBUttonWebelement的上下文中搜索意味着什么?请您解释一下。这是使用webElement搜索上下文的一个很好的示例。或者您可以提供一个更好的示例。如果您有这样的html
您好
。。。要使用代码获取p,需要将div webelement传递给该方法。waitElementIsVisible(driver, By.id, 5)