Skip to content

Instantly share code, notes, and snippets.

@paoloambrosio
Created October 11, 2012 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paoloambrosio/3874974 to your computer and use it in GitHub Desktop.
Save paoloambrosio/3874974 to your computer and use it in GitHub Desktop.
Cucumber-Jvm WebDriver implementation switching
public class Hooks {
@Autowired
private WebDriverFactory webDriverFactory;
@Before("@javascript")
public void setJavascriptWebDriver() {
webDriverFactory.selectJavascriptDriver();
webDriverFactory.getCurrentWebDriver().manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
}
@Before("~@javascript")
public void setNoJavascriptWebDriver() {
webDriverFactory.selectDefaultDriver();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean name="default-webdriver" class="org.openqa.selenium.htmlunit.HtmlUnitDriver" lazy-init="true" />
<bean name="javascript-webdriver" class="org.openqa.selenium.firefox.FirefoxDriver" destroy-method="close" lazy-init="true" />
</beans>
@Component
public class WebDriverFactory implements ApplicationContextAware {
private ApplicationContext applicationContext;
private WebDriver currentWebDriver;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void selectJavascriptDriver() {
currentWebDriver = getJsWebDriver();
}
public void selectDefaultDriver() {
currentWebDriver = getDefaultWebDriver();
}
public WebDriver getCurrentWebDriver() {
return currentWebDriver;
}
private WebDriver getJsWebDriver() {
return applicationContext.getBean("javascript-webdriver", WebDriver.class);
}
private WebDriver getDefaultWebDriver() {
return applicationContext.getBean("default-webdriver", WebDriver.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment