Selenium Grid running tests in parallel

4.8k views Asked by At

Currently, I have a Selenium grid setup, with 1 local hub and 2 local nodes. The hub is capable of distributing the tests to run in parallel and distribute it over to the nodes. I am running the tests in parallel.

The following is the base test

public abstract class BaseTest 
   {    
            String                      testFolder;
            String                      testName;
            protected String            envName;        
            protected Configuration     config;
            protected String            host;
            protected RemoteWebDriver   driver;
            protected String            proxy;
            protected SomeData      someData;
            protected SomeController someController;

            public BaseTest() {
              }

            public BaseTest( String testFolder, String testName) 
            {   
                this.testFolder         = testFolder;
                this.testName           = testName;
                this.envName            = System.getProperty("config");
                this.proxy              = System.getProperty("proxy");
                config = this.envName;
            }

            @BeforeMethod 
            public void startTest(Method testMethod) {
                LOG.info("Starting test: " + testMethod.getName());
                try {
                this.someData       = new SomeData();
                this.driver             = WebDriverSetup.getDriver();
                this.someController = new someController(this.driver, this.someData);
                driver.navigate().to("https://" + this.host);
                } catch (MalformedURLException e) {
                    System.out.println("MalformedURLException");
                }
            }
            @AfterMethod
            public void closeWindow() {
                driver.close();
                driver.quit();
            }
        }

The following is the class to get the RemoteWebDriver:

        public class WebDriverSetup {

            public static RemoteWebDriver getDriver() throws MalformedURLException{

                String SELENIUM_HUB_URL = "http://localhost:4444/wd/hub";
                ThreadLocal<RemoteWebDriver> remoteWebDriver = null;

                DesiredCapabilities capabilities = DesiredCapabilities.firefox();
                capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
                String proxy = System.getProperty("proxy");

                if (proxy != null && !proxy.isEmpty()) {
                    System.out.println("Using proxy: " + proxy);
                    capabilities.setCapability(CapabilityType.PROXY, proxy);
                }

                try {
                    remoteWebDriver = new ThreadLocal<RemoteWebDriver>();
                    remoteWebDriver.set(new RemoteWebDriver(new URL(SELENIUM_HUB_URL),
                            capabilities));
                } catch (MalformedURLException e) {
                    System.out.println("Tackle Issue with RemoteDriverSetup");
                }

                remoteWebDriver.get().manage().window()
                        .setSize(new Dimension(2880, 1524));
                remoteWebDriver.get().manage().timeouts()
                        .pageLoadTimeout(10, TimeUnit.SECONDS);
                remoteWebDriver.get().manage().timeouts()
                        .implicitlyWait(10, TimeUnit.SECONDS);

                return remoteWebDriver.get();
            }
        }

My test suite is like :

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Multiple Tests Suite" verbose="1" parallel="methods">
    <test name="Test1">
        <classes>
            <class name="com.itesteverything.qa.Tests"></class>
        </classes>
    </test>
</suite>

Tests are like :

public class Tests extends BaseTest {

@Parameters({"testName", "env" })
public Tests( @Optional String testName, @Optional String env ) {
    super( null, testName, null, env );
}

@BeforeMethod
public void setup() throws Exception {
    //setSomeData
}   

public void test1() throws Exception {  

use driver from super
use someData from super
use someController is using the driver from super
}

public void test2() throws Exception {  

use driver from super
use someData from super
use someController is using the driver from super
}

While running these tests, I get the following errors

Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40' Driver info: driver.version: RemoteWebDriver org.openqa.selenium.remote.SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40' Driver info: driver.version: RemoteWebDriver at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352) at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:393) at org.openqa.selenium.By$ById.findElement(By.java:214) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344) at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59) at com.sun.proxy.$Proxy25.sendKeys(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) at org.testng.internal.Invoker.invokeMethod(Invoker.java:673) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) TEST FAILED: test2 FAILED REASON: Session ID is null. Using WebDriver after calling quit()?

Is it something anyone aware of?

Thanks in advance!

2

There are 2 answers

4
niharika_neo On BEST ANSWER

Do not set driver in the base class, do not have driver property at all. The same instance is being overridden by different threadlocal drivers.

Any time you want to run your test, refer to WebDriverSetup.getDriver() in your test method itself and in your after/before methods.

3
SkorpEN On
    @AfterMethod

Is running after each method. And U run's only one setup. So after first method U close and it is closed as shows stack trace.