是否值得使用 Selenium com.thoughtworks.selenium.Wait 类来等待? (表现)

发布于 2024-10-21 01:01:55 字数 654 浏览 4 评论 0原文

Selenium 具有类 com.thoughtworks.selenium.Wait 为了方便起见。我以下面的方式使用它:

public void waitForElement(final String locator){
    Wait a = new Wait(){
        @Override public boolean until(){
             selenium.isElementPresent(locator);
        }
    }

    a.wait("Cannot found locator " + locator, TIMEOUT);
}

据我所知,每次调用函数时都会创建新的匿名类,然后创建该类的对象。或者只会创建一个匿名类?

反正。直接在 waitForElement 方法中使用 Thread.sleep() 然后每次创建新对象有多快?这个差别能有多大呢?

Selenium has class com.thoughtworks.selenium.Wait for convinience. I use it in next way:

public void waitForElement(final String locator){
    Wait a = new Wait(){
        @Override public boolean until(){
             selenium.isElementPresent(locator);
        }
    }

    a.wait("Cannot found locator " + locator, TIMEOUT);
}

As I understand each time when I call my function new anonymous class will be created and then created object of this class. Or only one anonymous class will be created?

Anyway. How faster is just using Thread.sleep() directly inside waitForElement method then creating new object each time? How big can be this difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2024-10-28 01:01:55

准确地说,匿名类仅创建一次(在编译期间)并且仅加载一次。

每次调用该方法时都会实例化,但开销非常小。

如果你经常这样做,那么你可以轻松创建一个(非匿名)类:

public class ElementPresenceWait extends Wait {
    private final String locator;

    public ElementPresenceWait(final String locator) {
        this.locator=locator;
    }

    @Override public boolean until(){
        selenium.isElementPresent(locator);
    }

    public void wait(long timeoutInMilliseconds) {
        wait("Cannot found locator " + locator, timeoutInMilliseconds);
    }
}

然后你可以做更简单的事情

new ElementPresenceWait("foo").wait(1000);

To be precise, the anonymous class is only created once (during compilaton) and only loaded once.

It will be instantiated each time you call the method, but that overhead is pretty small.

If you do this a lot, then you can easily create a (non-anonymous) class:

public class ElementPresenceWait extends Wait {
    private final String locator;

    public ElementPresenceWait(final String locator) {
        this.locator=locator;
    }

    @Override public boolean until(){
        selenium.isElementPresent(locator);
    }

    public void wait(long timeoutInMilliseconds) {
        wait("Cannot found locator " + locator, timeoutInMilliseconds);
    }
}

Then you can do the simpler

new ElementPresenceWait("foo").wait(1000);
万人眼中万个我 2024-10-28 01:01:55

我敢说差异很小,您应该使用使代码更具可读性的方法。我个人认为,使用Wait类更具可读性。

I dare say the difference is minimal and you should use what makes the code more readable. In my personal opinion, using the Wait class is more readable.

如梦初醒的夏天 2024-10-28 01:01:55

我不熟悉 com.thoughtworks.selenium.Wait 的内部实现,但是通常我更喜欢编写“智能等待”,它首先检查,然后休眠四分之一秒,然后再次检查,然后休眠,依此类推,直到已达到最大超时。

这允许在等待某些东西时比线程更快地执行。睡眠必须始终等待最大睡眠时间。

I am not familiar with the internal implementation of com.thoughtworks.selenium.Wait, however generally I prefer to write "smart waits" which checks first, then sleeps for a quarter of a second, then checks again, then sleeps and so on until a max timeout is reached.

This allows for faster execution when waiting for something than a thread.Sleep which must always wait the maximum sleep time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文