java - 将 ExplicitWait 方法调用到功能特性中

标签 java selenium user-interface selenium-webdriver

我有一个 utils 类,它定义了 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 以供单击。需要等待一段时间才能显示注销链接。因此,我必须为其应用等待时间,但团队只决定应用定义的显式等待方法。

请问有什么想法吗?

最佳答案

您可以在代码中添加显式等待,如下所示:

Utils 类 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:使用显式等待而不是隐式等待总是好的,因为显式等待更灵活,因为如果元素已经准备好与其交互,则不必等待整个时间。

关于java - 将 ExplicitWait 方法调用到功能特性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238335/

相关文章:

python - 有没有办法通过单击例如上的图标来运行 python 脚本桌面?

java - 如何精确除9位数以上的小数?

java - NoSuchElementException : No line found (using hasNextLine()

java - 使用 ClassLoader.getResource() 加载 BufferedImage

Symfony2 类 UserInterface 不存在

javascript - 如何将平移和缩放控件添加到Processing.js Canvas ?

java - 将字符串编码为 UCS2

python - 如何使用selenium的多个实例[python]

javascript - 使用 Selenium 自动结帐过程时出现 403

java - Selenium generic 搜索框架