我正在Windows 7的Safari浏览器5.1.7版中的测试脚本下运行。
运行@test方法后,我在@aftermethod中调用driver.quit()。但是,Safari浏览器在到达@aftermethod之前关闭并抛出java.lang.NullPointerException错误。由于此新的Safari浏览器实例在关闭浏览器后无法打开。如何解决此问题?如何覆盖野生动物园关机?下面是执行日志
public class Test{
WebDriver driver;
@BeforeMethod
public void init()
{
driver = invokeSafariDriver();
}
public static WebDriver invokeSafariDriver() throws InterruptedException
{
//logger.log(LogStatus.INFO, "Launching Chrome driver");
System.out.println("Launching Safari driver");
System.setProperty("webdriver.safari.noinstall", "true");
WebDriver driver = new SafariDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
Thread.sleep(2000);
return driver;
}
@Test
public void launch
{
driver.get("www.google.com");
}
@AfterMethod(alwaysRun=true)
public void closeBrowser()
{
try{
System.out.println("Closing the browser");
logger.log(LogStatus.INFO, "Closing the Browser");
driver.quit();
}
catch(Exception e){
driver.close();
}
}
Dec 01, 2016 4:54:45 PM org.openqa.selenium.safari.SafariDriverServer start
INFO: Server started on port 7469
Dec 01, 2016 4:54:45 PM org.openqa.selenium.safari.SafariDriverCommandExecutor start
INFO: Launching Safari
Dec 01, 2016 4:54:45 PM org.openqa.selenium.safari.SafariDriverCommandExecutor start
INFO: Waiting for SafariDriver to connect
Dec 01, 2016 4:54:48 PM org.openqa.selenium.safari.SafariDriverChannelHandler$1 operationComplete
INFO: Connection opened
Dec 01, 2016 4:54:48 PM org.openqa.selenium.safari.SafariDriverCommandExecutor start
INFO: Driver connected in 3408 ms
Dec 01, 2016 4:54:48 PM org.openqa.selenium.safari.SafariDriverCommandExecutor stop
INFO: Shutting down
Dec 01, 2016 4:54:48 PM org.openqa.selenium.safari.SafariDriverCommandExecutor stop
INFO: Closing connection
Dec 01, 2016 4:54:49 PM org.openqa.selenium.safari.SafariDriverCommandExecutor stop
INFO: Stopping Safari
Dec 01, 2016 4:54:49 PM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@c540f5a
Dec 01, 2016 4:54:49 PM org.openqa.selenium.safari.SafariDriverCommandExecutor stop
INFO: Stopping server
Dec 01, 2016 4:54:49 PM org.openqa.selenium.safari.SafariDriverServer stop
INFO: Stopping server
Dec 01, 2016 4:54:49 PM org.openqa.selenium.safari.SafariDriverCommandExecutor stop
INFO: Shutdown complete
FAILED CONFIGURATION: @AfterMethod closeBroser
java.lang.NullPointerException
at com.test.betawoohoo.Example.closeBroser(Example.java:289)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
最佳答案
您应该在init()
方法之外定义Webdriver,以便也可以在其他方法中访问它。
WebDriver driver;
@BeforeMethod
public void init()
{
driver = new SafariDriver();
}
@Test
public void launch
{
driver.get("www.google.com");
}
@AfterMethod
public void tearDown()
{
driver.quit();
}
关于java - 在@Aftermethod中调用driver.close()之前,safardriver自动关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40909578/