Changing Browsers without changing Code
I stated in our last post that we would be putting our code up on GitHub soon… I apologize for that not happening, but I really wanted to get this out in the open before we added our bits to GitHub. As usual that did not happen in a timely manner. For this I apologize.
We are introducing two concepts from the Spring Framework in this post. Spring is a bolt on product for Java development, which is typically used in enterprise applications. We will be using two new features of the framework. Namely, Java configuration and profiles. To ease this transition, I am switching from JUnit tests to TestNG. This is due to JUnit @Before & @After methods needing to be static. Spring cannot inject static values.
Below is our updated class (I’ve removed all but one of the tests to shorten the post):
@ContextConfiguration(classes = { Firefox.class, InternetExplorer.class, Chrome.class })
public class JQueryHomePageTest extends AbstractTestNGSpringContextTests {
@Resource
private WebDriver driver;
private JQueryHomePage homePage;
@BeforeClass
public void setup() {
homePage = new JQueryHomePage(driver);
}
@Test
public void getText() {
//Chrome and Firefox will include a \n in the text representation, whereas IE will not
final String text = StringUtils.replaceEach(homePage.getTagline().getText(), new String[] { "\r", "\n" }, new String[] { " ", " " });
Assert.assertTrue("jQuery is a new kind of JavaScript Library.".equalsIgnoreCase(text));
}
@AfterClass
public void shutdown() {
driver.quit();
}
}
Now we need to setup our configuration files. Here is what our Firefox class looks like:
@Configuration
@Profile("firefox")
public class Firefox {
@Bean
public WebDriver driver() {
return new FirefoxDriver();
}
}
And the Chrome class:
@Configuration
@Profile({ "chrome", "default" })
public class Chrome {
@Bean
public WebDriver driver() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
return new ChromeDriver();
}
}
And the InternetExplorer class:
@Configuration
@Profile("ie")
public class InternetExplorer {
@Bean
public WebDriver driver() {
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
return new InternetExplorerDriver();
}
}
If we run the test with no system parameters, we will utilize Chrome as our browser. If we add the following parameter to our run configuration “-Dspring.profiles.active=ie” we will run with Internet Explorer. If we add “-Dspring.profiles.active=firefox”, we will run with Firefox. This is a nice and easy way to modify our tests to run in a different browser, without having to modify code. Hopefully this is easily extensible for your environment. Need to run against Sauce Labs or Testing Bot? Create a profile(s) for each browser. Need to run against an internal grid? Create a profile(s) for each browser. If you can get this baked into your continuous integration server, you should feel pretty confident that your tests are behaving as expected in all browsers.

CAn you share full code example, please ??
The very next post had the link to the repo on github which contained the custom page factory and the browser configurations: https://github.com/selenium34/custom-page-factory
Actually need example with Spring and bean xml configuration file…
That project is Spring based… They are using the new(er) Java configuration.
Those beans would be quite easy to convert to XML files. Here is an example, from which you can convert those to XML:
http://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles/