Selenium 简明教程
Selenium WebDriver - Record & Playback
Selenium Webdriver 不能直接用于录制和回放。Selenium 组件通常被称为 Selenium 工具套件。总共有四个工具,一起称为 Selenium 组件。下面列出了 Selenium 的各种组件:
Selenium Webdriver can not be used directly for record and playback. Selenium Components are often referred to as a suite of Selenium tools. There are a total of four tools which are together called the Selenium Components. The various components of Selenium are listed below −
-
Selenium IDE
-
Selenium RC
-
Selenium Webdriver
-
Selenium Grid
Selenium IDE
对于录制和播放功能,我们需要使用 Selenium IDE。
For the record and play feature, we would require the use of Selenium IDE.
Selenium 集成开发环境 (IDE) 是 Selenium 套件的重要组成部分。最早将其开发为 Firefox 插件,但现在它同时适用于 Firefox 和 Chrome 浏览器。下面列出了 Selenium IDE 的一些功能:
Selenium Integrated Development Environment(IDE) is an important part of the Selenium suite. It was first developed as a Firefox plugin, however now it is available in both Firefox and Chrome browser. Some of the features of Selenium IDE are listed below −
-
The recording, debugging, executing and editing of the functional tests can be done in Selenium IDE.
-
The scripts in Selenium IDE are developed in Selenese which is a scripting language in Selenium.
-
Selenium IDE commands help us to perform tasks like clicking a button or link, taking input in an edit box, obtaining a text from a webelement and so on.
-
Selenium IDE tests can be converted to formats like Java, C#, Python, TestNG, and so on. Its default format is in .html.
-
Selenium IDE tests can be exported and used in Selenium Webdriver and Selenium RC.
-
Selenium IDE does not require technical or programming knowledge. Hence it can be used only testing simple scenarios and not used to cover any complex use cases.
-
Selenium IDE does not give the feature of parameterization of tests for data driven testing.
-
Maintenance of objects and other reusable components are difficult in Selenium IDE, in case it is being used for complex testing projects
Selenium IDE 的最新版本作为 Chrome 和 Firefox 的扩展程序提供。为了在 Selenium IDE 上创建测试,基本先决条件是从 Selenium IDE 的各个网络商店下载 Chrome 或 Firefox 扩展程序。
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.
下面列出了在 Selenium IDE 中创建测试以说明录制和回放功能的步骤:
The steps to create a test in Selenium IDE to illustrate a record and playback feature are listed below −
Open Selenium IDE
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 - 我们将单击选项 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.
Recording a Test
Step 1 - 在开始在 Selenium IDE 中创建测试之前,我们需要指定应用程序的基本 URL,在此录制并创建测试。在下面的图片中,我们输入了基本 URL: https://www.tutorialspoint.com/selenium/ 。最后,我们需要单击 Start Recording 。
Step 1 − 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: https://www.tutorialspoint.com/selenium/. Finally, we would need to click on the Start Recording.
Step 2 - 应使用 Selenium IDE 徽标和红色的 Selenium IDE 录制消息打开之前提供基本 URL 的相应应用程序。此外,Selenium IDE 的顶部应可见一个红色图标(在下面的图片中突出显示),表示应用程序中的录制正在进行中。
Step 2 − The corresponding application whose Base URL had been provided previously 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.
Step 3 - 我们将在应用程序中执行一些步骤,相应的步骤也会在 Selenium IDE 中被录制。
Step 3 − We would perform some steps on the application and corresponding steps would be recorded in the Selenium IDE as well.
Step 4 - 一旦所有测试都执行完,我们会通过单击上面步骤 5 中提到的红色图标来停止录制。之后,我们需要为测试输入一个名称。在下面的图片中,我们输入了 TEST NAME 作为 TestCase1。最后,我们会单击确定。
Step 4 − 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.
Step 5 - 我们在应用程序上执行的所有步骤都将在 Selenium IDE 中的命令、目标和值字段下被录制。此外,测试用例名称 - TestCase1,将与 REC 按钮一起显示在左窗格的右上角。
Step 5 − 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.
Step 6 - 一旦创建了测试,便可以通过单击它来获取有关该步骤的详细信息。完成后,它将在底部的命令、目标和值字段下可见。在下面的示例中,我们单击了第四步,其中 Command, Target 和 Value 字段的值分别为 type, id=name 和 Selenium 。在这一步中,我们基本上在具有 id 定位器(其值作为 name)的输入框中输入了文本 Selenium。
Step 6 − 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.
Step 7 - 如果我们单击目标字段中的下拉菜单,我们会发现,在录制时 Selenium IDE 成功捕获了该元素的所有其他定位器值。在下面的示例中,我们单击了第四步,其中 Command, Target 和 Value 字段的值分别为 type, id=name 和 Selenium 。在这一步中,该元素的其他定位器值(如名称、CSS 等)在目标下拉菜单中可见。
Step 7 − 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.
Step 8 - 一旦单击创建的测试的任何步骤, 我们就可以通过导航到下面的 Reference 标签来获取有关命令和操作的详细信息。
Step 8 − 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.
Step 9 - 我们可以右键单击任何步骤,并获得手动插入新命令(用于添加新步骤)、剪切、复制、清除、对某一步骤切换断点(用于调试)等选项。
Step 9 − 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.
Playing back the Recorded Test
Step 1 - 要运行创建的测试,我们会单击顶部如图中所示的 Run current test 按钮。
Step 1 − To run the test created, we would click on the Run current test button at the top as shown in the below image.
如果有许多测试,我们需要点击 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.
如果有断点添加到步骤,我们需要点击 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 breakpoints.
Step 2 - 我们可以使用图中所示的 Test execution speed 按钮来控制测试执行速度。
Step 2 − We can control the speed of test execution using the Test execution speed button as shown in the below image .
在 Selenium IDE 中,速度默认设置为“快速”。
By default, the speed is set to Fast in Selenium IDE.
Step 3 - Selenium IDE 提供了使用放在右上角的图标来创建、打开和保存项目的功能,如图中突出显示的那样。
Step 3 − 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.
我们保存项目并继续。一旦保存,我们需要给项目命名并选择项目保存位置。
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.
Step 4 - 我们会使用 Run current test 按钮运行测试 - TestCase1。
Step 4 − We would run the test - TestCase1 using the Run current test button.
Analyze Test Result after Playback
执行完成后,我们将获得结果消息。如果测试通过,我们会在 Selenium IDE 底部的 Log 选项卡下收到消息 - <TEST NAME> completed successfully(TEST NAME 完成成功)。此处,我们的测试名称为 TestCase1,因此收到消息 - TestCase1 completed successfully 。
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.
除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 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 中,我们将在测试步骤级别获取有关测试结果的信息,并且仅当所有步骤都成功执行时,我们才将在测试用例级别获得 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 Webdriver 教程中关于“录制与回放”部分的全面理解。我们从描述 Selenium IDE 开始(因为 Selenium Webdriver 不能直接用于录制和回放),并逐步讲解了如何打开 Selenium IDE、录制测试、回放录制的测试,以及在回放后分析测试结果。这让你对 Selenium IDE 有了深入的了解。明智的做法是反复练习所学内容,并探索其他与 Selenium 相关的内容,以加深对 Selenium 的理解并拓展眼界。
This concludes our comprehensive take on the tutorial on Selenium Webdriver - Record & Playback. We’ve started with describing the Selenium IDE (since Selenium Webdriver can not be used directly for record & playback) and walked through how to open Selenium IDE, Recording a Test, Playing back the Recorded Test, and Analyze Test Result after Playback. This equips you with in-depth knowledge of the 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.