Reputation: 78
I want to run a feature file in three different browsers parallely. How can I create three different instances of webdriver? and please add How to do the same for two different feature files?
P.S. I am working on cucumber with java.
Upvotes: 1
Views: 5721
Reputation: 9058
When you mention "three different browsers in parallel" do you mean like Chrome, Firefox and explorer running in parallel OR three different instances of Chrome only.
If it is the second case refer to this article - https://opencredo.com/running-cucumber-jvm-tests-in-parallel/. The basic logic behind this is forking into separate JVM's for the number of parallel instances required. This is done using Maven surefire plugin.
If the first case then you will need to modify certain aspects. The plugin passes the 'fork number' to the JVM which you can use to instantiate the specific browser driver for that JVM.
I am using Java 8, junit 4.12, picocontainer for Dependency Injection, Maven 3
(will not work with lower versions as 'fork number' passes null), selenium 2.53
(u need to figure out code changes for Selenium 3 if required in driver creation) and browsers Chrome and Firefox
(you need to add code for IE).
Code
ShareDriver.java
from article github -
Overwrite the static block with this and add other methods.static { instantiateDriver(); Runtime.getRuntime().addShutdownHook(CLOSE_THREAD); } private static void instantiateDriver() { //numFork will be passed in the maven command line or eclipse //--- clean install -DnumFork=${surefire.forkNumber} int browserType = Integer.parseInt(System.getProperty("numFork")); System.out.println("BROWSER TYPE "+browserType); if(browserType == 1) instantiateChromeDriver(); else if (browserType == 2) instantiateFirefoxDriver(); else if (browserType == 3) { instantiateIEDriver(); } } private static void instantiateIEDriver() { //Implement this } private static void instantiateFirefoxDriver() { REAL_DRIVER = new FirefoxDriver(); REAL_DRIVER.manage().window().maximize(); } private static void instantiateChromeDriver() { System.setProperty("webdriver.chrome.driver", "location of chromedriver.exe"); ChromeOptions chop = new ChromeOptions(); chop.addArguments("test-type"); chop.addArguments("start-maximized"); chop.addArguments("--disable-extensions"); REAL_DRIVER = new ChromeDriver(chop); }
Changes to feature files
- Remove the tags. Change one of the feature files to contain some other steps. Code the new steps in step definition class.
Changes to runner classes
- Remove the tag option from cucumberoptions, you may need to add glue option to point to your step definition class. So effectively both the runner classes are the same. You can remove one of the runner classes if you want. Else tests will be repeated twice in each browser.
pom.xml
- Make sure that the number of forks defined (<surefire.fork.count>5</surefire.fork.count>
) is more than the number of browsers you are using. Else the logic will fail.
Running in Maven -- You can run from eclipse plugin using command clean install -DnumFork=${surefire.forkNumber}
in goals option. Or from command line using mvn clean install -DnumFork=${surefire.forkNumber}
.
This should run all the scenarios in all the feature files in all the browser instances. You can refine the cucumberoptions such as tags and feature to run specific scenarios or feature files.
Upvotes: 2
Reputation: 4323
I would choose to execute the build three times. I would set an environment variable for each execution that decides which browser to use.
To run them in parallel, I would write a shell script that that sets the environment variable and start three executions.
Upvotes: 2