简体   繁体   English

Selenium:显式/无条件等待,即“等待”

[英]Selenium: Explicit / Unconditional wait, i.e. "just wait"

As it turns out, the WebDriverWait class does not allow for an "unconditional wait": you always have to specify a certain condition to wait for.事实证明, WebDriverWait class 不允许“无条件等待”:您始终必须指定要等待的特定条件。 However, for example when setting up a project and developing first tests, waiting unconditionally is a helpful feature.但是,例如在设置项目和开发第一个测试时,无条件等待是一个有用的功能。 The usage of Thread.sleep() is often discouraged.通常不鼓励使用Thread.sleep() Are there better solutions for the "just wait"-problem?是否有更好的解决方案来解决“等待”问题?

I'm using the latest Selenium/WebDriver release as of now - 2.44.0 .我现在使用的是最新的 Selenium/WebDriver 版本 - 2.44.0

If you just need to wait, then Sleep is fine. 如果您只需要等待,那么“ Sleep就可以了。

The point of not using Sleep is not in the sleep itself, but in the just waiting as you described it. 不使用Sleep的要点不在于睡眠本身,而在于您刚才描述的等待中。 Just waiting is what you should not. 只是等待是您不应该的。 Try changing your thinking, find some event you can attach the wait to. 尝试改变想法,找到一些您可以等待的事件。 Make it conditional. 使其成为有条件的。

In web testing, you don't usually need to just wait . 在网络测试中,通常不需要等待 You wait until page loads, until animation finishes, until form sends, element is clickable, user logs in, script executes, data are processed... That's why Sleep is discouraged, but if you really don't have a single event you are waiting for and simply need to just wait , then using the Sleep is perfectly OK. 您需要等到页面加载,动画完成,表单发送,元素可单击,用户登录,脚本执行,数据处理之后,这就是为什么不建议使用Sleep的原因,但是如果您真的没有单个事件,等待, 只需等待即可 ,然后使用Sleep

Qwerty's answer is correct insofar as the principle is, to paraphrase: 就原则而言, Qwerty的答案是正确的。

Do not "just wait" if you can actually wait for something significant to happen in the browser. 如果您实际上可以等待浏览器中发生的重大事件,请不要“仅等待”。 In other words, you be waiting for a specific event rather than "just waiting". 换句话说,您正在等待特定事件,而不是“仅等待”。

Ok, but let's assume, for the sake of argument, that you're in a situation where there is absolutely positively no event you can wait for. 好的,但是为了争辩,我们假设您处于绝对肯定没有事件可以等待的情况。 I have large test suites that use Selenium and I've never been in this situation, but I'm not omniscient. 我有使用Selenium的大型测试套件,但我从未遇到过这种情况,但我并不无所不知。 There could be situations I'm not foreseeing. 可能存在我无法预见的情况。 It is possible that Thread.sleep() is going to give you want you want, however: 但是, Thread.sleep()可能会给您想要的东西,但是:

  1. If you are running your browser on a remote provisioning service like Sauce Labs or Browser Stack, they may decide that your script is dead and shut down the browser. 如果您在Sauce Labs或Browser Stack之类的远程配置服务上运行浏览器,他们可能会确定您的脚本已死并关闭浏览器。 (Sauce Labs definitely does this. I've never used Browser Stack but I would expect the same.) (Sauce Labs肯定会这样做。我从未使用过Browser Stack,但我希望能做到这一点。)

  2. If a network issue happens while you are waiting and it is such as to cause a test failure, you won't know it until you Thread.sleep() call is done. 如果在等待时发生网络问题,并且会导致测试失败,则直到Thread.sleep()调用完成后,您才知道该问题。 This, again, can happen if you use services like those I've mentioned above. 如果您使用上述服务,同样会发生这种情况。 It happens to my test suites once in a while. 我的测试套件偶尔会发生这种情况。

  3. Or if you are testing locally: the browser crashes or enters an infinite loop. 或者,如果您正在本地进行测试:浏览器崩溃或进入无限循环。 I've not experienced crashes but I've experienced infinite loops. 我没有经历过崩溃,但是经历了无限循环。 Again, if you use Thread.sleep() you won't know about the issue until you are done waiting. 同样,如果您使用Thread.sleep() ,则直到完成等待后,您才知道该问题。

Of course, the longer you want to wait, the more of an issue it is. 当然,您要等待的时间越长,问题就越多。 If you just want to wait for a second... then you should be fine with Thread.sleep() . 如果您只想等待一秒钟...那么使用Thread.sleep() Otherwise, if I were backed against the wall and had to "just wait" I'd still use Selenium's waiting facilities, I'd do something like: 否则,如果我背对墙而不得不 “只是等待”,我仍将使用Selenium的等待设施,那么我将执行以下操作:

try: 
    WebDriverWait(driver, timeout).until(
        lambda driver: driver.execute_script("return false"))
except TimeoutException:
    pass # Do nothing

This is Python code but the principle is the same in any language that Selenium supports: just wait for something that cannot ever occur. 这是Python代码,但是其原理在Selenium支持的任何语言中都是相同的:只需等待永远不会发生的事情。 It is important that the wait executes a test that happens on the browser side because the whole point of doing this instead of using something like Thread.sleep() is so that the browser is contacted and we find issues as early as possible. 等待执行在浏览器端进行的测试非常重要,因为这样做的主要目的是使用浏览器,以便尽早发现问题,而不是使用Thread.sleep()类的东西。 The way WebDriverWait works, it will poll the browser every so often (every 0.5 seconds is the default in the Python implementation). WebDriverWait工作方式,它会如此频繁地轮询浏览器(每0.5秒是Python实现中的默认值)。 The test could be a find_element method that searches for an element that cannot exist. 测试可以是find_element方法,用于搜索不存在的元素。 I've used an execute_script that returns false . 我使用了execute_script返回false Since it is never going to return a true value, the wait will timeout. 由于它永远不会返回true值,因此等待将超时。 (Executing an empty script would also work, as it would return undefined which translates to a falsy value but return false looks deliberate whereas an empty script looks like a mistake.) Then you just need to ignore the exception raised due to timing out. (执行一个空脚本也可以,因为它将返回undefined ,这将转换为一个伪造的值,但return false看起来是故意的,而一个空脚本看起来像是一个错误。)然后,您只需要忽略由于超时而引发的异常。 If you do this often you can even wrap the whole thing in a utility function. 如果经常这样做,您甚至可以将整个内容包装在一个实用程序函数中。

If you want to stay Selenium-integral, you may use:如果你想保持 Selenium-integral,你可以使用:

Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofSeconds(3))

I can see a use case for that when you want to slow down the things a bit to see what is happening in the browser.当您想放慢速度以查看浏览器中发生的事情时,我可以看到一个用例。 So I make it conditional:所以我让它有条件:

Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofMillis(slowDownFactor * 100))

slowDownFactor 0 by default, or more when in a development profile (in my case, a Spring profile, through: slowDownFactor默认为 0,或者在开发配置文件中更多(在我的例子中,Spring 配置文件,通过:

@Value("${debug.slowDownFactor:0}") private int slowDownFactor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM