Selenium 简明教程
Selenium IDE - Verification Points
Selenium IDE 允许在测试用例中添加多个验证点,以检查应用程序是否按预期工作。在 Selenium IDE 中使用的每个命令都称为 Selenese 命令。使用 Selenese 命令,我们对 Selenium IDE 中的网页应用程序执行所有测试活动。
Selenium IDE allows adding multiple verification points within the test case to check if the application is working as expected. Every command used in Selenium IDE is known as the Selenese commands. Using the Selenese commands we perform all the testing activities on a web application in Selenium IDE.
Selenese 命令由三种类型组成 - 操作、断言和访问器。断言命令有助于在 Selenium IDE 测试中添加验证点。断言命令用于检查被测应用程序的状态。它有三个子类型,即断言、验证和 waitfor。
Selenese commands are composed of three types - actions, assertions, and accessors. The assertions commands help to add verification points in the Selenium IDE tests. The assertion commands are used to check the state of the application under test. It has three sub-types namely, assert, verify, and waitfor.
How to Insert Verification Points While Recording?
可以在记录测试时向 Selenium IDE 中的测试添加验证点。在下图中,我们通过右键单击 Name: 标签旁边的输入框添加了验证点 - 断言。然后我们选择了选项 Selenium IDE ,最后,我们必须选择选项 Assert 。通过这样做,所有可用的断言命令都将准备就绪,可以进行选择。
The verification points can be added to tests in Selenium IDE while recording a test. In the below image, we have added the verification point - assert, by right clicking on the input box beside the Name: label. Then we have selected the option Selenium IDE, and finally, we have to select the option Assert. By doing so, all the available assert commands will be ready to be selected.
在下图中,我们通过右键单击 Name: 标签旁边的输入框添加了验证点 - 验证。然后我们选择了选项 Selenium IDE ,最后,我们必须选择选项 Verify 。通过这样做,所有可用的验证命令都将准备就绪,可以进行选择。
In the below image, we have added the verification point verify, by right clicking on the input box beside the Name: label. Then we have selected the option Selenium IDE, and finally, we have to select the option Verify. By doing so, all the available verify commands will be ready to be selected.
在下图中,我们通过右键单击 Name: 标签旁边的输入框添加了验证点 - 等待。然后我们选择了选项 Selenium IDE ,最后,我们必须选择选项 Wait For 。通过这样做,所有可用的等待命令都将准备就绪,可以进行选择。
In the below image, we have added the verification point wait for, by right clicking on the input box beside the Name: label. Then we have selected the option Selenium IDE, and finally, we have to select the option Wait For. By doing so, all the available wait for commands will be ready to be selected.
要更详细地了解如何设置 Selenium IDE,请参阅链接 - Selenium IDE Installation 。
To get a more detailed view on how to set up of Selenium IDE, please refer to the link − Selenium IDE Installation.
Basic Verification Commands in Selenium IDE
以下是 Selenium IDE 中可用的各种 verification commands −
The following are the various types of verification commands available in Selenium IDE −
-
assert(variable, expected value)
-
assert alert(alert text)
-
assert checked(locator)
-
assert confirmation(text)
-
assert editable(locator)
-
assert element present(locator)
-
assert element not present(locator)
-
assert not checked(locator)
-
assert not editable(locator)
-
assert not selected value(locator, text)
-
assert not text(locator, text)
-
assert prompt(text)
-
assert selected value(locator, text)
-
assert selected label(locator, text)
-
assert text(locator, text)
-
assert title(text)
-
assert value(locator, text)
-
verify(variable, expected value)
-
verify checked(locator)
-
verify editable(locator)
-
verify element present(locator)
-
verify element not present(locator)
-
verify not editable(locator)
-
verify not selected value(locator, text)
-
verify not text(locator, text)
-
verify selected label(locator, text)
-
verify selected value(locator, text)
-
verify text(locator, text)
-
verify value(locator, text)
-
wait for element editable(locator, wait time)
-
wait for element not editable(locator, wait time)
-
wait for element not present(locator, wait time)
-
wait for element not visible(locator, wait time)
-
wait for element present(locator, wait time)
-
wait for element visible(locator, wait time)
Example
让我们以下面页面的示例为例,其中我们将会单击 Created 链接。
Let us take an example of the below page, where we would click on the Created link.
我们将会验证其页面标题为 Selenium Practice - Links 。点击创建链接之后,我们还将验证文本 - Link has responded with status 201 and status text Created 。
We would verify its page title which is Selenium Practice - Links. After clicking the Created link, we would also verify the text - Link has responded with status 201 and status text Created.
要遵循的步骤如下所示 -
The steps to be followed are listed below −
Step 1 − 单击浏览器上在安装后显示的 Selenium IDE 扩展。
Step 1 − Click on the Selenium IDE extension visible on the browser after its installation.
Step 2 − Selenium IDE 应该与它一起启动,应该显示一个显示该工具版本的欢迎消息。在下图中,安装的版本为 3.17.2。
Step 2 − Selenium IDE should be launched along with it, a welcome message should be displayed with the version of the tool. In the below image, the version installed is 3.17.2.
除此之外,它还为我们提供了选择我们想使用该工具做什么的选项,例如在新项目中记录新测试、打开现有项目、创建新项目和关闭 Selenium IDE。
Along with this, it gives us the option to select what we would like to do with the tool, like Record a new test in a new project, Open an existing project, Create a new project, and Close Selenium IDE.
此外,还提供了带有文本 - the Selenium IDE project page 的链接,单击该链接将使我们进入 Selenium 集成开发环境文档页面。
Also, a link with the text - the Selenium IDE project page is provided, which on clicking would land us to the Selenium Integrated Development Environment documentation page.
Step 3 - 单击创建新项目链接,然后在为新项目指定名称下输入一个项目名称,比如说 Test8。最后,我们将单击确定按钮。
Step 3 − Click on the Create a new project link, then enter a project name, say, Test8 under the Please provide a name for your new project. Finally, we would click on the OK button.
Step 4 − 我们现在在 Selenium IDE 中准备好了工作区。
Step 4 − We would have the workspace ready in the Selenium IDE now.
Step 5 - 在命令字段中输入 open,在目标字段中输入 Selenium Automation Practice Form ,启动应用程序。
Step 5 − Enter open in the Command field, and Selenium Automation Practice Form in the Target field, to launch the application.
Step 6 - 在命令字段中输入验证标题,在目标字段中输入 Selenium Practice - Links。
Step 6 − Enter verify title in the Command field and Selenium Practice - Links in the Target field.
Step 7 - 在命令字段中输入点击,xpath=// [@id="created"] in the Target field. Please note, xpath=// [@id="created"] 是链接定位符的值。
Step 7 − Enter click in the Command field, xpath=//[@id="created"] in the Target field. Please note, xpath=//[@id="created"] is the locator value of the link.
Step 8 - 在命令字段中输入等待元素可见,在目标字段中输入 xpath=/html/body/main/div/div/div[2]/div[1],在值字段中输入 30000。请注意,xpath=/html/body/main/div/div/div[2]/div[1] 是文本定位符的值。
Step 8 − Enter wait for element visible in the Command field, xpath=/html/body/main/div/div/div[2]/div[1] in the Target field and 30000 in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/div[1] is the locator value of the text to be visible.
Step 9 - 在命令字段中输入断言文本,在目标字段中输入 xpath=/html/body/main/div/div/div[2]/div[1],在值字段中输入链接已响应,状态为 201 并且状态文本已创建。请注意,xpath=/html/body/main/div/div/div[2]/div[1] 是文本的定位符值。
Step 9 − Enter assert text in the Command field, xpath=/html/body/main/div/div/div[2]/div[1] in the Target field and Link has responded with status 201 and status text Created in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/div[1] is the locator value of the text.
Step 10 − 在Command字段中输入close以关闭浏览器窗口。
Step 10 − Enter close in the Command field to close the browser window.
Step 11 − 单击出现在左侧窗格中的三个点,然后选择Rename选项。
Step 11 − Click on the three dots appearing at the left pane, then select the Rename option.
Step 12 - 在重命名测试用例字段中输入一个名称,比如说 Test8,然后单击重命名。输入的名称将显示在 Selenium IDE 的左侧。
Step 12 − Enter a name, say Test8 under the Rename Test case field, then click on Rename. The entered name would appear on the left of Selenium IDE.
Step 13 − 从顶部单击Run all tests,然后等待测试执行完成。在我们的示例中,我们将看到Runs: 1, Failures: 0和一个绿色条表示测试已成功运行,没有失败。此外,已成功完成的消息出现在Log下方。
Step 13 − 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: 0, and a green bar denoting the test ran successfully without any failures. Also, the message that the completed successfully appeared under the Log.
在上面的示例中,我们已启动该应用程序,并使用断言命令 verify title 验证了其页面标题 - Selenium Practice - Links 。然后点击 Created link ,并使用断言命令 wait for element visible 等待文本 Link has responded with status 201 and status text Created 可见,然后使用另一个断言命令断言文本。
In the example above, we had launched the application and verified its page title - Selenium Practice - Links using the assertions command verify title. Then clicked on the Created link, and waited for text Link has responded with status 201 and status text Created to be visible with the assertion command wait for element visible and verified the text with another assertion command assert text.
除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 PASS。所有在日志选项卡下显示的步骤均显示绿色 OK。
Apart from that, all the test steps, along with the test case name turned green, reflecting a PASS for all the steps. All the steps which reflected under the Log tab, showed an OK in green.
至此,我们对 Selenium IDE - 验证点教程的全面讲解就结束了。我们从描述什么是验证点、如何插入验证点、基本验证命令开始,并通过一个示例来说明如何将验证命令与 Selenium 一起使用。
This concludes our comprehensive take on the tutorial on Selenium IDE - Verification Points. We’ve started with describing what are verification points, how to insert verification points, basic verification commands, and an example to walk through how to use the verification commands along with Selenium.
这让你深入了解了 Selenium IDE 中的验证点。明智的做法是不断练习你所学到的内容,并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。
This equips you with in-depth knowledge of the verification points 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.