等待元素-WebDriver-PageObject模式

等待元素-WebDriver-PageObject模式,第1张

等待元素-WebDriver-PageObject模式

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

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

private final By yourElement = By.id("id");@Overridepublic 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();     }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5641905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存