0

I am developing a Java-based test automation project and using Selenium Standalone to run my tests. Currently, I can run tests on a single browser instance, but I want to execute parallel tests.

I tried using Selenium Grid, but I couldn't configure it properly. As an alternative, I attempted to run two separate Selenium Standalone instances on different ports (e.g., 4444 and 4445). However, I am not sure how to distribute tests between these instances in my code.

I know that Selenium Grid handles this automatically, but how can I manage parallel test execution using Selenium Standalone?

Any guidance on how to structure my code for this would be greatly appreciated!

I tried running two Selenium Standalone instances on ports 4444 and 4445. I expected my tests to distribute between them automatically, but instead, all tests ran on a single instance. I need a way to manage parallel execution across multiple standalone instances.

2
  • 1
    Are you not able to just create a new WebDriver instance (Browser) per test? Then just run your tests in parallelCommentedMar 6 at 7:19
  • Also, if you don't know how to configure Selenium Grid, you can use the standalone-chrome docker image which has a configured selenium grid ready to use. Then just create remotedriver instances per test pointed to http://[hostname]:4444/wd/hubCommentedMar 6 at 7:38

3 Answers 3

0

Start the Selenium Grid Hub

First, launch the Hub, which will manage test distribution:

java -jar selenium-server-4.x.jar hub 

Connect Nodes to the Hub

Now, start two nodes on different ports (you can add more if needed):

java -jar selenium-server-4.x.jar node --port 5555 --hub http://localhost:4444 java -jar selenium-server-4.x.jar node --port 5556 --hub http://localhost:4444 

These nodes will execute the tests assigned by the Hub.

Connect to Selenium Grid in a Test Class

In your test code, configure WebDriver to connect to the Grid Hub. Here’s an example using JUnit 5:

import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.MalformedURLException; import java.net.URL; import static org.junit.jupiter.api.Assertions.assertTrue; public class SeleniumGridTest { private WebDriver driver; @BeforeEach void setUp() throws MalformedURLException { // Specify Selenium Grid Hub URL URL hubUrl = new URL("http://localhost:4444/wd/hub"); // Browser options (you can also use FirefoxOptions, etc.) ChromeOptions options = new ChromeOptions(); // Create a Remote WebDriver driver = new RemoteWebDriver(hubUrl, options); } @Test void testGoogleHomePage() { driver.get("https://www.google.com"); String title = driver.getTitle(); System.out.println("Page Title: " + title); assertTrue(title.contains("Google")); } @AfterEach void tearDown() { if (driver != null) { driver.quit(); } } } 
1
  • It does not work btw. It's never connected to hub.CommentedMar 7 at 11:24
0

You do not need Selenium Grid to run tests in parallel. Modern computers have multiple cores or processors - you can utilize this to run tests vertically instead of horizontally.

As your project is based on Java - you can run tests in parallel using the TestNG test runner. Below is an example of how it can be done or configured.

enter image description here

1
  • thanks i know this method but i'm usign cucumber wtih testrunner.javaCommentedMar 10 at 14:00
-1

Running Tests in Parallel using Selenium Grid To execute tests on several browsers or machines, you can run Selenium Grid rather than a standalone server.

  1. Run Selenium Grid in Standalone Mode: java -jar selenium-server-4.x.x.jar standalone

  2. WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new ChromeOptions());

  3. Perform the tests as given in the JUnit or TestNG sections.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.