是否值得使用 Selenium com.thoughtworks.selenium.Wait 类来等待? (表现)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
准确地说,匿名类仅创建一次(在编译期间)并且仅加载一次。
每次调用该方法时都会实例化,但开销非常小。
如果你经常这样做,那么你可以轻松创建一个(非匿名)类:
然后你可以做更简单的事情
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:
Then you can do the simpler
我敢说差异很小,您应该使用使代码更具可读性的方法。我个人认为,使用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.
我不熟悉 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.