Selenium 简明教程
Selenium IDE - Store Variables
Selenium 集成开发环境可用于在创建自动化测试时存储和访问变量。为了存储变量并访问它们,我们可以在 Selenium IDE 中使用 store 及其相关命令。我们使用命令 echo 在 Selenium IDE 的“日志”选项卡下打印变量的存储值。我们还可以使用 store Text 命令访问网页上的元素并获取其文本,并使用 store Value 命令获取元素的值。
Selenium Integrated Development Environment can be used to store and access variables while creating a test for automation. To store variables and to access them, we can use the store and its related commands in Selenium IDE. We use the command echo to print the stored value of the variable under the Logs tab in Selenium IDE. We can also access an element on a web page and obtain its text using the store Text command and obtain the value of an element using the store Value command.
Example
让我们举一个下面页面的示例,我们将在 First Name: 标签旁边输入文本 Selenium ,在 Last Name: 标签旁边输入 Tutorialspoint 。此外,我们将使用 Selenium IDE 并借助此工具的存储变量功能来获取文本 Selenium - Automation Practice Form 。
Let us take an example of the below page, where we would enter the text Selenium beside the First Name: label and Tutorialspoint beside Last Name: label. Also, we would obtain the text Selenium - Automation Practice Form using the Selenium IDE taking the help of the store variables feature of this tool.
Storing and Accessing Variables in Selenium IDE
在 Selenium IDE 中存储变量并访问它们的步骤如下所示:
The steps to store variables and access them 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.
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 - 单击“创建新项目”链接,然后在 Please provide a name for your new project 下输入项目名称。最后,我们将单击“确定”。
Step 3 − Click on the Create a new project link, then enter a project name under the Please provide a name for your new project. Finally, we would click OK.
Step 4 - 我们将准备好在 Selenium IDE 中的工作区,如下图所示。
Step 4 − We would have the workspace ready in the Selenium IDE as shown in the below image.
Step 5 - 在“命令”字段中输入 open,在“目标”字段中输入 https://www.tutorialspoint.com/selenium/ ,以启动应用程序。
Step 5 − Enter open in the Command field, and https://www.tutorialspoint.com/selenium/ in the Target field, to launch the application.
Step 6 - 在“命令”字段中输入 store,在“目标”字段中输入 Selenium,在“值”字段中输入 i。请注意,Selenium 是我们想要使用变量 i 输入的值。
Step 6 − Enter store in the Command field, Selenium in the Target field, and i in the Value field. Please note, Selenium is the value we would like to enter using the variable i.
Step 7 - 在“命令”字段中输入 type,在“目标”字段中输入 id=firstname,在“值”字段中输入 ${i}。请注意,id=firstname 是输入框的定位器值,${i} 用于引用我们将使用变量 i 输入的变量值。
Step 7 − Enter type in the Command field, id=firstname in the Target field, and ${i} in the Value field. Please note, id=firstname is the locator value of the input box and ${i} is used to refer to the variable value we would enter using the variable i.
Step 8 - 在“命令”字段中输入 store,在“目标”字段中输入 Tutorialspoint,在“值”字段中输入 j。请注意,Tutorialspoint 是我们想要使用变量 j 输入的值。
Step 8 − Enter store in the Command field, Tutorialspoint in the Target field, and j in the Value field. Please note, Tutorialspoint is a value we would like to enter using the variable j.
Step 9 - 在“命令”字段中输入 type,在“目标”字段中输入 id=lastname,在“值”字段中输入 ${j}。请注意,id=lastname 是输入框的定位器值,${j} 用于引用我们将使用变量 j 输入的变量值。
Step 9 − Enter type in the Command field, id=lastname in the Target field, and ${j} in the Value field. Please note, id=lastname is the locator value of the input box and ${j} is used to refer to the variable value we would enter using the variable j.
Step 10 - 在“命令”字段中输入 store Text,在“目标”字段中输入 css=div > h1,在“值”字段中输入 k。请注意,css=div > h1 是我们将获取的文本的定位器值,k 用于引用我们将存储获取到的文本的变量值。
Step 10 − Enter store Text in the Command field, css=div > h1 in the Target field, and k in the Value field. Please note, css=div > h1 is the locator value of the text that we would obtain and k is used to refer to the variable value where we would store the text obtained.
Step 11 - 在“命令”字段中输入 echo,在“目标”字段中输入 ${k}。请注意,这将打印我们在步骤 10 中访问的文本,该文本位于 Selenium IDE 中的“日志”下的网页上。
Step 11 − Enter echo in the Command field, ${k} in the Target field. Please note, this would print the text we accessed in Step10 on the web page under the Logs in Selenium IDE.
Step 12 - 在“命令”字段中输入 store value,在“目标”字段中输入 id=firstname,在“值”字段中输入 l。请注意,id=firstname 是输入框的定位器值,l 用于引用我们将用于获取输入框中输入的值的变量。
Step 12 − Enter store value in the Command field, id=firstname in the Target field, and l in the Value field. Please note, id=firstname is the locator value of the input box and l is used to refer to the variable we would use to obtain the value entered in the input box.
Step 13 - 在“命令”字段中输入 echo,在“目标”字段中输入 ${l}。请注意,这将打印我们在步骤 7 中输入的值,该值位于 Selenium IDE 中的“日志”下的网页上。
Step 13 − Enter echo in the Command field, ${l} in the Target field. Please note, this would print the value entered in Step7 on the web page under the Logs in Selenium IDE.
Step 14 - 输入关闭 在命令字段中,以关闭浏览器窗口。
Step 14 − Enter close in the Command field to close the browser window.
Step 15 - 单击左侧窗格中显示的三个点,然后选择“重命名”选项。
Step 15 − Click on the three dots appearing at the left pane, then select the Rename option.
Step 16 — 在“重命名测试用例”字段下输入名称,比如 TestCase2,然后单击“重命名”。输入的名称将出现在 Selenium IDE 的左侧。
Step 16 − Enter a name, say TestCase2 under the Rename Test case field, then click on Rename. The entered name would appear on the left of Selenium IDE.
Step 17 — 单击顶部的“运行所有测试”,并等待测试执行完成。在我们的示例中,我们将看到 Runs: 1、Failures: 0,以及一个绿色条表示测试成功运行且没有任何失败。另外,消息 TestCase2 completed successfully(TestCase2 已成功完成)出现在日志下。
Step 17 − 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 TestCase2 completed successfully appeared under the Logs.
在以上示例中,我们已获取显示在网页上的文本 Selenium - Automation Practice Form ,并且还检索了在 First Name: 标签旁边的输入框中输入的值 - Selenium 在日志下。此外,我们在测试用例名称 - TestCase2 旁边获得了一个绿色勾号,表示测试通过。
In the example above, we obtained the text Selenium - Automation Practice Form which appeared on the web page, and also retrieved the value entered in the input box beside the First Name: label - Selenium under the Logs. Besides, we got a green tick beside the test case name - TestCase2 at the top, signifying a passed test.
除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 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.
此外,我们可以推断在一步中使用的变量可以使用语法 ${<variable name>} 在其他步骤中进一步访问。在步骤 6、7、8、9、10、11、12 和 13 中,我们在测试中遵循了此方法。此外,我们还使用 store 和 store 相关命令(如 store Text、store Value 等)来存储变量。echo 命令用于在“日志”选项卡下打印这些值。
Moreover, we would infer that variables used in one step can be accessed further in other steps using the syntax: ${<variable name>}. In the Steps6, 7, 8, 9, 10, 11, 12, and 13, we followed this approach in our test. Also, we had used store and store related commands like store Text, store Value, and so on to store the variables. The echo command was used to print those values under the Logs tab.
因此,在本教程中,我们讨论了如何使用 Selenium IDE 存储变量。
Thus, in this tutorial, we had discussed how to store variables using the Selenium IDE.