Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Selenium Grid

Selenium Grid

1. Introduction to Selenium Grid

Selenium Grid is an important component of the Selenium suite; it allows you to run tests in parallel across multiple different machines, browsers, and operating systems.

Through Selenium Grid, you can perform distributed testing, thereby improving test efficiency and shortening test time.

The core idea of Selenium Grid is to distribute test tasks to multiple nodes for execution, with these nodes managed centrally by a hub.

1.1 Main Features of Selenium Grid

1.2 Architecture of Selenium Grid

Selenium Grid uses a Hub-Node architecture:


2. Configuring Selenium Grid

Before you start using Selenium Grid, you need to do some basic configuration. The following steps are based on Selenium 4.

2.1 Install Java

Selenium Grid depends on the Java runtime environment, so you need to install Java first. You can install Java by following these steps:

  1. Access the Oracle Java or OpenJDK Download the Java version suitable for your operating system.
  2. Install Java and configure the environment variables.

2.2 Download Selenium Server

Selenium Server is the core component of Selenium Grid. You can download it from the official Selenium website the latest version of Selenium Server.

  1. Download selenium-server-.jar files.
  2. Save the downloaded file to a suitable directory.

2.3 Start the Hub

After configuring Java and Selenium Server, you can start the Hub. Open the command line tool, navigate to the directory where selenium-server-.jar is saved, and then run the following command:

Example

java

-jar

selenium-server-

<

version

.jar hub

This will start the Hub, which by default listens on port 4444. You can access http://localhost:4444 via a browser to view the Hub's status.

2.4 Start the Node

Next, you need to start one or more Nodes to execute test tasks. In another machine or a different terminal on the same machine, run the following command:

Example

java

-jar

selenium-server-

<

version

.jar node

--hub

http:

//<

hub-ip

:

4444

Where is the IP address of the machine where the Hub is located. If the Hub and Node are on the same machine, you can use localhost.

2.5 Configure the Node's Browser and Operating System

You can configure the Node's browser and operating system via command-line parameters. For example, the following command configures a Node that supports Chrome and Firefox browsers:

Example

java

-jar

selenium-server-

<

version

.jar node

--hub

http:

//<

hub-ip

:

4444

--browser

"browserName=chrome"

--browser

"browserName=firefox"

3. Running Tests Remotely

After configuring Selenium Grid, you can run tests remotely by writing test scripts. Here is a Python example based on Selenium 4.

3.1 Install Selenium Python Bindings

First, you need to install the Selenium Python bindings:

Example

pip

install

selenium

3.2 Write the Test Script

Here is a simple test script that executes tests on a remote node via Selenium Grid:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

common

.

desired_capabilities

import

DesiredCapabilities

Set Desired Capabilities

capabilities

=

DesiredCapabilities.

CHROME

.

copy

(

)

capabilities

[

'platform'

]

=

'WINDOWS'

 

Specify the Operating System

capabilities

[

'version'

]

=

'latest'

   

Specify the Browser Version

Connect to the Selenium Grid Hub

driver

=

webdriver.

Remote

(

    command_executor

=

'http://:4444/wd/hub'

,

    desired_capabilities

=

capabilities

)

Execute the Test

driver.

get

(

"https://www.example.com"

)

print

(

driver.

title

)

driver.

quit

(

)

3.3 Run the Test Script

Save the script and run it. The script will distribute the test tasks to the appropriate Node for execution via the Selenium Grid Hub.


4. Advanced Configuration of Selenium Grid

4.1 Configure the Node's Browser and Operating System

When starting a Node, you can specify the supported browsers and operating systems via parameters:



java -jar selenium-server-.jar node --hub http://:4444 --browser "browserName=chrome,platform=WINDOWS"

4.2 Run Selenium Grid Using Docker

Selenium provides official Docker images to quickly start the Hub and Node:



# 启动 Hub

docker run -d -p 4444:4444 --name selenium-hub selenium/hub



# 启动 Node

docker run -d --link selenium-hub:hub selenium/node-chrome

4.3 Configure Load Balancing for the Grid

You can improve test concurrency by starting multiple Hubs and Nodes and configuring load balancing.