0

I have a hybrid framework based on Selenium WebDriver. It is taking around 2-3 hours to run the test suite I have right now. What is the best way to start running the tests parallel On the same machine (Even if I use Selenium Grid, how many nodes can I have at max on one machine, provided I also I have to use the same machine as the Hub ?). I have the constraint of using only one physical machine, and I am not using Test NG.

User
  • 7
  • 7
  • Only one node you can use with your own machine if you are using same machine as node.. you can use Jenkins also – Ankur Singh Apr 13 '18 at 16:09

1 Answers1

0

Run it with maven using the failsafe plugin by configuring to run in parallel with multiple threads.

Below is a sample code for running junit tests in 5 threads with test classes (placed in default location src/test/java and using the default class inclusion rules) running in parallel. The webdriver are instantiated in the BeforeClass and destroyed in the AfterClass methods. The webdriver instances are stored in a static ThreadLocal variable for keeping them separate.

pom.xml -

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.21.0</version>

            <configuration>
                <parallel>classes</parallel> 
                <threadCount>5</threadCount>
            </configuration>

            <executions>
                <execution>
                    <id>integration-test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

BaseClass -

public class BaseTestIT {

    @BeforeClass
    public static void setup() {
        System.setProperty("webdriver.chrome.driver", "");
        WebDriver driver = new ChromeDriver();
        ThreadWebDriver.set(driver);
    }

    @AfterClass
    public static void teardown() {
        ThreadWebDriver.get().quit();
        ThreadWebDriver.remove();
    }
}

ThreadLocal -

public class ThreadWebDriver {

    private static final ThreadLocal<WebDriver> threadWebDriver = new InheritableThreadLocal<WebDriver>();

    private ThreadWebDriver() { }

    public static WebDriver get() {
        return threadWebDriver.get();
    }

    public static void set(WebDriver driver) {
        threadWebDriver.set(driver);
    }

    public static void remove() {
        threadWebDriver.remove();
    }    
}

TestOneClass - Both the methods will run in same thread.

public class FirstTestIT extends BaseTestIT {

    @Test
    public void testOne() {
        System.out.println(Thread.currentThread().getId());     
        WebDriver driver = ThreadWebDriver.get();
        driver.get("https://junit.org/junit4/");
        driver.manage().window().maximize();
    }

    @Test
    public void testTwo() {
        System.out.println(Thread.currentThread().getId());     
        WebDriver driver = ThreadWebDriver.get();
        driver.get("https://junit.org/junit5/");
        driver.manage().window().maximize();
    }
}

TestTwoClass - Single method will run in separate thread.

public class ThirdTestIT extends BaseTestIT{

    @Test
    public void test() {
        System.out.println("third one");
        WebDriver driver = ThreadWebDriver.get();       
        driver.get("https://stackoverflow.com/questions/tagged/selenium");
        driver.manage().window().maximize();
    }
}

You can even look at using a sharedwebdriver concept in which the driver will only be shut when the JVM closes. This link uses cucumber but pretty easy to modify just for selenium use. Remove the before and after hooks and handle this part in junit hooks.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32