TestNG-并行测试执行,WebDriver空指针

时间:2018-06-29 07:42:43

标签: java webdriver testng thread-local parallels

我确信这必须是一个容易解决的问题,但此刻我什么都没想到,这是情况。

我有一个Singleton类(DriverFactory.class)来设置和检索ThreadLocal对象。 两个并行运行的测试类(FirstTestClass)(SecondTestClass) 还有第三个TestListenerAdapter类,其中有一个方法,我想使用实际运行的WebDriver实例(它应该绑定到Thread),但是在这里我得到了空指针异常,即使这是代码

import config.DriverFactory;
import org.junit.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;

public class SecondTestClass {

    WebDriver driver;

    public SecondTestClass()throws Exception{
        DriverFactory.getInstance().setDriver();
        driver = DriverFactory.getInstance().getDriver();;
    }

    @Test
    public void testInSecondClass() {
        System.out.println("\n THREAD ID: " + Thread.currentThread().getId() + " " + "\n Name: " + Thread.currentThread().getName());
        driver.get("https://www.allsaints.com");
        System.out.println("CURRENT URL IS :" + driver.getCurrentUrl());

        Assert.assertTrue(false);

    }
}
package testParallel;

import config.DriverFactory;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class FirstTestClass {

    WebDriver driver;

    public FirstTestClass()throws Exception{
        DriverFactory.getInstance().setDriver();
        driver = DriverFactory.getInstance().getDriver();;
    }

    @Test
    public void testInFirstClass() throws Exception{
        System.out.println("\n THREAD ID: " + Thread.currentThread().getId() + " " + "\n Name: " + Thread.currentThread().getName());
        DriverFactory.getInstance().setDriver();
        driver = DriverFactory.getInstance().getDriver();;
        driver.get("https://pl.wikipedia.org");
        System.out.println("CURRENT URL IS :" + driver.getCurrentUrl());
    }
}
package config;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverFactory {

    private static DriverFactory instance = null;

    public ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

    private DriverFactory() {}

    @SuppressWarnings("varargs")
    public static DriverFactory getInstance() {
        if (instance == null) {
            instance = new DriverFactory();
        }
        return instance;
    }

    public final void setDriver() throws Exception {
        System.setProperty("webdriver.gecko.driver", "E:\\Projects\\allsaints\\tess\\drivers\\geckodriver.exe");
        webDriver.set(new FirefoxDriver());
    }

    public WebDriver getDriver() throws Exception {
        return webDriver.get();
    }
}
import config.DriverFactory;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class testSetup extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult tr) {
        System.out.println(
                "\n THREAD ID: " + Thread.currentThread().getId() + " " +
                        "\n Name: " + Thread.currentThread().getName()

        );
        WebDriver driver;
        try {
             driver = DriverFactory.getInstance().getDriver();
            System.out.println("CURRENT URL IS :" + driver.getCurrentUrl());

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onTestFailure(tr);
    }
}
<?xml version="1.0"?>
<suite name="All Test Suite" parallel="tests" thread-count="5">
    <listeners>
        <listener class-name="testParallel.testSetup"/>
    </listeners>
    <test verbose="3" preserve-order="true" name="test1">
        <classes>
            <class name="testParallel.FirstTestClass"/>
        </classes>
    </test>
    <test name="test2">
        <classes>
            <class name="testParallel.SecondTestClass"/>
        </classes>
    </test>
</suite>

控制台输出

  

class testParallel.FirstTestClass线程ID:22名称:TestNG

     

class testParallel.SecondTestClass线程ID:23名称:TestNG

     

class testParallel.testSetup线程ID:23名称:TestNG   java.lang.NullPointerException在   testParallel.testSetup.onTestFailure(testSetup.java:21)在   org.testng.internal.Invoker.runTestListeners(Invoker.java:1895)在   org.testng.internal.Invoker.runTestListeners(Invoker.java:1879)在   org.testng.internal.Invoker.invokeMethod(Invoker.java:778)在   org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)在   org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)在   org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)     在org.testng.TestRunner.privateRun(TestRunner.java:767)处   org.testng.TestRunner.run(TestRunner.java:617)在   org.testng.SuiteRunner.runTest(SuiteRunner.java:334)在   org.testng.SuiteRunner.access $ 000(SuiteRunner.java:37)在   org.testng.SuiteRunner $ SuiteWorker.run(SuiteRunner.java:368)在   org.testng.internal.thread.ThreadUtil $ 2.call(ThreadUtil.java:64)在   java.util.concurrent.FutureTask.run(FutureTask.java:266)在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)当前URL是   :https://pl.wikipedia.org/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna

1 个答案:

答案 0 :(得分:1)

在侦听器中,我们无法直接访问其他类实例。自此,它将在执行开始时开始侦听。如果需要获取某个特定的类实例,则可以通过下面的TestNG Listeners(export C_INCLUDE_PATH=/usr/include/python3.6m/ )进行访问,并且您所有的测试类都应该继承ITestResult类。

监听器类代码:

DriverFactory

测试类别1:

public class testSetup extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult tr) {
        System.out.println(
                "\n THREAD ID: " + Thread.currentThread().getId() + " " +
                        "\n Name: " + Thread.currentThread().getName()

        );

        Object currentClass=tr.getInstance();

        WebDriver driver;
        try {
             driver =((DriverFactory) currentClass).getDriver();
            System.out.println("CURRENT URL IS :" + driver.getCurrentUrl());

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onTestFailure(tr);
    }

}

测试类2:

public class FirstTestClass extends DriverFactory {

    WebDriver driver;