It is possible to synchronize a test case to your Application Under Test (AUT) using fixed-time syncronization (snooze) or variable-time synchronization (waitFor, waitForObject, waitForObjectExists). Fixed-time synchronization should be avoided, if possible, in favor variable-time synchronization. Reasons include:

  • 在一个系统上执行某项操作的正确时间可能不是在另一系统上进行同一项操作的正确时间。
  • 固定时间的延后加起来,尤其是如果它们以循环方式发生,则会增加总体测试执行时间。
  • 固定时间的延后是人为的:即使在同一系统上,它们也不会反映或适应可变的应用程序性能。

在这里,我们介绍了三个用例,其中初始示例脚本仅使用固定时间同步,并且我们重新设计了脚本以使用可变时间同步。这些示例演示了通过在 压扁 GUI测试器 框架。

What is 同步化and Why Do I Need It?

同步化—您的应用程序和测试脚本—有必要防止您的测试(和运行它的工具)对应用程序运行太快’s toolkit to keep up. 同步化gives your AUT the time it needs to process events before moving to the next line of the script.

Inserting snooze() statements is the simplest method of synchronizing your AUT and script, but it’由于上述原因,通常不足以创建可靠的测试。让’看一些例子。

用例1:等待对象出现

这里’是Python中的示例脚本:

clickButton(waitForObject(names.open_PushButton)) # webView takes 25-30 seconds to appear snooze(25) webView = waitForObject(names.webView) # loading web contents takes up to 10 seconds snooze(10)
Code language: Python (python)

The first snooze adds a delay to avoid a timeout in the following waitForObject() call, awaiting the webview becoming visible and available for GUI interaction. Our script assumes webview readiness in 25 seconds or less. But is 那 robust, or even accurate?

We could instead allow a (much) longer timeout on waitForObject. The amount is measured in milliseconds, and this waitForObject now has a timeout of 30 seconds instead of 20, the default.

clickButton(waitForObject(names.open_PushButton)) webView = waitForObject(names.webView, 30000) # loading web contents takes up to 10 seconds snooze(10)
Code language: Python (python)

这用更安全的填充替换了不灵活的,易碎的snooze()。

用例2:等待数据加载或处理

If we are waiting for a complex object 那 needs to load or process data, a short snooze() after the waitForObject might be used, but if there is another condition or property which changes in your AUT once the data is fully loaded, you could waitFor() instead. 这里’s an example:

clickButton(waitForObject(names.open_PushButton)) webView = waitForObject(names.webView, 30000) # loading web content can take up to 10 seconds waitFor(lambda: webView.loadProgress == 100, 10000)
Code language: Python (python)

(在10秒钟内删除#comment,此处不需要)

用例3:验证对象尚未准备好与用户交互

A snooze() before a findObject() might happen if we need to verify 那 a certain GUI object is disabled or invisible.

snooze(10) goButton = findObject(names.goButton) test.verify(goButton.enabled == False)
Code language: Python (python)

In this case, waitForObject cannot be used because it also requires objects to be visible and enabled. The first two statements can be replaced by waitForObjectExists() instead, provided the timeout is long enough:

goButton = waitForObjectExists(names.goButton) test.verify(goButton.enabled == False)
Code language: Python (python)

结论

Variable-time synchronization approaches are recommended for their robustness and flexibility, especially in cross-platform GUI testing. 这里, we saw how 压扁 offers a number of ways to get around hardcoded delays in your script. Avoiding fixed-time synchronization with snooze() can help improve the overall runtime of your tests, getting you your results faster.

推荐阅读

More on synchronization using the waitFor() function is provided 在这个博客中.

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *

复制链接