基类:
package env;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
import cucumber.api.Scenario;
import managers.FileReaderManager;
public class Env
{
static WebDriver driver = null;
Scenario s;
static String browserName = null;
public static String getBrowserName()
{
browserName = System.getProperty("browser");
if(browserName == null)
browserName = "ch";
return browserName;
}
public static WebDriver CreateWebDriver(String browser)
{
System.out.println("Browser: " + browser);
switch (browser.toLowerCase()) {
case "ff":
case "firefox":
System.out.println("Firefox is selected");
System.setProperty("webdriver.gecko.driver",
"E:\\eclipse\\geckodriver.exe");
driver=new FirefoxDriver();
break;
case "ch":
case "chrome":
System.out.println("Google chrome is selected");
System.setProperty("webdriver.chrome.driver",
FileReaderManager.getInstance().getConfigReader().getDriverPath());
driver = new ChromeDriver();
break;
case "ie" :
case "internetexplorer":
System.out.println("Internet Explorer is selected");
driver = new InternetExplorerDriver();
break;
case "safari":
System.out.println("Safari is selected");
driver = new SafariDriver();
break;
default:
System.out.println("Invalid browser name "+browser);
System.exit(0);
break;
}//switch
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
public static WebDriver getDriver() {
return driver;
}
public static void setDriver(WebDriver driver) {
Env.driver = driver;
}
}
钩类:
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.Scenario;
public class Hooks extends Env
{
//public static WebDriver driver;
Logger log = Logger.getLogger(Hooks.class);
Scenario scenario = null;
@Before
public void before(Scenario scenario) {
this.scenario = scenario;
Env.CreateWebDriver(getBrowserName());
}
@After
/**
* Embed a screenshot in test report if test is marked as failed
*/
public void afterMethodt(Scenario scenario) {
getDriver().quit();
}
}
当我运行跑步者类时,我得到了这个错误。
[31morg.openqa.selenium.remote.SessionNotFoundException:会话ID为空。调用quit()后使用WebDriver?构建信息:版本:'2.45.0',修订版:'5017cb8e7ca8e37638dc3091b2440b90a1d8686f',时间:'2015-02-27 09:10:26'系统信息:主机:'DESKTOP-235EN1N',ip:'192.168.0.105', os.name:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:'1.8.0_131'驱动程序信息:driver.version:RemoteWebDriver
你需要添加getDriver方法,如果driver = null返回一个新的驱动程序,则返回你第一次实例化的同一个驱动程序。
它可能会拉动多个新驱动程序而不是同一个驱动程序,因此不会保留数据。
你真正需要尝试的是用于DI的黄瓜皮克容器。这有点像Singleton的设计。