Selenium 简明教程
Selenium Grid - Configuration
最新版本的 Selenium Grid 与早期版本的 Selenium Grid 有很多不同之处。早期版本的 Selenium Grid 仅具有一种模式,即 Hub 和 Node,而最新版本的 Selenium Grid 支持三种模式:Standalone、Hub 和 Node 以及 Distributed。
The latest version of Selenium Grid has a lot of differences from the older versions of Selenium Grid. The older version of Selenium Grid had only one mode - Hub and Node, while the latest version of Selenium Grid supports three modes - Standalone, Hub and Node, and Distributed.
以前版本的 Selenium Grid 仅包含两种模式 - Hub 和 Node,而最新版本的 Selenium Grid 具有六个组件,如 Router、Distributor、Nodes、Session Queue、Session Map 和 Event Bus。
The previous versions of Selenium Grid consisted of only two modes - Hub and Node, while the latest version of Selenium Grid has six components like the Router, Distributor, Nodes, Session Queue, Session Map, and Event Bus.
Prerequisites to Configure the Selenium Grid
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 −
-
For the UI version, type http://localhost:4444.
-
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.
Standalone Mode Configuration in Selenium Grid
在 Selenium Grid 的最新版本中,Hub 和 Node 不用分别启动。因此,可以在同一台计算机内同时触发 Hub 和 Node。通过单一的 Standalone 命令,Hub 和 Node 将同时启动。因此,Standalone 有助于节省资源和时间。
In the latest version of Selenium Grid, Hub and Node need not be launched separately. Thus the Hub and Node can be triggered at the same time within the same machine. With a single Standalone command, both the Hub and the Node will be launched at one time. Thus the Standalone helps to save resources and time.
以下是配置 Standalone 模式的步骤 -
The steps to configure 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 ,再次检查 Grid 状态。
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.
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();
}
}
在 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 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.
Hub and Node Mode Configuration in Selenium Grid
在 Selenium Grid 的最新版本中,Hub 和 Node 模式需要在不同的计算机中触发 Hub 和 Node。Hub 包含路由器、分发器、会话映射、新会话队列和事件总线等组件。
In the latest version of Selenium Grid, Hub and Node mode need Hub and Node to be triggered in separate machines. A Hub comprises the components like the Router, Distributor, Session Map, New Session Queue, and Event Bus.
以下是配置 Hub 和 Node 模式的步骤 -
The steps to configure Hub and Node 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 hub.
Please Note - 在注册 hub 时从命令行日志中获得的 hub IP 地址。
Please Note − The hub IP Address in the command line logs received while registering the hub.
Step 3 - 通过打开浏览器并输入 http://localhost:4444 ,检查 Grid 状态。
Step 3 − Check Grid status by opening a browser and entering http://localhost:4444.
The Error - 将不再有“此网站无法访问”的显示,并且我们会获得显示消息的 Grid 状态 - Grid 还没有注册的 Node。这是因为尚未注册 Node。
The Error − This site can’t be reached would no longer be there, and we would get Grid status showing the message - The Grid has no registered Nodes yet. This was because Node had not been registered yet.
Run Node in Same Machine
按照步骤 1、2 和 3 配置 Hub 后,导航到已存储 Selenium Standalone Jar 的文件夹位置,并通过打开另一个新的终端窗口运行以下命令 -
After configuring Hub by following the above steps 1,2, and 3, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window −
java -jar selenium-server-<version>.jar node.
这将有助于在同一台计算机中启动节点。
This would help to launch the Node in the same machine.
通过打开浏览器并输入 http://localhost:4444 来再次检查 Grid 状态。
Check Grid again status by opening a browser and entering http://localhost:4444.
The Message Showing - Grid 尚未注册任何节点,而不会在那里,取而代之的是,该节点将反映。
The Message Showing − The Grid has no registered Nodes yet would not be there, in place of that, the Node would reflect.
Run Node in Different Machine
Step 1 - 在另一台计算机中,从以下链接下载 Selenium Standalone Jar 并将其保存在文件夹中 -
Step 1 − In another machine download Selenium Standalone Jar from the below link and save it in a folder −
Step 2 - 导航到已存储 Selenium Standalone Jar 的文件夹位置,然后通过打开终端窗口运行以下命令 -
Step 2 − Navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening the terminal window −
java -jar selenium-server-<version>.jar node
--detect-drivers true --publish-events tcp://{hub IP Address}:4442
--subscribe-events tcp://{hub IP Address}:4443.
Please Note - 在第 3 步中获得的注册中心时收到的命令行日志中可以获得注册中心的 IP 地址。
Please Note − The hub IP Address would be available in the command line logs received while registering the hub obtained in Step 3.
这将有助于在另一台计算机中启动节点。
This would help to launch the Node in a different machine.
Step 3 - 通过打开浏览器并输入 http://localhost:4444 来再次检查 Grid 状态。
Step 3 − Check Grid again status by opening a browser and entering http://localhost:4444.
The Message Showing - Grid 尚未注册任何节点,而不会在那里,取而代之的是,该节点将反映。
The Message Showing − The Grid has no registered Nodes yet would not be there, in place of that, the Node would reflect.
Step 4 - 从 testng.xml 文件中运行为独立模式编写的同一测试。
Step 4 − Run the same test that was written for the Standalone mode 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 的 Hub 和 Node 模式。
In the above example, we had configured the Hub and Node mode of the Selenium Grid.
Distributed Configuration in Selenium Grid
Selenium Grid 的分布式模式用于在有大量节点设置大型 Grid 时使用,在此只有一个中心节点和多台计算机上的许多节点。在这样的情况下,中心模式与节点模式不是理想选择。
The Distributed mode of Selenium Grid is used when there is a large number of Nodes to set up 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.
另外,在分布式模式中,每个组件(路由器、分配器、会话映射、新会话队列和事件总线)都可以借助不同的命令单独启动。
Also, in a Distributed mode, every component - Router, Distributor, Session Map, New Session Queue, and Event Bus can be started individually with the help of different commands.
配置分布式模式的步骤如下 -
The steps to configure Distributed 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 to kick off the Event Bus −
java -jar selenium-server-<version>.jar event-bus.
请注意事件总线在终端中命令行日志中启动的 IP 地址。
Please note the IP Address where the Event bus had been started in the command line logs in the terminal.
Step 3 - 通过执行步骤 2 配置事件总线后,导航到已存储 Selenium Standalone Jar 的文件夹位置,然后通过打开另一个新终端窗口运行以下命令以启动会话映射 -
Step 3 − After configuring Event Bus by following the Step 2, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window to kick off the Session Map −
java -jar selenium-server-<version>.jar sessions.
请注意会话映射在终端中命令行日志中启动的 IP 地址。
Please note the IP Address where the Session Map had been started in the command line logs in the terminal.
Step 4 - 通过执行步骤 3 配置会话映射后,导航到已存储 Selenium Standalone Jar 的文件夹位置,然后通过打开另一个终端窗口运行以下命令以启动会话队列 -
Step 4 − After configuring Session Map by following the Step 3, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening new another terminal window to kick off the Session Queue −
java -jar selenium-server-<version>.jar sessionqueue.
请注意:终端的命令行日志中已启动会话队列的 IP 地址。
Please note the IP Address where the Session Queue had been started in the command line logs in the terminal.
Step 5 - 按照步骤 4 配置会话队列后,导航到存储 Selenium Standalone Jar 的文件夹位置,并通过打开另一个新终端窗口运行以下命令以启动分发器:
Step 5 − After configuring Session Queue by following the Step 4, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window to kick off the Distributor −
java -jar selenium-server-<version>.jar distributor
--sessions http://{IP Address of Session Map}:5556
--sessionqueue http://{IP Address of Session Queue}:5559 --bind-bus false.
请注意,终端的命令行日志中已启动分发器的 IP 地址。
Please note the IP Address where the Distributor had been started in the command line logs in the terminal.
Step 6 - 按照步骤 5 配置分发器后,导航到存储 Selenium Standalone Jar 的文件夹位置,并通过打开另一个新终端窗口运行以下命令以启动路由器:
Step 6 − After configuring the Distributor by following the Step 5, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening another new terminal window to kick off the Router −
java -jar selenium-server-<version>.jar router
--sessions http://{IP Address of Session Map}:5556
--distributor http://{IP Address of Distributor}:5553
--sessionqueue http://{IP Address of Session Queue}:5559.
请注意,终端的命令行日志中已启动路由器的 IP 地址。
Please note the IP Address where the Router had been started in the command line logs in the terminal.
按照步骤 6 配置路由器后,导航到存储 Selenium Standalone Jar 的文件夹位置,并通过打开一个新终端窗口运行以下命令:
After configuring the Router by following the Step 6, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening new another terminal window −
java -jar selenium-server-<version>.jar node.
这将有助于在同一台计算机中启动节点。
This would help to launch the Node in the same machine.
Run Node in Different Machine
按照步骤 6 配置路由器后,导航到存储 Selenium Standalone Jar 的文件夹位置,并通过打开一个新终端窗口运行以下命令:
After configuring the Router by following the Step 6, navigate to the location of the folder where the Selenium Standalone Jar had been stored, and run the below command by opening a new terminal window −
java -jar selenium-server-<version>.jar node
--detect-drivers true --publish-events tcp://{hub IP Address}:4442
--subscribe-events tcp://{hub IP Address}:4443.
我们在 Hub 和节点配置期间注册 Hub 时使用了接收的 Hub IP 地址。
We had used the hub IP Address received while registering the hub during Hub and Node Configurations.
这将有助于在另一台计算机中启动节点。
This would help to launch the Node in a different machine.
这结束了我们对 Selenium Grid - 配置教程的全面介绍。我们从描述配置 Selenium Grid 的先决条件开始,并逐步介绍了 Selenium Grid 中的 Standalone、Hub 和节点以及分布式模式配置步骤。
This concludes our comprehensive take on the tutorial on Selenium Grid - Configuration. We’ve started with describing prerequisites to configure the Selenium Grid, and walked through steps for Standalone, Hub and Node, and Distributed modes configuration in Selenium Grid.
这使你深入了解 Selenium Grid 配置。明智的做法是不断实践你所学的内容,并探索与 Selenium 相关的其他内容,以加深理解并拓展视野。
This equips you with in-depth knowledge of the Selenium Grid Configuration. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.