Selenium 简明教程

Selenium IDE - Selenese Commands

Selenium IDE 中使用的每个命令都称为 Selenese 命令。使用 Selenese 命令,我们在 Web 应用程序上执行所有测试活动。Selenese 命令有三种类型:

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. Selenese commands are of three types −

因此 Selenese 是一种用于使用 Selenium IDE 创建测试自动化的语言。我们还可以在 Selenium IDE 命令中定义 Selenese。这些 Selenese 命令可广泛用于测试 Web 应用程序的功能。

Thus Selenese is a language which is used to create test automation using Selenium IDE. We can also define Selenese in Selenium IDE commands. These Selenese commands can be used extensively to test the functionalities of the web applications.

Basic Actions Commands

操作命令用于控制正在测试的应用程序的状态。Selenium IDE 中提供了各种操作命令。

The action commands are used to manipulate the state of the application under test. There various types of actions commands available in Selenium IDE.

  1. click(locator)

  2. check(locator)

  3. uncheck(locator)

  4. choose cancel on next confirmation

  5. choose cancel on next prompt

  6. choose ok on next confirmation

  7. click At(locator, coordinateString)

  8. close

  9. debugger

  10. double click(locator)

  11. double click at(locator)

  12. drag and drop to object(source locator, destination locator)

  13. echo(message)

  14. edit content(locator, value)

  15. execute script(script, variable name)

  16. execute async script(script, variable name)

  17. mouse down(locator)

  18. mouse down at(locator, coordinateString)

  19. mouse move at(locator, coordinateString)

  20. mouse out(locator)

  21. mouse over(locator)

  22. mouse up(locator)

  23. mouse up at(locator, , coordinateString)

  24. open(url)

  25. pause(wait time)

  26. remove selection(locator, option)

  27. run(test case)

  28. select(locator, option)

  29. select frame(locator)

  30. select window(window handle)

  31. set window size(resolution)

  32. type(locator)

Basic Accessors Commands

访问器命令用于控制正在测试的应用程序的状态。

The accessors commands are used to manipulate the state of the application under test.

以下是访问器命令:

The following are the accessors command −

  1. store(text, variable)

  2. store attribute(locator, variable)

  3. store text(locator, variable)

  4. store title(text, variable)

  5. store value(locator, variable)

  6. store window handle(window handle)

  7. store xpath count(xpath, variable)

Basic Assertions Commands

断言命令用于检查正在测试的应用程序的状态。Selenium IDE 中提供了各种断言命令。

The assertion commands are used to check the state of the application under test. There various types of assertions commands available in Selenium IDE.

  1. assert(variable, expected value)

  2. assert alert(alert text)

  3. assert checked(locator)

  4. assert confirmation(text)

  5. assert editable(locator)

  6. assert element present(locator)

  7. assert element not present(locator)

  8. assert not checked(locator)

  9. assert not editable(locator)

  10. assert not selected value(locator, text)

  11. assert not text(locator, text)

  12. assert prompt(text)

  13. assert selected value(locator, text)

  14. assert selected label(locator, text)

  15. assert text(locator, text)

  16. assert title(text)

  17. assert value(locator, text)

  18. verify(variable, expected value)

  19. verify checked(locator)

  20. verify editable(locator)

  21. verify element present(locator)

  22. verify element not present(locator)

  23. verify not editable(locator)

  24. verify not selected value(locator, text)

  25. verify not text(locator, text)

  26. verify selected label(locator, text)

  27. verify selected value(locator, text)

  28. verify text(locator, text)

  29. verify value(locator, text)

  30. wait for element editable(locator, wait time)

  31. wait for element not editable(locator, wait time)

  32. wait for element not present(locator, wait time)

  33. wait for element not visible(locator, wait time)

  34. wait for element present(locator, wait time)

  35. wait for element visible(locator, wait time)

Example

让我们以以下页面为例,在该页面中,我们将使用访问器命令捕获页面标题 Selenium Practice - Radio Button 。然后使用操作命令单击 Yes 标签旁边的圆形按钮。

Let us take an example of the below page, where we would capture the page title Selenium Practice - Radio Button taking the help of the accessors commands. Then click on the radio button beside the Yes label using the actions commands.

selenium selenese commands 1

选择圆形按钮后,我们将获得文本 - You have checked Yes ,我们使用断言命令对其进行验证。

After selecting the radio button, we would get the text - You have checked Yes, which we would verify using the assertions command.

selenium selenese commands 2

要遵循的步骤如下所示 -

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.

selenium selenese commands 3

Step 3 − 单击Create a new project链接,然后输入项目名称,例如在 Please provide a name for your new project 下输入Test8。最后,我们将单击OK按钮。

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.

selenium selenese commands 4

Step 5 − 在Command字段中输入open,在Target字段中输入 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 − 在Command字段中输入store title,在Value字段中输入pageTitle。请注意,pageTitle是用于捕获已启动页面的标题的变量。

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

Step 7 − 在Command字段中输入echo,在Target字段中输入${pageTitle}。请注意,这将打印我们在第 6 步中在 Selenium IDE 中获取的文本。

Step 7 − 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 8 − 在Command字段中输入click,在Target字段中输入xpath=/html/body/main/div/div/div[2]/form/div[1]/input。请注意,xpath=/html/body/main/div/div/div[2]/form/div[1]/input是圆形按钮的定位器值。

Step 8 − Enter click in the Command field, xpath=/html/body/main/div/div/div[2]/form/div[1]/input in the Target field. Please note, xpath=/html/body/main/div/div/div[2]/form/div[1]/input is the locator value of the radio button.

Step 9 − 在Command字段中输入assert element present,xpath=// [@id="check"] in the Target field. Please note, xpath=// [@id="check"]是文本 - You have checked Yes 的定位器值。

Step 9 − Enter assert element present in the Command field, xpath=//[@id="check"] in the Target field. Please note, xpath=//[@id="check"] is the locator value of the text - You have checked Yes.

Step 10 − 在Command字段中输入close以关闭浏览器窗口。

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

selenium selenese commands 5

Step 11 − 单击出现在左侧窗格中的三个点,然后选择Rename选项。

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

Step 12 − 在Rename Test case字段中输入一个名称,例如Test9,然后单击Rename。输入的名称将出现在 Selenium IDE 的左侧。

Step 12 − Enter a name, say Test9 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.

selenium selenese commands 6

在上面的示例中,我们使用 open 命令启动了应用程序,并在日志中使用 store title 命令获取了页面标题 Selenium Practice - Radio Button 。我们使用 click 命令单击了单选按钮,并使用断言元素存在命令验证了文本 - 您选中了是。此外,我们在顶部测试用例名称 Test9 旁边得到了一个绿色勾号,表示通过了测试。

In the example above, we had launched the application with open command and obtained the page title Selenium Practice - Radio Button under the Log with the help of the store title command. We clicked on the radio button using the click command and verified the text - You have checked Yes with the help of the assert element present command. Besides, we got a green tick beside the test case name - Test9 at the top, signifying a passed test.

以下是 Selenium IDE - Selenese 命令教程上的全面总结。我们从描述一个 Selenese 命令开始,以及各种访问器、操作和断言命令,并提供了一个示例来演练如何将 Selenese 命令与 Selenium 结合使用。

This concludes our comprehensive take on the tutorial on Selenium IDE - Selenese Commands. We’ve started with describing a Selenese command, and various accessors, actions, and assertions commands and an example to walk through how to use the Selenese commands along with Selenium.

这使你掌握了 Selenium IDE 中 Selenese 命令的深入知识。明智的做法是不断练习你学到的内容,并探索其他人与 Selenium 相关的知识,以加深你的理解并扩展你的视野。

This equips you with in-depth knowledge of the Selenese commands 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.