1

我在 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)
4

3 回答 3

1

您应该在 mehtod 之外定义 Webdriver,init()以便它也可以在其他方法中访问。

    WebDriver driver;

    @BeforeMethod
    public void init()
    {
        driver = new SafariDriver();
    }

    @Test
    public void launch
    {
        driver.get("www.google.com");
    }

    @AfterMethod
    public void tearDown()
    {
        driver.quit();
    }
于 2016-12-01T12:44:47.210 回答
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();
}
于 2016-12-01T12:31:51.123 回答
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();

            }

        }
于 2016-12-01T14:18:00.613 回答