Добрый день.
У меня возникла проблема при запуске одного теста в двух разных браузерах. Java+TestNG
При запуске теста в параллельном режиме я получаю ошибку The FirefoxDriver cannot be used after quit() was called.
WebDriver у меня статический
Надо ли использовать какой-то специальный класс обертку или достаточно убрать статичность в классе инициализации драйвера
public enum BrowserProvider {
firefox,
chrome,
iexplorer,
}
public class BaseTest {
public static WebDriver driver = null;
@Parameters({"browser"})
@BeforeTest(alwaysRun = true)
public void setUp(String browser) throws MalformedURLException, FileNotFoundException {
SingletonInstance.browser = BrowserProvider.valueOf(browser);
driver = SingletonInstance.getInstance();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
}
@AfterTest(alwaysRun = true)
public void tearDown() throws MalformedURLException {
driver.quit();
//driver.close();
}
}
public class SingletonInstance {
static FactoryWebDriver factory = new FactoryWebDriver();
public static BrowserProvider browser = BrowserProvider.firefox;
private static WebDriver instance = null;
private SingletonInstance(){}
public static WebDriver getInstance() throws MalformedURLException {
if (instance == null) {
try {
instance = factory.createWebdriver(browser);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return instance;
}
public class FactoryWebDriver {
public WebDriver driver = null;
public WebDriver createWebdriver(final BrowserProvider browser) throws FileNotFoundException, MalformedURLException {
String remote = PropertyParser.getPropValues("remote");
if(remote.equalsIgnoreCase("true")) {
return createRemoteWebDriver(browser);
}
switch (browser) {
case firefox:
driver = new FirefoxDriver();
break;
case chrome:
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\chromedriver.exe");
driver = new ChromeDriver();
break;
case iexplorer:
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + "\\drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
break;
default:
System.out.print("Is not a valid parameter");
break;
}
return driver;
}
protected WebDriver createRemoteWebDriver(final BrowserProvider browser) throws MalformedURLException {
DesiredCapabilities capabilities = null;
switch(browser){
case firefox:
capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(Platform.WINDOWS);
break;
case chrome:
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe");
//capabilities = DesiredCapabilities.chrome();
capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
break;
case iexplorer:
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + "\\drivers\\IEDriverServer.exe");
capabilities = DesiredCapabilities.internetExplorer();
break;
default:
System.out.print("Is not a valid parameter");
break;
}
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capabilities);
return driver;
}
}