如何解析Java.lang.threadLocal.get()尝试在testng.xml中运行并行执行时是无效的?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要运行的方法应为(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