Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 在测试之间重新启动浏览器_Selenium_Webdriver - Fatal编程技术网

Selenium 在测试之间重新启动浏览器

Selenium 在测试之间重新启动浏览器,selenium,webdriver,Selenium,Webdriver,如何在测试之间重新启动浏览器? 我有DrivErFactory,在构造函数的BasePage中我调用 public BasePage() { this.driver = DriverFactory.getDriver(); this.wait = new WebDriverWait(getDriver(), waitTime); } 在我的测试中,我有: @BeforeMethod public void beforeTest() { loginPage.open();} 及

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

public BasePage() {
    this.driver = DriverFactory.getDriver();
    this.wait = new WebDriverWait(getDriver(), waitTime);
}
在我的测试中,我有:

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

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

如果我在之后使用:

loginPage.closeDriver();
我有: org.openqa.selenium.NoSuchSessionException:没有这样的会话

在BeforeTest中,WebDriver可以启动,那么为什么在BeforeTest之后不启动呢

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

DriverFactory

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;
    }
公共类驱动程序工厂{
静态属性数据库属性数据库=新属性数据库();
Properties prop=新属性();
私有静态WebDriver;
私有静态void setChromeDriverPath(){
字符串chromeDriverPath=“.\\resources\\chromedriver.exe”;
System.setProperty(“webdriver.chrome.driver”,chromeDriverPath);
}
私有静态void setFireFoxDriverPath(){
字符串chromeDriverPath=“.\\resources\\geckodriver.exe”;
System.setProperty(“webdriver.gecko.driver”,chromeDriverPath);
}
专用静态void配置驱动程序(){
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
}
私有静态FirefoxProfile setFireFoxProfile(){
FirefoxProfile profile=新的FirefoxProfile();
profile.setPreference(“browser.download.dir”,“/downloads”);
profile.setPreference(“browser.download.folderList”,2);
profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”,
“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet;”;
profile.setPreference(“browser.download.manager.showWhenStarting”,false);
setPreference(“pdfjs.disabled”,true);
回报曲线;
}
专用静态ChromeOptions设置ChromeProfile(){
ChromeOptions选项=新的ChromeOptions();
Map prefs=新的HashMap();
prefs.put(“download.prompt\u for\u download”,true);
prefs.put(“download.default_目录”,“/download”);
选项。设置实验选项(“prefs”,prefs);
返回选项;
}
私有静态WebDriver createChromeDriver(){
setChromeDriverPath();
返回驱动程序=新的ChromeDriver(setChromeProfile());
}
私有静态WebDriver createFireFoxDriver(){
setFireFoxDriverPath();
返回驱动程序=新的FirefoxDriver(setFireFoxProfile());
}
公共静态WebDriver getDriver(){
String browserType=propertyBase.getProperty(“浏览器”);
开关(浏览器型){
案例“铬”:
if(驱动程序==null){
driver=createChromeDriver();
}
打破
案例“firefox”:
if(驱动程序==null){
createFireFoxDriver();
}
打破
}
配置驱动程序();
返回驱动器;
}

您有一个从工厂获取驱动程序对象的方法。也许您也应该在那里创建一个方法来关闭它。您现在可以在loginpage上调用它。这似乎不太符合逻辑。但是重要的是,可能需要使用driver.quit()或driver.close()关闭驱动程序,然后确保将driver=null

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

如果我的假设是正确的,那么这应该可以实现,否则请在loginPage.quitDriver()中发布您的所有内容

类似这样的,在DriverFactory类中

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

编辑:输入错误

你能分享getDriver方法的代码吗?请澄清,你想在什么时候重新启动浏览器。在每次测试前重新启动显示函数代码
loginPage.quitedDriver()
loginPage.closeDriver()
有效:)谢谢,老兄!也谢谢你提供关于loginPage.quitDriver()的提示.如果是的话,也许你可以接受答案,让人们知道这个问题已经得到了回答。谢谢
public static void quitDriver() {
    driver.quit();
    driver = null;
}