Selenium 简明教程

Selenium IDE - Creating Tests

可以使用 Selenium IDE 创建自动化测试。Selenium IDE 的最新版本可作为 Chrome 和 Firefox 的扩展。要在 Selenium IDE 上创建测试,基本前提是从其各自的网络商店下载 Selenium IDE 的 Chrome 扩展或 Firefox 扩展。

Selenium IDE can be used to create tests for automation. The latest version of Selenium IDE is available as extensions for both Chrome and Firefox. In order to create tests on Selenium IDE, the basic prerequisite would be to download the Chrome or Firefox extension for Selenium IDE from their respective web stores.

Creating a Test in Selenium IDE

在 Selenium IDE 中创建测试的步骤如下 −

The steps to create a test in Selenium IDE are listed below −

Step 1 − 单击浏览器上在安装后显示的 Selenium IDE 扩展。

Step 1 − Click on the Selenium IDE extension visible on the browser after its installation.

selenium ide creating tests 1

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.

selenium ide creating tests 2

Step 3 - 我们将单击选项 Record a new test in a new project 。然后输入 PROJECT NAME ,如 SeleniumIDE1 。最后,单击“确定”。

Step 3 − We would click on the option Record a new test in a new project. After which we would need to enter the PROJECT NAME, say SeleniumIDE1. Finally, we would click OK.

selenium ide creating tests 3

Step 4 - 在开始使用 Selenium IDE 创建测试之前,我们需要指定应用程序基础 URL,我们将在其中记录和创建测试。在下面的图片中,我们输入了基础 URL: [role="bare" [role="bare"]https://www.tutorialspoint.com/selenium/ ]. 最后,我们需要单击 Start Recording

Step 4 − Before starting with creating a test in Selenium IDE, we would need to specify the application Base URL, where we would record and create the test. In the below image, we had entered the Base URL: [role="bare"https://www.tutorialspoint.com/selenium/]. Finally, we would need to click on the Start Recording.

selenium ide creating tests 4

Step 5 - 在第 4 步中提供基础 URL 的相应应用程序应使用 Selenium IDE 徽标和红色中的 Selenium IDE 记录消息打开。此外,Selenium IDE 的顶部应该可见一个红色图标(在下面的图片中突出显示),表示正在应用程序中进行记录。

Step 5 − The corresponding application whose Base URL had been provided in Step4 should be opened with Selenium IDE logo and Selenium IDE recording message in red. Also, a red icon(highlighted in the below image) should be visible at the top of the Selenium IDE, denoting recording is in progress in the application.

selenium ide creating tests 5

Step 6 − 我们将在应用程序上执行一些步骤,相应步骤将以 Selenium IDE 中记录。

Step 6 − We would perform some steps on the application and corresponding steps would be recorded in the Selenium IDE as well.

selenium ide creating tests 6

Step 7 − 一旦所有测试执行完毕,我们将通过单击上文第 5 步中提到的红色图标停止录制。然后,我们需要为测试输入名称。在下图中,我们在 TEST NAME 里输入了 TestCase1。最后,我们将单击确定。

Step 7 − Once all the tests have been performed, we would stop the recording by clicking on the red icon as mentioned above in Step5. After which, we would need to enter a name for the test. In the below image, we had entered the TEST NAME as TestCase1. Finally, we would click on the OK.

selenium ide creating tests 7

Step 8 − 我们在应用程序上执行的所有步骤将在 Selenium IDE 中的命令、目标和值字段下记录。此外,测试用例名称 - TestCase1 将在左窗格中显示,右上角会有一个 REC 按钮。

Step 8 − All the steps that we had performed on the application would be recorded in the Selenium IDE under the Command, Target, and Value fields. Also, the test case name - TestCase1, would be visible to the left pane along with a REC button to the top right.

selenium ide creating tests 8

Step 9 − 一旦创建了测试,我们就可以通过单击它来获得该步骤的详细信息。完成后,它将在底部的命令、目标和值字段下显示。在下例中,我们点击了第四步,其中 Command, TargetValue 字段的值分别为 type, id=nameSelenium 。在此步骤中,我们基本在 input 框中输入了文本 Selenium,该 input 框由 id 定位器识别,其值是 name。

Step 9 − Once the test would be created, we would be able to get the details on that step, by clicking on it. Once done, it would be visible under the Command, Target, and Value fields at the bottom. In the below example, we had clicked on the fourth step, where the values of Command, Target, and Value fields are type, id=name, and Selenium respectively. In this step, we had basically entered the text Selenium in an input box which was identified with the id locator having the value as name.

selenium ide creating tests 9

Step 10 - 如果我们单击目标字段中的下拉菜单,我们会观察到 Selenium IDE 在记录时成功捕获元素的所有其他定位器值。在下面的示例中,我们单击了第 4 步,其中 values of Command, TargetValue 字段分别是 type, id=nameSelenium 。在此步骤中,该元素的名称、css 等其他定位器值在目标下拉菜单中可见。

Step 10 − If we would click on the dropdown in the Target field, we would observe that Selenium IDE while recording was successful in capturing all the other locator values for an element. In the below example, we had clicked on the fourth step, where the values of Command, Target, and Value fields were type, id=name, and Selenium respectively. In this step, other locator values like the name, css, and so on for that element were visible within the Target dropdown.

selenium ide creating tests 10

Step 11 − 一旦我们点击创建的测试的任何步骤,我们会通过导航到下面的 Reference 标签获得命令和进行操作的详细信息。

Step 11 − Once we would click on any step of the test we had created, we would get the details of the command and the action taken place, by navigating to the Reference tab at the below.

selenium ide creating tests 11

Step 12 - 我们可以右键单击任何步骤并获得手动插入新命令(用于新增步骤)、剪切、复制、清除、切换断点(用于调试)某一步等选项。

Step 12 − We can right click on any step and get the option to manually insert a new command(for addition of a new step), cut, copy, clear, Toggle breakpoint(used for debugging) on a step, and so on.

selenium ide creating tests 12

Step 13 − 要运行创建的测试,我们会点击顶部 Run current test 按钮,如下面的图片所示。

Step 13 − To run the test created, we would click on the Run current test button at the top as shown in the below image.

selenium ide creating tests 13

如果有许多测试,我们需要点击 Run all tests 按钮,如下图所示。

In case, there are multiple tests, we would need to click on the Run all tests button as shown in the below image.

selenium ide creating tests 14

如果已向步骤添加断点,我们需要单击如下图所示的 Step over current command 按钮,其中步骤 2 和步骤 4 具有断点。

In case, there are breakpoints added to steps, we would need to click on the Step over current command button as shown in the below image where the steps 2, and 4 have breakoints.

selenium ide creating tests 15

Step 14 - 我们可以使用 Test execution speed 按钮控制测试执行速度,如下图所示。

Step 14 − We can control the speed of test execution using the Test execution speed button as shown in the below image .

selenium ide creating tests 16

在 Selenium IDE 中,速度默认设置为“快速”。

By default, the speed is set to Fast in Selenium IDE.

selenium ide creating tests 17

Step 15 − Selenium IDE 提供一个选项来创建、打开和保存项目,方法是使用右上角图标,如下图所示。

Step 15 − Selenium IDE gives the option to Create, Open and Save a project using the icons at the top right upper corner as highlighted on the below image.

selenium ide creating tests 18

我们保存项目并继续。一旦保存,我们需要给项目命名并选择项目保存位置。

We would Save our project and continue. Once saved, we would be required to give a Name and location where our project would be saved.

selenium ide creating tests 19

Step 16 - 我们将使用 Run current test button 运行测试 - TestCase1。

Step 16 − We would run the test - TestCase1 using the Run current test button.

Step 17 - 执行完成后,我们会收到结果消息。如果测试通过,我们会在 Selenium IDE 底部的日志选项卡下收到一条消息 - <TEST NAME> 已成功完成。此处,我们的测试名称是 TestCase1,因此收到了消息 - TestCase1 completed successfully

Step 17 − Once the execution would be completed, we would get the message of the result. In case, the test passed, we would get the message - <TEST NAME> completed successfully under the Log tab at the bottom of the Selenium IDE. Here, our test name was TestCase1, hence received the message - TestCase1 completed successfully.

除此之外,所有测试步骤(连同测试用例名称)都变为绿色,表示所有步骤都已通过。在日志选项卡下反映的所有步骤都在绿色中显示了一个 OK。

Apart from that, the 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 中,我们将在测试步骤级别获取有关测试结果的信息,并且仅当所有步骤都成功执行时,我们才将在测试用例级别获得 PASS。

Thus, in Selenium IDE, we would get the information about the test results both at the test step level and only if all the steps would be executed successfully, we would get a PASS at the test case level as well.

selenium ide creating tests 20

因此,在本教程中,我们讨论了如何使用 Selenium IDE 创建测试。

Thus, in this tutorial, we had discussed about how to create a test using the Selenium IDE.