在@Aftermethod中调用driver.close()之前,safardriver会自动关闭

时间:2016-12-01 11:31:53

标签: java selenium selenium-webdriver safaridriver

我在Windows 7上的safari浏览器版本5.1.7中运行测试脚本。

运行 @test 方法后,我在@aftermethod中调用 driver.quit()。但是safari浏览器在到达@aftermethod之前关闭并抛出 java.lang.NullPointerException 错误。由于这个新的Safari浏览器实例在浏览器关闭后没有打开。如何解决此问题?如何覆盖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)

3 个答案:

答案 0 :(得分:1)

您应该在init() mehtod之外定义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();
    }

答案 1 :(得分:0)

您应首先初始化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();
}

答案 2 :(得分:0)

试试这个,

public class Test{
        public WebDriver driver;

        @BeforeMethod
        public void init()
        {
            invokeSafariDriver();
        }

    public static void invokeSafariDriver() throws InterruptedException
        {
            //logger.log(LogStatus.INFO, "Launching Chrome driver");
            System.out.println("Launching Safari driver");

            System.setProperty("webdriver.safari.noinstall", "true");
            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();

            }

        }