Selenium 简明教程

Selenium IDE - Alerts & Popups

Selenium IDE 可用于处理警报和弹出窗口。网页上的警报旨在显示警告消息或信息,或获取用户的授权以执行进一步的操作。

Selenium IDE can be used to handle alerts and popups. An alert on a web page is designed to show a warning message, or information, or to get the user authorization to proceed for further actions.

Selenium IDE Commands for Alerts and Popups

警报和弹出窗口的各种命令如下:

The various commands for alerts and popups are listed below −

  1. Webdriver answer on visible prompt − This command is used to input text in the prompt and then click on the OK button inside the prompt.

  2. Webdriver choose ok on visible confirmation − This command is used to click on the OK button within a confirmation dialogue box.

  3. Webdriver choose cancel on visible confirmation − This command is used to click on the Cancel button within a confirmation dialogue box.

  4. Webdriver choose cancel on visible prompt − This command is used to cancel the prompt.

  5. Answer on next prompt − This command is used to plan and answer the prompt before it is displayed.

  6. Choose cancel on next prompt − This command is used to plan, then choose and click on the Cancel button on the prompt before it is displayed.

  7. Choose ok on next confirmation − This command is used to plan, then choose and click on the OK button on the confirmation dialogue box before it is displayed.

  8. Assert prompt(text) − This command is used to verify if a prompt has been rendered. If assert did not pass, then the execution will stop at the point of mismatch.

  9. Assert alert(alert text) − This command is used to verify if an alert has the text matching the alert text. If assert did not pass, then the execution will stop at the point of mismatch.

  10. Assert confirmation(text) − This command is used to verify if a confirmation has been rendered. If assert did not pass, then the execution will stop at the point of mismatch.

Example 1

我们以以下页面的示例来说明,点击最后一个 Click Me 按钮时,我们将获得提示,然后我们将输入文本 Tutorialspoint ,最后使用 Webdriver 在可见提示命令中单击“确定”按钮。

Let us take an example of the below page, where on clicking the last Click Me button we would get the prompt, and we would input the text Tutorialspoint and finally click on the OK button using the webdriver answer on visible prompt command.

selenium ide alerts popups 1

然后,我们将点击 Alert 按钮,该按钮将生成一条文本为 - Hello World! 的警报。我们将使用断言警报命令验证警报文本。

Then we would click on the Alert button, which would generate an alert with text - Hello World!. We would verify alert text with the assert alert command.

selenium ide alerts popups 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.

Step 3 − 单击创建新项目链接,然后输入项目名称,如 Please provide a name for your new project 下的 Test12。最后,我们将点击确定按钮。

Step 3 − Click on the Create a new project link, then enter a project name, say, Test12 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,并在“目标”字段中输入 [role="bare" [role="bare"]https://www.tutorialspoint.com/selenium/ ],以启动应用程序。

Step 5 − Enter open in the Command field, and [role="bare"https://www.tutorialspoint.com/selenium/] in the Target field, to launch the application.

Step 6 - 在“命令”字段中输入 answer on next prompt,并在“值”字段中输入 Tutorialspoint。请注意,Tutorialspoint 是在可见提示中输入的文本。

Step 6 − Enter answer on next prompt in the Command field, and Tutorialspoint in the Value field. Please note, Tutorialspoint is the text entered in the visible prompt.

Step 7 - 在“命令”字段中输入 click,在“目标”字段中输入 xpath=/html/body/main/div/div/div[2]/div[4]/button。请注意,xpath=/html/body/main/div/div/div[2]/div[4]/button 是“单击我”按钮的位置值。

Step 7 − Enter click in the Command field, xpath=/html/body/main/div/div/div[2]/div[4]/button in the Target field. Please note, xpath=/html/body/main/div/div/div[2]/div[4]/button is the locator value of the Click Me button.

Step 8 - 在“命令”字段中输入 click,在“目标”字段中输入 xpath=/html/body/main/div/div/div[2]/div[1]/button。请注意,xpath=/html/body/main/div/div/div[2]/div[1]/button 是警报按钮的位置值。

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

Step 9 - 在“命令”字段中输入 assert alert,并在“值”字段中输入 Hello World!。请注意,Hello World! 是要验证的警报文本。

Step 9 − Enter assert alert in the Command field, and Hello World! in the Value field. Please note, Hello World! is the alert text being verified.

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

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

selenium ide alerts popups 3

Step 11 - 从顶部单击“运行所有测试”,并等待测试执行完成。在我们的示例中,我们会看到“运行:1,失败:0”,还有一个绿色条表示测试成功运行,没有任何失败。而且,成功完成的消息出现在日志下面。

Step 11 − 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 ide alerts popups 4

在上面的示例中,我们使用 open 命令启动了应用程序并输入了文本 - Tutorialspoint ,还验证了警报文本 - Hello World! 。此外,我们在顶部得到了一个绿色勾号,表示测试通过。

In the example above, we had launched the application with open command and entered the text - Tutorialspoint on the prompt also verified the alert text - Hello World!. Besides, we got a green tick at the top, signifying a passed test.

Example 2

我们以以下页面的示例来说明,点击第三个 Click Me 按钮,我们将获得确认对话框,然后我们将使用在下一个确认中选择确定命令单击“确定”按钮。

Let us take an example of the below page, where on clicking the third Click Me button we would get the confirmation dialogue box, and we would click on the OK button using the choose ok on next confirmation command.

selenium ide alerts popups 5

确认对话框关闭后,我们将在网页上获得文本 - You pressed OK!

Once the confirmation dialogue box is closed, we would get the text - You pressed OK! on the web page.

selenium ide alerts popups 6

要遵循的步骤如下所示 -

The steps to be followed are listed below −

Step 1 - 按照示例 1 中提到的步骤 1 至 5 执行操作。

Step 1 − Follow steps 1 to 5 as mentioned in Example 1.

Step 2 − 输入选择,在命令字段的下一个确认中确认。

Step 2 − Enter choose ok on next confirmation in the Command field.

Step 3 − 在命令字段中输入点击,

Step 3 − Enter click in the Command field,

Target 字段中的 xpath=/html/body/main/div/div/div[2]/div[3]/button。请注意,xpath=/html/body/main/div/div/div[2]/div[3]/button 是点击我的按钮的定位器值。

xpath=/html/body/main/div/div/div[2]/div[3]/button in the Target field. Please note, xpath=/html/body/main/div/div/div[2]/div[3]/button is the locator value of the Click Me button.

Step 4 − 在命令字段中输入断言文本,Target 字段中的 xpath=//*[@id="desk"],Value 字段中为 You pressed OK!

Step 4 − Enter assert text in the Command field, xpath=//*[@id="desk"] in the Target field, and You pressed OK! in the Value field.

Step 5 − 输入 close,在命令字段中关闭浏览器窗口。

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

selenium ide alerts popups 7

Step 6 − 从顶部点击运行全部测试,然后等待测试执行完成。在我们示例中,我们将看到运行:1、失败:0,绿色栏表示测试在没有任何失败的情况下成功运行。此外,成功完成的消息出现在日志下。

Step 6 − 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 ide alerts popups 8

在上面的示例中,我们已使用 open 命令启动应用程序并点击确认对话框上的确定按钮。此外,我们还得到了顶部的绿色勾号,表示测试通过。

In the example above, we had launched the application with an open command and clicked on the OK button on the confirmation dialogue. Besides, we got a green tick at the top, signifying a passed test.

这总结了我们对 Selenium IDE - 警报和弹出式窗口教程的全面介绍。我们从描述用于处理警报和弹出式窗口的各种命令开始,并通过示例演示如何将这些命令与 Selenium 一起使用。

This concludes our comprehensive take on the tutorial on Selenium IDE - Alerts & Popups. We’ve started with describing various commands to handle alerts and popups and examples to walk through how to use these commands along with Selenium.

这使您深入了解 Selenium IDE 中的警报和弹出式窗口。明智的做法是继续实践您学到的知识,并探索与 Selenium 相关的其他知识,以加深您的理解并拓宽您的视野。

This equips you with in-depth knowledge of the Alerts & Popups 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.