Back to Resources

Blog

Posted October 22, 2010

Parallel JUnit 4 and Selenium – Part Two: External Properties

quote

In the first part of this series, we saw how to make use of the Parameterized runner that comes with JUnit 4 to execute our tests across multiple browsers. But that came with two penalties. The increasing test duration will be addressed in part three, but today we'll address how to modify the browsers used without having to recompile our test.As someone who sees a lot of different teams' Selenium code, I'm appalled at how often a simple change, such as adjusting which browser(s) to run against, requires a recompile. To me, that is a nasty code smell. Things that do not materially affect the purpose of the script and are often changing should be externalized out of the code. In our case, we are going to use a Properties file inside the @Parameters method. Nothing else in the script has changed.

@Parameters public static LinkedList browsersStrings() throws Exception { LinkedList browsers = new LinkedList();

InputStream is = TestProperties.class.getResourceAsStream("/environment.properties"); environmentProps.load(is);

String[] rawBrowserStrings = environmentProps.getProperty("browsers").split(","); for (String rawBrowserString : rawBrowserStrings) { browsers.add(new String[] { rawBrowserString }); } return browsers; }

Nothing special here, just standard loading of a properties file that has a browsers key, which is a comma separated list of allowable Selenium browser strings. Now the same compiled code will run different browsers without actually change the Java code. The result is much nicer, but still has to address the problem of a massive increase in execution. That will be the subject of part three of this series.To see the full source code the snippet was pulled from, see TestProperties.java

Published:
Oct 22, 2010
Topics
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.