首页 > 解决方案 > 在测试之间重新启动浏览器

问题描述

如何在测试之间重新启动浏览器?我有 DriveErFactory 并在构造函数的 BasePage 中调用

public BasePage() {
    this.driver = DriverFactory.getDriver();
    this.wait = new WebDriverWait(getDriver(), waitTime);
}

然后在我的测试中我有:

@BeforeMethod
public void beforeTest() {
loginPage.open();}

@AfterMethod
    public void afterTest() {
        loginPage.quitDriver();
    }

第一次测试后,我得到: org.openqa.selenium.NoSuchSessionException:会话 ID 为空。调用 quit() 后使用 WebDriver?

如果我在之后使用:

loginPage.closeDriver();

我有: org.openqa.selenium.NoSuchSessionException:没有这样的会话

BeforeTest WebDriver 中可以启动,为什么他在AfterTest之后不启动?

如何做得更好?每次测试前如何重启浏览器?

驱动工厂

public class DriverFactory {
    static PropertyBase propertyBase = new PropertyBase();
    Properties prop = new Properties();

    private static WebDriver driver;

    private static void setChromeDriverPath() {
        String chromeDriverPath = ".\\resources\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
    }

    private static void setFireFoxDriverPath() {
        String chromeDriverPath = ".\\resources\\geckodriver.exe";
        System.setProperty("webdriver.gecko.driver", chromeDriverPath);
    }

    private static void configureDriver() {
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
    }

    private static FirefoxProfile setFireFoxProfile() {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.dir", "./downloads");
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 
        profile.setPreference( "browser.download.manager.showWhenStarting", false );
        profile.setPreference( "pdfjs.disabled", true );
        return profile;
    }

    private static ChromeOptions setChromeProfile() {
        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("download.prompt_for_download", true);
        prefs.put("download.default_directory", "./download");
        options.setExperimentalOption("prefs", prefs);
        return options;
    }

    private static WebDriver createChromeDriver(){
        setChromeDriverPath();
        return driver = new ChromeDriver(setChromeProfile());
    }

    private static WebDriver createFireFoxDriver(){
        setFireFoxDriverPath();
        return driver = new FirefoxDriver(setFireFoxProfile());
    }

    public static WebDriver getDriver() {
        String browserType = propertyBase.getProperty("browser");

        switch (browserType) {
        case "chrome":
            if (driver==null) {
                driver = createChromeDriver();
            }
            break;
        case "firefox":
            if (driver==null) {
                createFireFoxDriver();
            }
            break;
        }
        configureDriver();
        return driver;
    }

标签: seleniumwebdriver

解决方案


您有一种从工厂获取驱动程序对象的方法。也许你也应该在那里制定一个方法来关闭它。您现在在登录页面上调用它。这似乎不太合乎逻辑。然而,重要的可能是使用 driver.quit() 或 driver.close() 关闭驱动程序,然后确保设置 driver=null;

这样,当您运行新测试时,将调用 getDriver 并且 if (driver == null) 确保实例化一个新对象。

如果我的假设是正确的,那么这应该可以解决问题,否则请在 loginPage.quitDriver() 中发布您拥有的任何内容。

像这样的东西,在 DriverFactory 类中

public static void quitDriver() {
    driver.quit();
    driver = null;
}

编辑:错别字


推荐阅读