Created
October 11, 2012 19:35
-
-
Save paoloambrosio/3874974 to your computer and use it in GitHub Desktop.
Cucumber-Jvm WebDriver implementation switching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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