我有一个定义有ExplictWait的utils类,并想调用该成员并将其应用于扩展到上述utils类的功能要素类。

实用程序类ExplicitWait方法:

public void explicitWait(){

        WebDriverWait waitExplicit = 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");


SatusPageElements是一个类,其中在SatusPageElements中定义了状态页面对象,并单击了profileName和logoutLink。在这种情况下,单击profileName链接时,将显示logoutLink以单击。登出链接需要等待一些时间。因此,我必须为其应用等待时间,但团队仅决定应用定义的Explicit Wait方法。

有什么想法吗?

最佳答案

您可以像这样在代码中添加显式等待:

实用程序类ExplicitWait方法:

public WebDriverWait explicitWait(){ // this method returns WebDriverWait instance

    return new WebDriverWait(driver, 20);

}


功能要素类方法:

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>));


PS:最好使用显式等待而不是隐式等待,因为显式等待更加灵活,因为如果元素已经准备好与之交互,则不必等待整个时间。

07-28 03:18
查看更多