Selenium 简明教程

Selenium IDE - Assert/Verify Methods

Selenium IDE 使用 assert 和 verify 方法来检查特定的文本、元素、页面标题等是否与要求相匹配。这两种方法基本上帮助我们基于添加的验证点(以 assert 和 verify 命令的形式)来判断测试是通过还是失败。

Selenium IDE uses assert and verify methods to check if a certain text, element, page title, and so on matches with the requirements. These two methods basically help us to conclude if a test has passed or failed based on the verification points added (in the form of assert and verify commands).

What is an Assert Command?

在 Selenium IDE 中,assert 命令用于验证某些情况是否与规范文档相匹配。如果它们不匹配,测试执行将在不匹配处停止。

In Selenium IDE, an assert command is used to verify if a certain situation is matching with the specification document. In case, they are not matching, the test execution stops at the point of a mismatch.

Example with Assert Command

让我们以以下页面为例,我们将在其中捕获页面标题 Selenium Practice Radio Button 并使用 assert 命令检查是否显示了正确的页面标题。

Let us take an example of the below page, where we would capture the page title Selenium Practice Radio Button and check if the correct page title is displayed using the assert command.

selenium assert verify 1

使用 assert 命令的步骤如下 −

The steps to use an assert command are listed below −

Step 1 − 按照 * Selenium IDE Store Variables* 中所述执行步骤 1 至步骤 4。

Step 1 − Follow the Steps 1 to 4 as described in the link Selenium IDE Store Variables.

Step 2 − 在“Command”字段输入“Open”,在“Target”字段输入 * Selenium Automation Practice Form* ,以启动应用程序。

Step 2 − Enter open in the Command field, and Selenium Automation Practice Form in the Target field, to launch the application.

Step 3 − 在“命令”字段中输入 store title,在“值”字段中输入 pageTitle。请注意,pageTitle 是变量,用于捕获启动的应用程序标题。

Step 3 − Enter store title in the Command field, and pageTitle in the Value field. Please note, pageTitle is variable to capture the title of the application launched.

Step 4 − 在“命令”字段中输入 assert title,在“目标”字段中输入 ABC。请注意,在此步骤中,我们要验证页面标题是否是 ABC。

Step 4 − Enter assert title in the Command field, ABC in the Target field. Please note, in this step we are verifying if the page tile is ABC.

Step 5 − 在“命令”字段中输入 echo,在“目标”字段中输入 ${pageTitle}。请注意,这将打印我们在第 6 步中在 Selenium IDE 的日志中得到的内容。

Step 5 − Enter echo in the Command field, ${pageTitle} in the Target field. Please note, this would print the text we obtained in Step6 under the Log in Selenium IDE.

Step 6 − 在“命令”字段中输入 close 以关闭浏览器窗口。

Step 6 − Enter close in the Command field to close the browser window.

Step 7 − 单击左窗格中显示的三个点,然后选择“重命名”选项。

Step 7 − Click on the three dots appearing at the left pane, then select the Rename option.

Step 8 − 在“重命名测试用例”字段中输入名称(比如说 Test6),然后单击“重命名”。输入的名称将显示在 Selenium IDE 的左侧。

Step 8 − Enter a name, say Test6 under the Rename Test case field, then click on Rename. The entered name would appear on the left of Selenium IDE.

Step 9 − 单击顶部的“全部运行测试”,然后等待测试执行完成。在我们的示例中,我们将看到“运行:1,失败:1”,以及红色栏表示测试未成功运行且有失败。此外,消息 Test6 ended with 1 error(s) 将显示在日志中。

Step 9 − Click on the Run all tests from the top, and wait for the test execution to complete. In our example, we would see Runs: 1, Failures: 1, and a red bar denoting the test ran unsuccessfully with failures. Also, the message Test6 ended with 1 error(s) would appear under the Log.

selenium assert verify 3

在上面的示例中,我们已经获得了第 Selenium Practice Radio Button 页的标题。然而,我们添加了 assert title 命令来检查页面标题是否等于 ABC 。因此,实际值和预期值不匹配。此外,由于 assert 命令,执行停止了,第 4 步和第 5 步没有执行。

In the example above, we had obtained the page title which is Selenium Practice Radio Button. However, we had added the assert title command to check if the page title is equal to ABC. Hence the actual and the expected values did not match. Moreover, due to the assert command, the execution halted and the steps 4, and 5 did not execute.

What is a Verify Command?

在 * Selenium IDE* 中,verify 命令也用于验证特定情况是否与规范文档相符。当然,如果它们不匹配,则测试执行不会在不匹配点停止。错误被捕获在 Selenium IDE 的日志中,并且在发现错误之后执行的步骤也会被执行。

In Selenium IDE, a verify command is also used to verify if a certain situation is matching with the specification document. In case, they are not matching, the test execution does not stop at the point of a mismatch. The error is captured at the Log in Selenium IDE and the steps after the error IS encountered also get executed.

Example with Verify Command

让我们以上面的示例为基础,其中我们将捕获页面标题“Selenium Practice Radio Button”并使用 verify 命令来检查是否显示了正确的页面标题。

Let us take the same above example, where we would capture the page title Selenium Practice Radio Button and check if the correct page title is displayed using the verify command.

捕获浏览器标题的步骤如下 −

The steps to capture the browser title are listed below −

Step 1 − 按照 * Selenium IDE Store Variables* 中所述执行步骤 1 至步骤 4。

Step 1 − Follow the Steps 1 to 4 as described in the link Selenium IDE Store Variables.

Step 2 − 按照先前示例中的第 2 步和第 3 步操作。

Step 2 − Follow steps 2 and 3 from the previous example.

Step 3 − 在“命令”字段中输入 verify title,在“目标”字段中输入 ABC。请注意,在此步骤中,我们正在验证页面标题是否是 ABC。

Step 3 − Enter verify title in the Command field, ABC in the Target field. Please note, in this step we are verifying if the page tile is ABC.

Step 4 − 按照先前示例中的第 8、9、10、11 和 12 步操作。

Step 4 − Follow steps 8,9, 10, 11, and 12 from the previous example.

selenium assert verify 4

在上面的示例中,我们已经获得了第 Selenium Practice Radio Button 页的标题。然而,我们添加了 verify title 命令来检查页面标题是否等于 ABC 。因此,实际值和预期值不匹配。此外,即使在第 3 步中遇到失败,第 4 步和第 5 步也会执行,并且执行不会在中途停止,同时,错误也会记录在日志中。

In the example above, we had obtained the page title which is Selenium Practice Radio Button. However, we had added the verify title command to check if the page title is equal to ABC. Hence the actual and the expected values did not match. Moreover, even after encountering failure in step3 the steps 4, and 5 executed and the execution did not stop in between, simultaneously, the error also got recorded under the Log.

Difference between Assert and Verify Commands

从上面的示例中,我们可以推断出 assert 和 verify 命令都可以用于验证目的。但是,在测试中遇到错误时,执行会在失败步骤停止,剩余的步骤不会在 assert 命令使用时执行。然而,在使用 verify 命令时,即使遇到失败步骤,执行也会继续进行。针对这两个命令,遇到的错误也会记录在 Selenium IDE 的日志中。

From the above examples, we can infer that both assert and verify commands can be used for verification purposes. But in case of errors encountered in a test, the execution stops at the failed step, and remaining steps do not get executed while assert command is used. However, the execution continued even after encountering a failed step, while we are using the verify command. The error encountered is also recorded under the Log in Selenium IDE for both the commands.

Conclusion

到此,我们完成了 Selenium IDE Assert/Verify 方法教程的全面讲解。我们首先介绍了 Selenium IDE 中的 assert 命令和 verify 命令是什么,然后通过包含 assert 命令和 verify 命令的示例,说明了如何使用它们以及这两个命令之间的基本差异。这让你深入了解了 Selenium IDE 中的 assert 和 verify 方法。明智的做法是不断实践你所学到的知识并探索与 Selenium 相关的其他知识,以加深你的理解并拓宽你的视野。

This concludes our comprehensive take on the tutorial on Selenium IDE Assert/Verify methods. We’ve started with describing what is an assert and a verify command in Selenium IDE, and walked through examples having an assert command and a verify command, illustrated how to use them, and the basic differences between the two commands along with Selenium. This equips you with in-depth knowledge of the assert and verify methods in Selenium IDE. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.