Selenium 简明教程

Selenium Grid - Test Execution

最新版本的 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.

Selenium Grid 是一种将测试分布在多台物理机或虚拟机上以使我们可以并行(同时)执行脚本的工具。它极大地加速了测试过程,并且可在多个浏览器和平台上运行测试,并向我们提供有关产品的快速且准确的反馈。

Selenium Grid is a tool that distributes the tests across multiple physical or virtual machines so that we can execute scripts in parallel (simultaneously). It dramatically accelerates the testing process, and runs the tests across multiple browsers and platforms and gives us quick and accurate feedback on the product.

Selenium Grid 允许我们执行 WebDriver 的多个实例,或者 Selenium Remote Control 允许执行使用相同代码库的测试,因此代码不必存在于它们执行的系统上。 selenium-server-standalone 程序包包括 Hub、WebDriver 和 Selenium RC 来执行 grid 中的脚本。

Selenium Grid allows us to execute multiple instances of WebDriver or Selenium Remote Control allows execution of tests in parallel which use the same code base, hence the code need not be present on the system they execute. The selenium-server-standalone package includes Hub, WebDriver, and Selenium RC to execute the scripts in grid.

Prerequisites For Test Execution in 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 − 通过打开浏览器并输入来检查 Grid 状态:

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

Test Execution in Selenium Grid

在 Hub 和 Node 模式下在 Selenium Grid 中设置测试执行的步骤如下 −

The steps to set up test execution in Selenium Grid in a Hub and Node mode are listed below −

Step 1 − 从以下链接下载 Selenium Standalone Jar 并将其保存在文件夹中 − https://github.com/SeleniumHQ/selenium/releases

Step 1 − Download Selenium Standalone Jar from the below link and save it in a folder − https://github.com/SeleniumHQ/selenium/releases.

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.

在下面的图像中,它显示了 Hub 的成功触发。

In the below image, it shows the successful triggering of the Hub.

selenium grid test execution 2

Step 3 − 通过打开浏览器并输入来检查 Grid 状态: http://localhost:4444

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.

selenium grid test execution 3

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.

Run Node in Different Machine

Step 1 − 在另一台计算机上从以下链接下载 Selenium Standalone Jar 并将其保存在文件夹中 − https://github.com/SeleniumHQ/selenium/releases

Step 1 − In another machine download Selenium Standalone Jar from the below link and save it in a folder − https://github.com/SeleniumHQ/selenium/releases.

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.

在下面的图像中,它显示了从同一台计算机成功注册 Node。

In the below image, it shows the successful registration of a Node from the same machine.

selenium grid test execution 4

Step 3 − 通过打开浏览器并输入再次检查 Grid 状态: http://localhost:4444

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.

selenium grid test execution 5

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 的 Hub 和 Node 模式执行了测试。

In the above example, we had performed the test execution using the Hub and Node mode of the Selenium Grid.

这结束了我们对 Selenium Grid - 测试执行教程的全面讲解。我们首先描述了在 Selenium Grid 中执行测试执行的先决条件,以及如何在 Selenium Grid 中实际执行测试执行。

This concludes our comprehensive take on the tutorial on Selenium Grid - Test Execution. We’ve started with describing prerequisites to perform test execution in Selenium Grid, and how to actually perform a test execution in Selenium Grid.

这为您提供了对 Selenium Grid - 测试执行的深入了解。明智的做法是继续练习您所学的内容并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽您的视野。

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