Created
December 3, 2012 09:46
-
-
Save nicegraham/4193926 to your computer and use it in GitHub Desktop.
Running JBehave in parallel
This file contains hidden or 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
| import org.openqa.selenium.Capabilities; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import java.net.URL; | |
| import java.util.concurrent.TimeUnit; | |
| public final class DriverFactory { | |
| private RemoteWebDriver remoteWebDriver = null; | |
| private URL url = null; | |
| private Capabilities capabilities = null; | |
| private static final ThreadLocal<DriverFactory> DRIVER_FACTORY_STORAGE = new ThreadLocal<DriverFactory>() { | |
| protected DriverFactory initialValue() { | |
| return new DriverFactory(); | |
| } | |
| }; | |
| private DriverFactory() { | |
| } | |
| public static DriverFactory getDriverFactory() { | |
| return DRIVER_FACTORY_STORAGE.get(); | |
| } | |
| public RemoteWebDriver getRemoteWebDriver() { | |
| synchronized (this) { | |
| try { | |
| if (this.remoteWebDriver == null) { | |
| this.remoteWebDriver = new RemoteWebDriver(getURL(), getCapabilities()); | |
| this.remoteWebDriver.manage().timeouts().implicitlyWait(50, TimeUnit.MILLISECONDS); | |
| this.remoteWebDriver.manage().timeouts().setScriptTimeout(120, TimeUnit.SECONDS); | |
| } | |
| } catch (Exception e) { | |
| System.out.println("Could not start Webdriver."); | |
| } | |
| return this.remoteWebDriver; | |
| } | |
| } | |
| public void closeDriver() { | |
| System.out.println("Closing RemoteWebDriver"); | |
| if (remoteWebDriver != null) { | |
| remoteWebDriver.quit(); | |
| remoteWebDriver = null; | |
| } | |
| } | |
| private void setCapabilities(Capabilities c) { | |
| synchronized (this) { | |
| capabilities = c; | |
| } | |
| } | |
| private Capabilities getCapabilities() { | |
| synchronized (this) { | |
| return capabilities; | |
| } | |
| } | |
| private void setUrl(URL u) { | |
| synchronized (this) { | |
| url = u; | |
| } | |
| } | |
| private URL getURL() { | |
| synchronized (this) { | |
| return url; | |
| } | |
| } | |
| public void init(URL u, Capabilities c) { | |
| setUrl(u); | |
| setCapabilities(c); | |
| } | |
| } |
This file contains hidden or 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 MyStoryRunner extends StoryRunner { | |
| private String browser; | |
| private boolean standaloneMode; | |
| private String targetOS; | |
| private int sessionID; | |
| private String rackName; | |
| private String testrunId; | |
| private boolean proxyRequired; | |
| public MyStoryRunner(String browser, boolean standaloneMode, String targetOS, int sessionID, String rackName, String testrunId, boolean proxyRequired) { | |
| this.browser = browser; | |
| this.standaloneMode = standaloneMode; | |
| this.targetOS = targetOS; | |
| this.sessionID = sessionID; | |
| this.rackName = rackName; | |
| this.testrunId = testrunId; | |
| this.proxyRequired = proxyRequired; | |
| } | |
| @Override | |
| public void run(Configuration configuration, InjectableStepsFactory stepsFactory, Story story, MetaFilter filter, State beforeStories) throws Throwable { | |
| try { | |
| DriverFactory driverFactory = DriverFactory.getDriverFactory(); | |
| DesiredCapabilities cap = ...; | |
| // set capabilities based on inputs to class... | |
| driverFactory.init(new URL(hubUrl), cap); | |
| RemoteWebDriver driver = driverFactory.getRemoteWebDriver(); | |
| super.run(configuration, stepsFactory, story, filter, beforeStories); | |
| } catch (Exception e) { | |
| System.out.println("problem in runner"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would have done this differently. I would have made MyStoryRunner a JUnit test and then forked it in parallel using Gradle maxParallelForks runner in my Gradle task.