Selenium 简明教程

Selenium IDE - Browser Execution & Command-line Runner

在 Selenium IDE 中开发的测试用例也应该能够在多个浏览器中运行,例如 Chrome、Firefox、Safari、Edge 等,同时对测试用例进行一些小小的修改。这有助于检查正在测试的应用程序是否符合所有浏览器中的要求。

The test case developed in Selenium IDE should be able to run in multiple browsers like Chrome, Firefox, Safari, Edge, and so on with minor updates to the test case. This helps to check if the application under test is working as per the requirements in all the browsers.

Why is Cross Browser Execution Beneficial?

通常在处理任何应用程序(例如电子商务或旅行预订等)的过程中,我们会注意到在付款或向购物车中添加产品时,应用程序在特定浏览器中花费太多时间加载页面。

Often on working on any applications for example, e-commerce or on travel reservations, and so on, we observe that while doing the payment, or adding products to cart, the applications are taking too much time for page load on a specific browser.

作为用户,我们可以立即推断出该应用程序可能存在错误或正在出现问题,然后我们转向具有类似产品和功能的其他公司网站。

As a user, we promptly infer that there may be an error or an ongoing issue in that application, and we move towards a different company website having the similar products and functionalities.

Prerequisites for Selenium IDE cross browser execution

  1. Install Selenium IDE in the local system in Chrome or Firefox. To get more information about its installation, please refer to the link Selenium IDE Installation.

  2. Install Node.js(version above 10) using the link https://nodejs.org/en/download/.

  3. Check Node.js installed successfully by running the command in the terminal: node -v. The version of Node.js installed should be returned. Along with the Node.js installation, the npm should be installed as well. Check npm has been installed by running the command in the terminal: npm -v. The version of npm installed should be returned.

  4. Install Selenium side runner, by running the command in the terminal: npm install -g selenium-side-runner.

How to run Selenium IDE tests in multiple browsers?

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 browser execution 1

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

Step 3 − Click on the Create a new project link, then enter a project name, say, Test7 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 browser execution 2

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 − 在命令字段中输入 type,在目标字段中输入 id=name,在值字段中输入 Selenium。请注意,id=name 是输入框的定位器值,Selenium 是要输入的值。

Step 6 − Enter type in the Command field, id=name in the Target field, and Selenium in the Value field. Please note, id=name is the locator value of the input box and Selenium is the value to be entered.

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

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

selenium browser execution 3

Step 8 − 单击左侧窗格中显示的三个圆点,然后选择重命名选项。

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

Step 9 − 在重命名测试用例字段下输入一个名称,例如 Test7,然后单击重命名。输入的名称将出现在 Selenium IDE 的左侧。

Step 9 − Enter a name, say Test7 under the Rename Test case field, then click on Rename. The entered name would appear on the left of Selenium IDE.

selenium browser execution 4

Step 10 - 从顶部单击“全部运行测试”,然后等待测试执行完成。在我们的示例中,我们会看到运行:1,失败:0 以及指示测试在未出现任何失败的情况下成功运行的绿色条形图。另外,日志下还会显示消息 Test7 已成功完成。

Step 10 − 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 Test7 completed successfully appeared under the Log.

selenium browser execution 5

在上面的示例中,我们使用日志下出现的 id 定位符在输入框中输入文本 Selenium 。此外,我们还可以在顶部的测试用例名称 - Test7 旁获得一个绿色对勾,表示测试已通过。所有这些都使用 open、type 和 close 等操作命令成功执行。

In the example above, we entered the text Selenium in an input box using the id locator which appeared under the Log. Besides, we got a green tick beside the test case name - Test7 at the top, signifying a passed test. All these were performed successfully using the actions commands like open, type, and close.

除此之外,所有测试步骤以及测试用例名称全部变为绿色,这表示所有步骤都已通过。日志选项卡下显示的所有步骤都会出现绿色的 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

Step 11 - 单击“保存项目”,该项目将使用 Test7.side 文件保存。

Step 11 − Click on Save Project, and this project would be saved with Test7.side file.

selenium browser execution 6

Step 12 - 要在 Chrome 浏览器中运行此测试,我们需要运行如下命令:npm install -g chromedriver。类似地,要在 Firefox 中运行测试,我们需要运行如下命令:npm install -g geckodriver。要在 Edge 中运行测试,我们需要运行如下命令:npm install -g edgedriver。要在 Internet Explorer 中运行测试,我们需要运行如下命令:npm install -g iedriver。

Step 12 − To run this test in the Chrome browser, we would need to run the command: npm install -g chromedriver. Similarly, for running the test in Firefox, we would need to run the command: npm install -g geckodriver. For running the test in Edge, we would need to run the command: npm install -g edgedriver. For running the test in Internet Explorer, we would need to run the command: npm install -g iedriver.

Step 13 - 要启动 Selenium Side Runner 并运行我们在步骤 11 中刚刚保存的测试,我们需要运行如下命令:selenium-side-runner /path/to/<project name>.side。

Step 13 − To launch the Selenium Side Runner, and run the test we had just saved in Step11, we would need to run the command: selenium-side-runner /path/to/<project name>.side.

Browser Capabilities for Different Browser Execution

要添加 Chrome 浏览器的功能,用于本地执行,命令如下:

To add Chrome browser capabilities for local execution, the command would be −

selenium-side-runner -c "browserName=chrome".

要添加 Safari 浏览器的功能,用于本地执行,命令如下:

To add Safari browser capabilities for local execution, the command would be −

selenium-side-runner -c "browserName=safari".

要添加 Edge 浏览器的功能,用于本地执行,命令如下:

To add Edge browser capabilities for local execution, the command would be −

selenium-side-runner -c "browserName=edge".

要添加 Internet Explorer 浏览器的功能,用于本地执行,命令如下:

To add Internet Explorer browser capabilities for local execution, the command would be −

selenium-side-runner -c "browserName=internet explorer".

要添加 Internet Firefox 浏览器的功能,用于本地执行,命令如下:

To add Internet Firefox browser capabilities for local execution, the command would be −

selenium-side-runner -c "browserName=firefox".

Conclusion

至此,我们关于 Selenium IDE 浏览器执行和命令行执行器的教程全面总结完毕。我们首先说明了浏览器执行的含义、为何跨执行有益、在跨浏览器中运行 Selenium IDE 测试的先决条件、如何在不同浏览器中运行测试以及如何在不同浏览器中执行时添加浏览器功能。这让你对 Selenium IDE 中的浏览器执行和命令行执行器有了深入了解。明智的做法是继续实践你所学到的知识,探索其他与 Selenium 相关的内容,加深理解,拓宽视野。

This concludes our comprehensive take on the tutorial on Selenium IDE Browser Execution & Command-line Runner. We’ve started with describing the meaning of browser execution, why cross execution is beneficial, prerequisites to run Selenium IDE tests in cross browsers, how to run them in different browsers, and how to add browser capabilities while executing them in different browsers. This equips you with in-depth knowledge of the browser execution and command line runner 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.