WebDriver をスレッドセーフにしようとしていますが、テストを実行すると nullPointerException が発生します。次に、リモート WebDriver が初期化されるかどうかを確認しましたが、初期化されておらず、どこが間違っているのかわかりません。方法がわからないため、オンラインのヘルプを使用してWebDriverをスレッドセーフにしています。
ここに私のコード。
DesiredCapabilities cap = null;
private List<WebDriver> webDriverPool = Collections.synchronizedList(new ArrayList<WebDriver>());
private ThreadLocal<WebDriver> driverThread;
@Parameters ({"browser"})
@BeforeTest
public void launchBrowser(String browser) {
//initLogs(this.getClass());
initConfigurations();
if(browser.equals("firefox")){
cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox"); //
cap.setPlatform(Platform.WINDOWS);
}else if (browser.equals("chrome")){
cap = DesiredCapabilities.chrome(); // no need path of chrome exe
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
}else if (browser.equals("internet explorer")){
cap = DesiredCapabilities.internetExplorer(); // no need path of ie exe
cap.setBrowserName("internet explorer");
cap.setPlatform(Platform.WINDOWS);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability("ie.ensureCleanSession", true);
}
this.driverThread = new ThreadLocal<WebDriver>() {
@Override
protected WebDriver initialValue(){
WebDriver webDriver = null;
System.out.println(webDriver);
try {
webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
webDriverPool.add(webDriver);
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println(webDriver);
return webDriver;
}
};
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public WebDriver getDriver() {
return this.driverThread.get();
}
@AfterTest
public void afterTest() {
for (WebDriver driver : this.webDriverPool) {
driver.quit();
}
}
例外トレース
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:34)
at com.sun.proxy.$Proxy9.sendKeys(Unknown Source)
at ww5.pages.LoginPage.doLogin(LoginPage.java:31)
at ww5.testcases.storageRuleSuite.GoToStorageRulePageTest.goToStorageRulePageTest(GoToStorageRulePageTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)