如何解析Java.lang.threadLocal.get()尝试在testng.xml中运行并行执行时是无效的?

发布于 2025-02-10 02:01:04 字数 2199 浏览 1 评论 0原文

In testng.xml, I am running few tests paralleled(parallel="classes" thread-count="5") and few sequentially as two different tests. But for first testmethod from second test it is throwing NullPointerException because value of Java.lang.ThreadLocal.get() is null. I am calling setDriver() from InvokedMethodListener.beforeInvocation() and removeDriver() from InvokedMethodListener.afterInvocation().

`public class DriverFactory {
 private static DriverFactory instance = new DriverFactory();

 public static synchronized DriverFactory getInstance() {
    return instance;
 }

 ThreadLocal<WebDriver> tlDriver = new ThreadLocal<>();

 public synchronized void setDriver(String browserName) {
    if (browserName.equalsIgnoreCase("chrome")) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("disable-infobars");
        chromeOptions.addArguments("--start-maximized");
        chromeOptions.addArguments("--headless");
        System.setProperty("webdriver.chrome.args", "--disable-logging");
        System.setProperty("webdriver.chrome.silentOutput", "true");
        WebDriverManager.chromedriver().setup();
        tlDriver.set(new ChromeDriver(chromeOptions));

    } else if (browserName.equalsIgnoreCase("firefox")) {
         System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") +
         "/Drivers/geckodriver.exe");
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.setHeadless(true).setProfile(firefoxProfile);
        System.setProperty("webdriver.firefox.args", "--disable-logging");
        System.setProperty("webdriver.firefox.silentOutput", "true");
        tlDriver.set(new FirefoxDriver(firefoxOptions));
    } 
 public synchronized void removeDriver() {
    try {
    tlDriver.get().quit();
    tlDriver.remove();
    }
    catch(Exception e) {
        e.printStackTrace();
        System.out.println("***AfterRemove***");
        System.out.println(e);
        //System.out.println(e.getMessage());
    }
   }
   }

 `

我已经尝试更改测试的顺序,但没有用。问题可能与线程池的WebDriver实例正确设置。可能已经死了。

In testng.xml, I am running few tests paralleled(parallel="classes" thread-count="5") and few sequentially as two different tests. But for first testmethod from second test it is throwing NullPointerException because value of Java.lang.ThreadLocal.get() is null. I am calling setDriver() from InvokedMethodListener.beforeInvocation() and removeDriver() from InvokedMethodListener.afterInvocation().

`public class DriverFactory {
 private static DriverFactory instance = new DriverFactory();

 public static synchronized DriverFactory getInstance() {
    return instance;
 }

 ThreadLocal<WebDriver> tlDriver = new ThreadLocal<>();

 public synchronized void setDriver(String browserName) {
    if (browserName.equalsIgnoreCase("chrome")) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("disable-infobars");
        chromeOptions.addArguments("--start-maximized");
        chromeOptions.addArguments("--headless");
        System.setProperty("webdriver.chrome.args", "--disable-logging");
        System.setProperty("webdriver.chrome.silentOutput", "true");
        WebDriverManager.chromedriver().setup();
        tlDriver.set(new ChromeDriver(chromeOptions));

    } else if (browserName.equalsIgnoreCase("firefox")) {
         System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") +
         "/Drivers/geckodriver.exe");
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.setHeadless(true).setProfile(firefoxProfile);
        System.setProperty("webdriver.firefox.args", "--disable-logging");
        System.setProperty("webdriver.firefox.silentOutput", "true");
        tlDriver.set(new FirefoxDriver(firefoxOptions));
    } 
 public synchronized void removeDriver() {
    try {
    tlDriver.get().quit();
    tlDriver.remove();
    }
    catch(Exception e) {
        e.printStackTrace();
        System.out.println("***AfterRemove***");
        System.out.println(e);
        //System.out.println(e.getMessage());
    }
   }
   }

 `

I have tried changing the sequence of tests but no use.Problem might be with webdriver instance from thread pool is seting up properly.Sometimes it is also throwing exception a org.openqa.selenium.remote.UnreachableBrowserException:Error communicatimg with remote browser.It may have died.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2025-02-17 02:01:04

您要运行的方法应为(suite&gt; test&gt; class&gt; class&gt; class&gt; class&gt;方法)在testng xml中,还提及侦听器(suite&gt;听众&gt; listerer)testng xml中

The method you are trying to run should be (suite > test > classes > class >methods) in the testng xml and also mention the Listener (suite > listeners > listener) class in testng xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文