Java 为什么我不';是否通过webdriver关闭浏览器?

Java 为什么我不';是否通过webdriver关闭浏览器?,java,windows,internet-explorer,intellij-idea,selenium-webdriver,Java,Windows,Internet Explorer,Intellij Idea,Selenium Webdriver,有人能解释一下为什么@After部分中的方法在测试后不关闭浏览器吗 package TestCases; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver;

有人能解释一下为什么@After部分中的方法在测试后不关闭浏览器吗

package TestCases;

import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class ScriptCase {

    private WebDriver driver;

    @Before
    public void startWeb() {
        WebDriver driver = new InternetExplorerDriver();

        driver.navigate().to("https://play.google.com/store/apps/details?id=com.recursify.pixstack.free&hl=en");
    }

    @After
    public void ShutdownWeb() {
        driver.close();
    }

    @Test
    public void startWebDriver(){

        Assert.assertTrue("Title is different from expected",
                driver.getTitle().startsWith("PixStack Photo Editor Free"));

    }
}

当我直接在@Test节中将代码从@After移动到末尾时,我的项目将成功关闭浏览器。项目编译良好。

而不是在您可以尝试之前使用@

      @BeforeClass 
    baseUrl = "http://localhost:8080/";
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get(baseUrl);

and @AfterClass
  driver.quit();

所以试着用这个,它对我有用。

在你的示例代码中,你有两个不同的
驱动变量。一个是
startWeb
方法的本地,用于创建浏览器实例。另一个变量位于类级别,并且从未实例化。这是您试图在
ShutdownWeb
方法中使用的实例。要解决此问题,请不要在设置方法中重新声明本地
驱动程序
变量。也就是说:

public class ScriptCase {

  private WebDriver driver;

  @Before
  public void startWeb() {
    // This is the line of code that has changed. By removing
    // the type "WebDriver", the statement changes from declaring
    // a new local-scope variable to use of the already declared
    // class scope variable of the same name.
    driver = new InternetExplorerDriver();
    driver.navigate().to("https://play.google.com/store/apps/details?id=com.recursify.pixstack.free&hl=en");
  }

  @After
  public void shutdownWeb() {
    driver.quit();
  }

  @Test
  public void startWebDriver(){

    Assert.assertTrue("Title is different from expected", driver.getTitle().startsWith("PixStack Photo Editor Free"));

  }
}
此外,使用
quit
方法而不是
close
的建议是合理的,我已经在上面的代码中包含了这一更改

public class ScriptCase {

    private WebDriver driver;

   @BeforeClass 
    public void startWeb() {
       driver = new InternetExplorerDriver(); 
       driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
       driver.navigate().to("https://play.google.com/store/apps/details?id=com.recursify.pixstack.free&hl=en");
    }

    @AfterClass 
    public void ShutdownWeb() {
        driver.close();
        driver.quit();
    }

    @Test
    public void startWebDriver(){

        Assert.assertTrue("Title is different from expected",
                driver.getTitle().startsWith("PixStack Photo Editor Free"));

    }
}

试试这个。。。。。它可以工作

试试这个
driver.browser.close
2马丁·拉森:我得到提示:无法解析符号“browser”驱动程序。quit()关闭所有webdriver实例