Cross browser testing in cucumber

亡梦爱人 提交于 2019-12-02 07:45:45

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

  1. Changes to 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);
  }
  1. 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.

  2. 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.

  3. 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!