Selenium Grid version 4 Tutorial – Standalone

Hello everybody,

As you realized, we started exploring Selenium 4. Here’s the first article that we have. After our first article, I wanted to take a look at the Selenium Grid version 4. There are many changes coming with Selenium Grid. Concepts, commands, URLs, and many more things have changed.

There are three ways to run Selenium Grid

  • Standalone
  • Classical Grid (Hub and node)
  • Fully Distributed (Session, Distributor, etc)

I am going to show you how to run a Standalone Selenium Grid and run a test on it.

From now on, when you execute your selenium standalone.jar file, it detects which drivers you have in your system. That’s why you need to place all the executables into your PATH.

Run Selenium Grid 4

Let’s add our first driver (chrome driver) into a folder. Then add this folder to the computer’s PATH and run the below command. If you don’t place Chrome or Firefox drivers in a folder you will not be able to execute test on those browsers

java -jar selenium-server-4.0.0-alpha-1.jar standalone

You will see an output like below. As you can see Chrome Driver is registered into our Grid.

21:55:06.349 INFO [Standalone.lambda$configure$1] - Logging configured.
21:55:06.355 INFO [Standalone.lambda$configure$1] - Using tracer: OpenTracing(NoopTracerImpl)
21:55:06.356 INFO [EventBusConfig.createBus] - Creating event bus: org.openqa.selenium.events.local.GuavaEventBus
21:55:06.649 INFO [NodeOptions.lambda$null$1] - Adding Capabilities {browserName: chrome} 5 times

Then navigate to http://localhost:4444/status

You will see your driver registered with 5 instances. Safari driver is automatically registered as I am using a MacBook.

{
  "value": {
    "ready": true,
    "message": "Selenium Grid ready.",
    "nodes": [
      {
        "id": "9a08c5bb-8d96-47ed-9b57-edd967690273",
        "uri": "http:\u002f\u002f192.168.1.106:4444",
        "maxSessions": 12,
        "stereotypes": [
          {
            "capabilities": {
              "browserName": "Safari Technology Preview"
            },
            "count": 1
          },
          {
            "capabilities": {
              "browserName": "safari"
            },
            "count": 1
          },
          {
            "capabilities": {
              "browserName": "chrome"
            },
            "count": 5
          }
        ],
        "sessions": [
        ]
      }
    ]
  }
}

You can add Gecko Driver in the same folder with Chrome Driver. Then you can check if it’s registered to Grid or not. Now my output should be like below.

{
  "value": {
    "ready": true,
    "message": "Selenium Grid ready.",
    "nodes": [
      {
        "id": "9a08c5bb-8d96-47ed-9b57-edd967690273",
        "uri": "http:\u002f\u002f192.168.1.106:4444",
        "maxSessions": 12,
        "stereotypes": [
          {
            "capabilities": {
              "browserName": "Safari Technology Preview"
            },
            "count": 1
          },
          {
            "capabilities": {
              "browserName": "safari"
            },
            "count": 1
          },
          {
            "capabilities": {
              "browserName": "chrome"
            },
            "count": 5
          },
          {
            "capabilities": {
              "browserName": "firefox"
            },
            "count": 5
          }
        ],
        "sessions": [
        ]
      }
    ]
  }
}

Run a Test

Now, you can implement your first test. You just need to point your RemoveWebDriver to http://localhost:4444/ instead of http://localhost:4444/wd/hub which is the relevant URL for Selenium Grid 2.

@Test
  public void remoteTest(){
    ChromeOptions chromeOptions = new ChromeOptions();
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(CapabilityType.BROWSER_NAME,"chrome");

    caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    WebDriver driver = null;
    try {
      driver = new RemoteWebDriver(URI.create("http://localhost:4444/").toURL(), caps);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    driver.navigate().to("https://www.swtestacademy.com");

    WebElement logo = driver.findElement(By.className("fusion-logo-link"));

    System.out.println(logo.isDisplayed());
  }

After the execution, if you navigate to http://localhost:4444/status you’ll be able to see the session details.

,
        "sessions": [
          {
            "sessionId": "a47775a1f81397275d1fc1ecf5e17bf6",
            "stereotype": {
              "browserName": "chrome"
            },
            "currentCapabilities": {
              "acceptInsecureCerts": false,
              "browserName": "chrome",
              "browserVersion": "78.0.3904.70",
              "chrome": {
                "chromedriverVersion": "77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs\u002fbranch-heads\u002f3865@{#442})",
                "userDataDir": "\u002fvar\u002ffolders\u002fxv\u002fm8cs2_cj31jgk2x4qjmw9gp40000gn\u002fT\u002f.com.google.Chrome.IWb9JG"
              },
              "goog:chromeOptions": {
                "debuggerAddress": "localhost:51537"
              },
              "networkConnectionEnabled": false,
              "pageLoadStrategy": "normal",
              "platformName": "mac os x",
              "proxy": {
              },
              "setWindowRect": true,
              "strictFileInteractability": false,
              "timeouts": {
                "implicit": 0,
                "pageLoad": 300000,
                "script": 30000
              },
              "unhandledPromptBehavior": "dismiss and notify"
            }
          }
        ]

You can find the related codes in our Github Account https://github.com/swtestacademy/Selenium4

Happy Testing!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.