Skip to content
October 18, 2012 / selenium34

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 {

  private WebDriver driver;
  private JQueryHomePage homePage;

  public void setup() {
    homePage = new JQueryHomePage(driver);

  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));

  public void shutdown() {

Now we need to setup our configuration files. Here is what our Firefox class looks like:

public class Firefox {
  public WebDriver driver() {
    return new FirefoxDriver();

And the Chrome class:

@Profile({ "chrome", "default" })
public class Chrome { 
  public WebDriver driver() {
    System.setProperty("", "chromedriver.exe");
    return new ChromeDriver();

And the InternetExplorer class:

public class InternetExplorer {
  public WebDriver driver() {
    System.setProperty("", "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 “” we will run with Internet Explorer. If we add “”, 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.



Leave a Comment
  1. Taras Lytvyn / Jun 9 2014 10:36 am

    CAn you share full code example, please ??

  2. Taras Lytvyn / Jun 10 2014 9:07 pm

    Actually need example with Spring and bean xml configuration file…


  1. A Smattering of Selenium #124 « Official Selenium Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: