Java 将显式Wait方法调用到函数特征中
我有一个Java 将显式Wait方法调用到函数特征中,java,selenium,user-interface,selenium-webdriver,Java,Selenium,User Interface,Selenium Webdriver,我有一个utils类,它定义了ExplictWait,希望调用该成员并将其应用于扩展到上述utils类的功能要素类 Utils类的显式方法: public void explicitWait(){ WebDriverWait waitExplicit = new WebDriverWait(driver, 20); } public WebDriverWait explicitWait(){ // this method returns WebDriverWait
utils
类,它定义了ExplictWait,希望调用该成员并将其应用于扩展到上述utils
类的功能要素类
Utils类的显式方法:
public void explicitWait(){
WebDriverWait waitExplicit = new WebDriverWait(driver, 20);
}
public WebDriverWait explicitWait(){ // this method returns WebDriverWait instance
return new WebDriverWait(driver, 20);
}
功能要素类方法:
public void userLogout(){
SatusPageElements forLogOut = new SatusPageElements();
if(driver != null) {
driver.findElement(forLogOut.profileName).click();
driver.findElement(forLogOut.logoutLink).click();
} else{
System.out.println("No Driver");
public void userLogout(){ // and then you can use explicitWait() in this method
SatusPageElements forLogOut = new SatusPageElements();
if(driver != null) {
UtilsClass uc = new UtilsClass(); // create instance of class where explicitWait()
uc.explicitWait().until(ExpectedConditions.elementToBeClickable(forLogOut.profileName))
driver.findElement(forLogOut.profileName).click();
uc.explicitWait().until(ExpectedConditions.elementToBeClickable(forLogOut.logoutLink))
driver.findElement(forLogOut.logoutLink).click();
} else{
System.out.println("No Driver");
SatuPageElements是一个类,其中定义了状态页对象,并且在SatuPageElements中定义了profileName和logoutLink。在此上下文中,单击profileName链接时,将显示logoutLink以单击。要显示注销链接,需要等待一段时间。所以,我必须为它应用等待时间,但团队只决定应用定义的显式等待方法
有什么想法吗?您可以在代码中添加显式等待,如下所示:
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
Utils类的显式方法:
public void explicitWait(){
WebDriverWait waitExplicit = new WebDriverWait(driver, 20);
}
public WebDriverWait explicitWait(){ // this method returns WebDriverWait instance
return new WebDriverWait(driver, 20);
}
功能要素类方法:
public void userLogout(){
SatusPageElements forLogOut = new SatusPageElements();
if(driver != null) {
driver.findElement(forLogOut.profileName).click();
driver.findElement(forLogOut.logoutLink).click();
} else{
System.out.println("No Driver");
public void userLogout(){ // and then you can use explicitWait() in this method
SatusPageElements forLogOut = new SatusPageElements();
if(driver != null) {
UtilsClass uc = new UtilsClass(); // create instance of class where explicitWait()
uc.explicitWait().until(ExpectedConditions.elementToBeClickable(forLogOut.profileName))
driver.findElement(forLogOut.profileName).click();
uc.explicitWait().until(ExpectedConditions.elementToBeClickable(forLogOut.logoutLink))
driver.findElement(forLogOut.logoutLink).click();
} else{
System.out.println("No Driver");
WebDriverWait的示例构造如下所示:
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
WebDriverWait wait=new-WebDriverWait(驱动程序,timeoutingseconds);
wait.until(ExpectedConditions.elementtobelickable(By.id));
PS:使用显式等待而不是隐式等待总是好的,因为显式等待更灵活,因为如果元素已经准备好与它交互,则不必等待整个时间。我尝试了它,它抛出了以下错误:“无法解析方法'until'(org.openqa.selenium.support.ui.ExpectedConditions)'抱歉,忘了创建UtilsClass实例()。我不知道如何确切地调用您的类,其中explicitWait()。您必须创建此类的实例,然后在另一个类中使用其方法。请查看更新的答案。这是完全正确的,我没有调用utils类并因此引发错误。我已调用并修复了它。感谢您指出。它正在工作,我可以等到元素可单击为止。