How to instantiate remote webdriver for parallel testing

200 views
Skip to first unread message

MCD dotCom

unread,
Sep 30, 2022, 10:35:32 PM9/30/22
to testng-users
Hello,

Base:
public class Base{
    String remote_grid_server_url = "http://localhost:4444/wd/hub";

    public WebDriver driver;
    ChromeOptions chromeOptions = new ChromeOptions();

    @BeforeTest
    public void beforeTest(final ITestContext testContext) throws MalformedURLException {
         driver   = new RemoteWebDriver(new URL(remote_grid_server_url), chromeOptions);
         driver.manage().window().maximize();
    }
   
    @AfterTest
    public void afterTest() throws InterruptedException {
        System.out.println("afterTest");
    }
}

FirstTest
public class FirstTest extends  Base  {    
    @Test
    public void test1() {
        System.out.println("FirstTest test1");
         driver.get("http://www.gmail.com");
    }
}

SecondTest
public class SecondTest extends  Base  {    
    @Test
    public void test1() {
        System.out.println("SecondTest test1");
         driver.get("http://www.aol.com");

    }
}

ThirdTest
public class ThirdTest extends  Base  {
   
    @Test
    public void test1() {
        System.out.println("ThirdTest test1");
        driver.get("http://www.hotmail.com");

    }
}

XML
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Access Regression Suite" parallel="tests">
    <test name="Test1">
        <classes>
            <class name="Grid.FirstTest" />
            <class name=" Grid.ThirdTest" />
        </classes>
    </test>
    <test name="Test2">
        <classes>
            <class name=" Grid .SecondTest" />
        </classes>
    </test>
</suite>

Error:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null
    at Grid.ThirdTest.test1(ThirdTest.java:12)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:28)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:418)
    at org.testng.internal.thread.ThreadUtil.lambda$execute$0(ThreadUtil.java:64)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)

Under <Test1> tag, I have two tests: FIrstTest and ThirdTest. ThirdTest is failing. How can I instantiate the remote webdriver to fix the problem?

Thank you!

⇜Krishnan Mahadevan⇝

unread,
Oct 2, 2022, 2:46:44 AM10/2/22
to testng...@googlegroups.com
You have not mentioned what TestNG version you are working with.

If you use TestNG 7.6.1 (latest released version and needs JDK11), then this problem wouldn't occur. I have not been able to reproduce this with the below sample.

package com.rationaleemotions.beforetest;

import org.testng.ITestContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class Base {

  protected Object object;

  @BeforeTest
  public void beforeTest(ITestContext context) {
    System.err.println("Running beforeTest() for [" + context.getName() + "]");
    object = new Object();
  }

  @AfterTest
  public void afterTest(ITestContext context) {
    System.err.println("Running afterTest() for [" + context.getName() + "]");
  }
}
package com.rationaleemotions.beforetest;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.annotations.Test;

public class FirstTest extends Base {

  @Test
  public void testMethodFirstTest() {
    assertThat(this.object).isNotNull();
  }
}
package com.rationaleemotions.beforetest;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.annotations.Test;

public class SecondTest extends Base {

  @Test
  public void testMethodSecondTest() {
    assertThat(this.object).isNotNull();
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="my_suite" verbose="2">
  <test name="my_test_one" verbose="2">
    <classes>
      <class name="com.rationaleemotions.beforetest.FirstTest"/>
    </classes>
  </test>
  <test name="my_second_one" verbose="2">
    <classes>
      <class name="com.rationaleemotions.beforetest.SecondTest"/>
    </classes>
  </test>
</suite>
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
...
... TestNG 7.6.1 by Cédric Beust (ced...@beust.com)
...

Running beforeTest() for [my_test_one]
Running afterTest() for [my_test_one]
Running beforeTest() for [my_second_one]
Running afterTest() for [my_second_one]

===============================================
my_suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

PS: You should consider working with ThreadLocal variables and resort to using BeforeMethod driven WebDriver instantiation so that you can run multiple test methods in parallel (Your current design won't let you do it).

To learn more about parallel executions you can go through my blog: https://rationaleemotions.com/parallel_webdriver_executions_using_testng/

You can also try leveraging this library that I built which makes this a bit more easier by abstracting the webdriver creation and cleanup via annotations


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/ae3457ad-aa80-4a3a-a900-7ecf2c3249aan%40googlegroups.com.

MCD dotCom

unread,
Oct 2, 2022, 2:14:48 PM10/2/22
to testng-users
Sorry! I forgot to mention about the version

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
            <scope>test</scope>
        </dependency>

Thank you for your feedback. Let me go thru them. 
Reply all
Reply to author
Forward
0 new messages