小编典典

等待元素-WebDriver-PageObject模式

selenium

只要我使用PageObject模式,我就想知道应该在哪里等待动态页面上的元素。假设我们有测试方法和pageObject类。我应该做类似的事情吗(在测试方法中):

  1. 点击按钮
  2. 等待元素显示
  3. 验证元素(例如包含方法isElementDisplayed())

也许还有其他好的做法来等待该要素?也许我们应该等待PageObject.class中的isElementDisplayed方法中的元素?


阅读 307

收藏
2020-06-26

共1个答案

小编典典

您应该等待页面对象类中的元素,而不是测试类中的元素,因为您的元素应该在页面对象类中定义,测试类应该不了解任何元素,选择器或类似元素。测试(恕我直言)应该只包含描述测试流程的方法调用链,与网站和基础DOM的所有交互都应在Page
Object类中进行。

因此,等待某个元素出现的过于冗长的方法可能类似于:

private final By yourElement = By.id("id");
@Override
public void isLoaded() throws Error {
    new FluentWait<WebDriver>(driver)
            .withTimeout(60, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class)
            .ignoring(StaleElementReferenceException.class)
            .until(new Function<WebDriver, Boolean>() {
                @NotNull
                @Override
                public Boolean apply(WebDriver webDriver) {
                    WebElement element = driver.findElement(yourElement);
                    return element != null && element.isDisplayed();
                }
            });
}

用简单的话来说,该函数将轮询DOM
60秒(每1秒)以查看元素是否存在于DOM中并且可见(意味着高度和witdh大于1px)。如果该元素存在(并显示),则该函数将返回找到的元素并停止轮询(尽管isLoaded()在这种情况下方法不会返回该元素)。

如果找不到元素NoSuchElementException,可以忽略findElement方法可能抛出的错误,并且StaleElementException,它指示对元素的引用现在已“陈旧”-该元素不再出现在页面的DOM上。这通常意味着,某些内容(最常见的是JS)已修改DOM,并且引用不再有效,因此WebDriver需要再次查找它。

当然,较短的代码也可以解决问题,例如:

    new WebDriverWait(driver, 60)
            .until(ExpectedConditions.visibilityOf(someWebElement));

文档实际上对此非常好。

编辑:评论的答案:

好,懂了。但是,如果在单击某个按钮等之后存在元素,该怎么办?

假设您有一个方案,其中有一个按钮,单击该按钮后,将出现一个文本框,您想与之交互。

public class PageObject extends LoadableComponent<PageObject>{

    public PageObject() throws Exception {
        driver = getWebDriver();
        PageFactory.initElements(driver, this);
        isLoaded();
    }
    private WebDriver driver = null;

    @FindBy(id = "yourButton")
    private WebElement button;

    @FindBy(id = "textBoxThatAppears")
    private WebElement txtBox;

    @Override
    public void isLoaded() throws Error {
        // Initial loading, called when creating the page object to make sure that the page is loaded to a state where it is ready to interact with us, in our case it means that button is present in DOM and visible.
        waitForVisibility(button);
    }

    private void waitForVisibility(WebElement element) throws Error{
           new WebDriverWait(driver, 60)
                .until(ExpectedConditions.visibilityOf(element));
    }

    public void clickButton(){
        button.click();

    }

    public void interactWithTextbox(String text){
        // Wait for txtBox to be visible, then send text
        waitForVisibility(txtBox);
        txtBox.sendKeys(text);

       // EDIT 27.04.14: 
       // Actually you should not do the assertion here or anywhere in 
       // the pageObject, because when reusing the method in some other test, you might
       // not want to assert, you might wonder that why wouldn't you assert some 
       // specific condition every time, but I would throw that question right back 
       // to you and ask: What is the point of checking the exact same thing over and 
       // over again. There are 2 things, firstly the assertion takes resources (and
       // that can become important when test suite grows, secondly your tests can 
       // simply start failing at the same point when one little condition is not as
       // it should be. Also, having the asserts in the test, makes the test more
       // readable and understandable for others.
         // end edit 27.04.14
        // Next line is no longer recommended by this answer.
         // assert that something happened that you expected.
    }

}

现在您的测试课:

public void TestClass {

     @Test
     public void testClickButtonAndInteractWithTextbox(){
         // Initiate the page object
         Pageobject po = new PageObject();
         po.clickButtonAndWaitForTextbox();
         po.interactWithTextbox("blabla");
         // edit 27.04.14
         assertSomethingGoodHappened();
     }
}
2020-06-26