如何在测试中添加延迟,以便页面完全加载?

huangapple go评论75阅读模式
英文:

How can I make a delay in the test so that the page is fully loaded?

问题

我使用 [io.qameta.atlas](https://github.com/qameta/atlas),在测试完第一个复选框后,如何添加延迟?以下是测试代码:

```java
    @Test
    public void produrtTest(){
        onSite().onMainPage().getButton().findElement(By.xpath("//[@id=\"-category-section\"]/div/ul/li[1]/label/span")).click();
        onSite().onMainPage().getButton().findElement(By.xpath("//[@id=\"-category-section\"]/div/ul/li[2]/label/span")).click();
    }

我使用了 @Retry 注解(timeout = 20, polling = 5000)来等待页面加载,但没有暂停效果。

如何使用 waitUntil() 方法也不太清楚。


<details>
<summary>英文:</summary>

I use [io.qameta.atlas](https://github.com/qameta/atlas), how do I make a delay after testing the first checkbox? Here&#39;s the test:

@Test

public void produrtTest(){
onSite().onMainPage().getButton().findElement(By.xpath("//[@id=&quot;-category-section&quot;]/div/ul/li[1]/label/span")).click();
onSite().onMainPage().getButton().findElement(By.xpath("//
[@id=&quot;-
category-section&quot;]/div/ul/li[2]/label/span")).click();

}


I used the `@Retry annotation(timeout = 20, polling = 5000)` to wait 20 seconds for the page to load, but there is no pause. 

How to use the waitUntil () method is also unclear.

</details>


# 答案1
**得分**: 1

在您正在使用的工具的[首页](https://github.com/qameta/atlas)上有这样一个部分:

> **等待页面加载。** 有一个特殊的 shouldUrl(Matcher&lt;String&gt;
> url),它等待页面当前的URL和
> document.readyState 标志的条件。

```java
@BaseUrl("http://www.base.url/search")
public interface SearchPage extends WebPage {

     @Description("账户按钮")
     @FindBy("//div[@class = 'account-button']")
     AtlasWebElement accountButton();
}

@BaseUrl("http://www.base.url/account")
public interface AccountPage extends WebPage {

     //账户页面的元素在这里
}

以及

等待单个元素的条件。 AtlasWebElement 有两种不同的扩展方法用于等待。

第一种 - waitUntil(Matcher matcher),它在可配置的超时和传递的匹配器中等待,如果条件未满足,则抛出 java.lang.RuntimeException。

第二种 - should(Matcher matcher),以相同的方式在传递的匹配器上等待,但抛出 java.lang.AssertionError。

这两种方法中的一种应该能满足您的需求,具体取决于您等待什么(您没有指定)。

英文:

At the very front page of the tool you are using there's this section:

> Waiting for page loading. There is a special shouldUrl(Matcher<String>
> url) that waits on the condition for page’s current url and
> document.readyState flag.

@BaseUrl(&quot;http://www.base.url/search&quot;)
public interface SearchPage extends WebPage {

     @Description(&quot;Account button&quot;)
     @FindBy(&quot;//div[@class = &#39;account-button&#39;]&quot;)
     AtlasWebElement accountButton();
}

@BaseUrl(&quot;http://www.base.url/account&quot;)
public interface AccountPage extends WebPage {

     //elements for account page here
}

As well as

> Waiting on a condition for a single element. AtlasWebElement have two
> different extension methods for waiting.
>
> First - waitUntil(Matcher matcher) which waits with a configurable
> timeout and polling on some condition in a passed matcher, throwing
> java.lang.RuntimeException if condition has not been satisfied.
>
> Second - should(Matcher matcher) which waits the same way on a passed
> matcher, but throwing java.lang.AssertionError instead.

One of those should satisfy your need, depending on what you're waiting for (you did not specify).

huangapple
  • 本文由 发表于 2020年10月21日 03:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64451926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定