Selenium 简明教程

Selenium Grid - Create Test Script

Selenium Grid 由六个组件组成,即路由器、分配器、节点、会话队列、会话映射和事件总线,这使得能够以多种方式部署它。根据要求,我们可以在分布式模式下单独启动 Selenium Grid 的各个组件,将它们在中心和节点模式下隔离为中心和节点,并在独立模式下一次启动所有组件。

Selenium Grid consists of six components namely, the Router, Distributor, Nodes, Session Queue, Session Map, and Event Bus which enables the option to deploy it in various ways. Depending on the requirements, we can begin each of the components of the Selenium Grid individually in a Distributed mode, segregate them as Hub and Node in the Hub and Node mode, and all at once in a Standalone mode.

Standalone Mode in Selenium Grid

在独立模式下,Selenium Grid 的每个组件作为一个单元工作,并且可以使用单个命令通过单个进程触发。这是运行 Selenium Grid 的最便捷方式。服务器连接到 http://localhost:4444 上的 RemoteWebdriver 请求,并自动从系统路径中找到所有驱动程序。在独立模式下触发 Selenium Grid 后,所有测试都应使用 http://localhost:4444

In a Standalone Mode, every component of Selenium Grid works as a single unit and can be triggered with a single command using a single process. It is the most convenient way to run the Selenium Grid. The server connects with the RemoteWebdriver requests on http://localhost:4444 and locates all the drivers from the System path automatically. Post triggering the Selenium Grid in Standalone mode, all the tests should use http://localhost:4444.

Benefits of Standalone Mode in Selenium Grid

Selenium Grid 中独立模式的好处如下:

The benefits of Standalone Mode in Selenium Grid are listed below −

  1. Create and troubleshoot tests built using the RemoteWebdriver locally.

  2. Executing and unit testing quickly prior to pushing the code.

  3. Can be easily configured with CI/CD tools.

Hub and Node Mode in Selenium Grid

在 Selenium Grid 中,中心包括路由器、会话映射、会话队列和事件总线等组件。服务器自动连接到 http://localhost:4444 上的 RemoteWebdriver 请求。

In Selenium Grid, the Hub comprises the components namely the Router, Session Map, Session Queue, and the Event Bus. The server connects with the RemoteWebdriver requests on http://localhost:4444 automatically.

该节点在起点期间从系统路径找到所有驱动程序。多个节点可以在同一台机器上执行。例如,如果有两个节点 A 和 B 在同一台机器上运行,则使用以下命令:

The Node locates all the drivers from the System path during the starting point. Multiple Nodes can execute in the same machine. For example, if there are two Nodes, A and B running in the same machine, using the below commands −

For Node A,

For Node A,

java -jar selenium-server-<version>.jar node --port 5555

For Node B,

For Node B,

java -jar selenium-server-<version>.jar node --port 6666

中心和节点可以定位在不同的机器上,它们通过 HTTP 和事件总线进行通信。该节点使用事件总线向中心发送消息进行注册。中心收到消息后,会使用 HTTP 扩展到节点以检查其是否存在。

The Hub and Node can be positioned on different machines where they communicate via the HTTP and the Event Bus. The Node directs messages to the Hub using the Event Bus for registration. Once the Hub gets the message, it extends to the Node using the HTTP to check its presence.

要正确地将节点注册到中心,事件总线端口(默认端口:4442 和 4443)需要在中心计算机和节点上可用。这有助于建立中心和节点之间的连接。如果中心正在使用默认端口,则可以使用 --hub 标志来注册节点,如以下命令所示。

For registering a Node properly to the Hub, the Event Bus ports(default ports: 4442 and 4443) need to be available on the Hub machine and to the Nodes. This helps to establish the connection between the Hub and the Node. In case the Hub is utilizing the default ports, the --hub flag is used to register the Node as shown in the below command.

java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444

如果中心没有使用默认端口,则使用标志 --publish-events 和 --subscribe-events,如下面的命令所示。

If the Hub is not utilizing the default ports, the flags --publish-events and --subscribe-events are used as shown in the below command.

java -jar selenium-server-<version>.jar hub
   --publish-events tcp://<hub-ip>:8886
   --subscribe-events tcp://<hub-ip>:8887 --port 8888

在上例中,中心正在使用端口 8886、8887 和 8888。在注册中心后,节点将使用相同的端口进行成功注册,使用与中心相同的端口,如下面命令所示。

In the above example, the Hub is using the ports 8886, 8887, and 8888. After the Hub is registered, the Node uses the same ports for successful registration using the same Hub ports as shown in the below command.

java -jar selenium-server-<version>.jar node
   --publish-events tcp://<hub-ip>:8886
   --subscribe-events tcp://<hub-ip>:8887

Benefits of Hub and Node mode in Selenium Grid

在 Selenium Grid 中,已观察到中心和节点模式是最常用的模式。Selenium Grid 中中心和节点模式的优势如下:

In Selenium Grid, it is observed that the Hub and Node mode is the most commonly used mode. The benefits of Hub and Node Mode in Selenium Grid are listed below −

  1. The Hub and Node mode allows integration of various machines having different platforms, browsers, and their versions in the Selenium Grid.

  2. The Hub and Node mode runs by pointing to one entry to execute the WebDriver tests in various environments.

  3. The tests can be scaled up and down easily without impacting the complete Grid.

Distributed Mode in Selenium Grid

在分布式模式下,Selenium Grid 的每个组件都应理想地位于不同的计算机上,并且它们需要分别启动。必须正确使用每个端口才能使所有组件之间正确交互。

In a Distributed mode, each component of the Selenium Grid should be ideally positioned at different machines and they need to be kicked off separately. It is mandatory to utilize every port correctly to have correct interaction among all components.

从事件总线开始允许网格组件之间的通信。事件总线的默认端口为 4442、4443 和 5557。

To start with the Event Bus allows communication among Grid components. The default ports for the Event Bus are 4442, 4443, and 5557.

java -jar selenium-server-<version>.jar event-bus
   --publish-events tcp://<event-bus-ip>:4442 --subscribe-events
   tcp://<event-bus-ip>:4443 --port 5557

然后,新会话队列将新会话附加到队列中,该队列将由分发器获取。新会话队列的默认端口为 5559。

Then the New Session Queue appends a new session to the queue which will be obtained by the Distributor. The default ports for the New Session Queue is 5559.

java -jar selenium-server-<version>.jar sessionqueue --port 5559

会话图将会话 ID 连接到会话正在执行的节点,并与事件总线通信。新会话队列的默认端口为 5556。

The Session Map connects the session id to the Node where the session is executing and also communicates with the Event Bus. The default port for the New Session Queue is 5556.

java -jar selenium-server-<version>.jar sessions
   --publish-events tcp://<event-bus-ip>:4442
   --subscribe-events tcp://<event-bus-ip>:4443 --port 5556

分发器获取新会话队列以进行新会话请求到节点,前提是它们的功能类似。节点以类似于在中心和节点模式下注册节点的方式注册到分发器。分发器的默认端口为 5553。分发器与新会话队列、会话图、事件总线和节点进行通信。

The Distributor obtains the New Session Queue for new session requests to the Node provided their capabilities are similar. The Nodes are registered to the Distributor in the similar way a Node is registered in a Hub and Node mode. The default port for the Distributor is 5553. The Distributor communicates with the New Session Queue, Session Map, Event Bus, and the Nodes.

java -jar selenium-server-<version>.jar distributor
   --publish-events tcp://<event-bus-ip>:4442 --subscribe-events
   tcp://<event-bus-ip>:4443 --sessions
   http://<sessions-ip>:5556 --sessionqueue
   http://<new-session-queue-ip>:5559 --port 5553 --bind-bus false

路由器将新会话请求更改为队列,并将正在运行的会话请求转发到正在运行会话的节点。路由器的默认端口为 4444。路由器与新会话队列、会话图和分发器进行通信。

The Router changes the new session requests to the queue, and forwards the running session requests to the Node which is running the session. The default port for the Router is 4444. The Router communicates with the New Session Queue, Session Map, and the Distributor.

java -jar selenium-server-<version>.jar router --sessions
   http://<sessions-ip>:5556 --distributor
   http://<distributor-ip>:5553 --sessionqueue
   http://<new-session-queue-ip>:5559 --port 4444

节点的默认端口为 5555。

The default port for Node is 5555.

java -jar selenium-server-<version>.jar node
   --publish-events tcp://<event-bus-ip>:4442
   --subscribe-events tcp://<event-bus-ip>:4443

Benefits of Distributed Mode in Selenium Grid

Selenium Grid 的分布式模式用于在大型网格中需要设置大量节点时(只有一个中心和跨多台计算机的许多节点)。在这种情况下,中心和节点模式不是理想的选择。

The Distributed mode of Selenium Grid is used when there is a requirement to set up a large number of Nodes within a big size Grid(where there is only one Hub and so many Nodes across multiple machines). In such a situation, Hub and Node mode is not an ideal choice.

Prerequisites to Create a Test Script

Step 1 − 在系统中安装 Java(版本高于 8),并使用以下命令检查它是否存在:java -version。如果安装成功完成,将显示已安装的 java 版本。

Step 1 − Install Java(version above 8) in the system and check if it is present with the command: java -version. The java version installed will be visible if installation has been completed successfully.

Step 2 − 通过打开浏览器并输入来检查网格状态 −

Step 2 − Check Grid status by opening a browser and entering −

  1. For the UI version, type http://localhost:4444.

  2. For Non UI version, type http://localhost:4444/status.

对于这两种情况,我们都会收到错误 - 无法使用此网站。因为 Selenium Grid 尚未启动。

For both the cases, we would get the error - This site can’t be reached. Since Selenium Grid has not been started yet.

selenium grid create test script 1

Create a Test Script in Selenium Grid

在独立模式下创建测试脚本的步骤如下:

The steps to create a test script in a Standalone mode are listed below −

Step 1 - 从下面的链接下载 Selenium Standalone Jar 并将其保存在文件夹中 -

Step 1 − Download Selenium Standalone Jar from the below link and save it in a folder −

Step 2 - 从已存储 Selenium Standalone Jar 的文件夹位置,从终端运行以下命令 -

Step 2 − From the location of the folder where the Selenium Standalone Jar had been stored, run the below command from the terminal −

java -jar selenium-server-<version>.jar standalone.

Step 3 - 再次通过打开浏览器并输入: http://localhost:4444 检查网格状态。

Step 3 − Check Grid status again by opening a browser and entering: http://localhost:4444.

The Error - 将不再有“此网站无法访问”的显示,并且我们会获得显示不同浏览器的 Grid 状态。这将证明 Selenium Grid 已在 Standalone 模式下触发。

The Error − This site can’t be reached would no longer be there, and we would get Grid status showing different browsers. This would prove Selenium Grid had been triggered in the Standalone mode.

selenium grid create test script 2

Step 4 -

Step 4

Base.java 中实现代码

Code Implementation in Base.java

package BaseClass;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Base {
   public WebDriver setBrowser(String browserName) throws MalformedURLException {
      WebDriver driver = null;
      DesiredCapabilities dc = new DesiredCapabilities();

      if(browserName.equalsIgnoreCase("chrome")) {
         dc.setBrowserName("chrome");
      } else if(browserName.equalsIgnoreCase("edge")) {
         dc.setBrowserName("MicrosoftEdge");
      }

      // Initiate RemoteWebDriver
      driver = new RemoteWebDriver(new URL("http://localhost:4444"),dc);
      return driver;
   }
}

TestOne.java 中实现代码

Code Implementation in TestOne.java

package Grid;

import BaseClass.Base;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;

public class TestOne extends Base {
   public WebDriver driver = null;
   @Test
   public void testOne() {

      // launch application
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");

      // get page title
      System.out.println("Page title is: " + driver.getTitle() + " obtained from testOne");
   }

   @BeforeMethod
   public void setup() throws MalformedURLException {
      driver = setBrowser("chrome");
   }

   @AfterMethod
   public void tearDown() {

      // quitting browser
      driver.quit();
   }
}

TestTwo.java 中的代码实现

Code Implementation in TestTwo.java

package Grid;

import BaseClass.Base;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;

public class TestTwo extends Base {
   public WebDriver driver = null;
   @Test
   public void testTwo() {

      // launch application
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");

      // get page title
      System.out.println("Page title is: " + driver.getTitle() + " obtained from testTwo");
   }
   @BeforeMethod
   public void setup() throws MalformedURLException {
      driver = setBrowser("edge");
   }
   @AfterMethod
   public void tearDown() {

      // quitting browser
      driver.quit();
   }
}

testng.xml 文件中的配置。

Configurations in testng.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Grid Test">
   <test thread-count = "5" name="Test">
      <classes>
         <class name="Grid.TestOne" />
         <class name="Grid.TestTwo"/>
      </classes>
   </test>
</suite>

Step 5 - 从 testng.xml 文件中运行测试。

Step 5 − Run the test from the testng.xml file.

它将显示以下内容 output

It will show the following output

Page title is: Selenium Practice - Links obtained from testOne
Page title is: Selenium Practice - Links obtained from testTwo

===============================================
Grid Test
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

在上面的示例中,我们已经配置了 Selenium Grid 的 Standalone 模式。

In the above example, we had configured the Standalone mode of the Selenium Grid.

这结束了我们对 Selenium Grid - 创建测试脚本教程的全面介绍。我们从描述 Selenium Grid 中的独立、中心和节点以及分布式模式及其优点、创建测试脚本的先决条件以及如何在 Selenium Grid 中创建测试脚本开始。

This concludes our comprehensive take on the tutorial on Selenium Grid - Create Test Script. We’ve started with describing Standalone, Hub and Node, and Distributed modes in Selenium Grid and their benefits, prerequisites to create a test script, and how to create a test script in Selenium Grid.

这使您对 Selenium Grid - 创建测试脚本有了深入的了解。明智的做法是继续实践你所学到的知识并探索与硒相关的其他知识,以加深你的理解并扩展你的视野。

This equips you with in-depth knowledge of the Selenium Grid - Create Test Script. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.