This page looks best with JavaScript enabled

Selenium Grid

 ·  πŸŽƒ kr0m

Selenium Grid is a system for remotely executing web browsers. With Selenium Grid, a server acts as a hub while the rest of the servers act as nodes (WebDriver nodes).

When a test is executed, it is sent to the hub, and the hub distributes the workload among the nodes. This allows tests to be executed in parallel across multiple browsers, versions, and operating systems in a centralized manner.

Let’s start by installing the hub. We will need Java for this:

emerge -av virtual/jre

Download the Selenium server:

wget https://bit.ly/2TlkRyu -O selenium-server-standalone-3.141.59.jar

Finally, run the Selenium server with the -role hub option:

java -jar selenium-server-standalone-3.141.59.jar -role hub

Now we can move on to the WebDriver nodes. Let’s also install Java:

emerge -av virtual/jre

We are going to install Chrome, Firefox, and their associated drivers. This way, from the test script, we can choose which browser to run it on.

emerge -av www-client/google-chrome www-client/firefox

Create a user to run everything related to Selenium:

useradd selenium
su selenium -l

Download the Chrome driver for the installed Chrome version:

We install the Firefox driver (gecko):

We download the Selenium server:

wget https://bit.ly/2TlkRyu -O selenium-server-standalone-3.141.59.jar

This time we execute it with the parameter -role node and indicating the hub URL:

java -Dwebdriver.chrome.driver=chromedriver -Dwebdriver.gecko.driver=geckodriver -jar selenium-server-standalone-3.141.59.jar -role node -hub http://{{ip or host}}:4444/grid/register/ -port 4444

We can check the status of the grid by accessing the hub:
http://{{ip or host}}:4444/grid/console

Now we can start programming our first test, in my case I will do it in Python, first we will install selenium using pip:

pip install selenium --user

vi seleniumGrid.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Remote(command_executor='http://{{ip or host}}:4444/wd/hub', desired_capabilities=chrome_options.to_capabilities())

driver.get("http://alfaexploit.com")
print(driver.title)
assert "Seguridad, Linux, BSD y demΓ‘s frikerias" in driver.title
driver.close()

We run the test:

python3 seleniumGrid.py

In the Hub, we will see:

15:54:24.742 INFO [RequestHandler.process] - Got a request to create a new session: Capabilities {browserName: chrome, goog:chromeOptions: {args: [--headless, --no-sandbox, --disable-dev-shm-usage], extensions: []}, version: }
15:54:24.743 INFO [TestSlot.getNewSession] - Trying to create a new session on test slot {server:CONFIG_UUID=03799637-35c8-455c-a3e5-7d9e77f52819, seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platformName=LINUX, platform=LINUX}

In the node:

15:54:24.765 INFO [ActiveSessionFactory.apply] - Capabilities are: {
  "browserName": "chrome",
  "goog:chromeOptions": {
    "extensions": [
    ],
    "args": [
      "--headless",
      "--no-sandbox",
      "--disable-dev-shm-usage"
    ]
  },
  "version": ""
}
15:54:24.766 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 22576
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
15:54:24.956 INFO [ProtocolHandshake.createSession] - Detected dialect: W3C
15:54:24.978 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session 02d65cf76d5b0bf4ef74da780f9b20e8 (org.openqa.selenium.chrome.ChromeDriverService)

In the final server:

A.B.C.D - - - [05/Mar/2020:16:55:43 +0100] "GET /css/custom.css HTTP/1.1" 200 491 "http://alfaexploit.com/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/80.0.3987.132 Safari/537.36"

NOTE: It would be advisable to protect the SeleniumGrid servers with firewall rules to prevent unauthorized use.

To speed up the testing, we can write the following script in bash:

vi test.sh

#! /bin/bash
for i in $(seq 1 100); do
    python3 seleniumGrid.py &
done
chmod 700 test.sh
./test.sh
If you liked the article, you can treat me to a RedBull here