如何并行执行黄瓜特征文件 [英] How to execute cucumber feature file parallel

查看:34
本文介绍了如何并行执行黄瓜特征文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 src/test/resources/feature/中有以下功能文件(单独的功能文件),我想并行运行它们.如:一个功能文件必须在 chrome 中执行,另一个必须在 firefox 中执行,如提到的@Tags 名称.

I have below feature files (Separate feature files) in src/test/resources/feature/ and I would like to run them in parallel. Like: One feature file has to execute in chrome and another one has to execute in firefox as mentioned @Tags name.

Feature: Refund item

@chrome
  Scenario: Jeff returns a faulty microwave
    Given Jeff has bought a microwave for $100
    And he has a receipt
    When he returns the microwave
    Then Jeff should be refunded $100

Feature: Refund Money

@firefox
  Scenario: Jeff returns the money
    Given Jeff has bought a microwave for $100
    And he has a receipt
    When he returns the microwave
    Then Jeff should be refunded $100

有人可以帮助我实现这一点吗?我使用的是cucumber-java 1.2.2 版本,AbstractTestNGCucumberTests 用作跑步者.另外,请告诉我如何使用功能文件动态创建测试运行器并使它们并行运行.

Can somebody assist me to achieve this.I'm using cucumber-java 1.2.2 version, and AbstractTestNGCucumberTests using as runner. Also, let me know how can I create a Test Runner dynamically by using feature files and make them run in parallel.

推荐答案

更新: 4.0.0 版本在 maven 中央存储库中可用,并进行了大量更改.欲知更多详情,请访问这里.

Update: 4.0.0 version is available at maven central repository with bunch of changes.for more details go here.

更新: 2.2.0 版本在 maven 中央存储库可用.

Update: 2.2.0 version is available at maven central repository.

您可以使用开源插件 cucumber-jvm-parallel-plugin与现有解决方案相比具有许多优势.可在 maven 存储库

You can use opensource plugin cucumber-jvm-parallel-plugin which has many advantages over existing solutions. Available at maven repository

   <dependency>
     <groupId>com.github.temyers</groupId>
     <artifactId>cucumber-jvm-parallel-plugin</artifactId>
     <version>2.1.0</version>
   </dependency>

  1. 首先你需要在你的项目 pom 文件中添加这个具有所需配置的插件.

  1. First you need to add this plugin with required configuration in your project pom file.

<plugin>
  <groupId>com.github.temyers</groupId>
  <artifactId>cucumber-jvm-parallel-plugin</artifactId>
  <version>2.1.0</version>
  <executions>
     <execution>
     <id>generateRunners</id>
     <phase>generate-test-sources</phase>
     <goals>
       <goal>generateRunners</goal>
     </goals>
     <configuration>
         <!-- Mandatory -->
         <!-- comma separated list of package names to scan for glue code -->
         <glue>foo, bar</glue>
         <outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory>
          <!-- The directory, which must be in the root of the runtime classpath, containing your feature files.  -->
           <featuresDirectory>src/test/resources/features/</featuresDirectory>
          <!-- Directory where the cucumber report files shall be written  -->
          <cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
          <!-- comma separated list of output formats json,html,rerun.txt -->
          <format>json</format>
          <!-- CucumberOptions.strict property -->
          <strict>true</strict>
          <!-- CucumberOptions.monochrome property -->
          <monochrome>true</monochrome>
          <!-- The tags to run, maps to CucumberOptions.tags property you can pass ANDed tags like "@tag1","@tag2" and ORed tags like "@tag1,@tag2,@tag3" -->
         <tags></tags>
         <!-- If set to true, only feature files containing the required tags shall be generated. -->
         <filterFeaturesByTags>false</filterFeaturesByTags>
         <!-- Generate TestNG runners instead of default JUnit ones. --> 
         <useTestNG>false</useTestNG>
         <!-- The naming scheme to use for the generated test classes.  One of 'simple' or 'feature-title' --> 
        <namingScheme>simple</namingScheme>
        <!-- The class naming pattern to use.  Only required/used if naming scheme is 'pattern'.-->
        <namingPattern>Parallel{c}IT</namingPattern>
        <!-- One of [SCENARIO, FEATURE]. SCENARIO generates one runner per scenario.  FEATURE generates a runner per feature. -->
        <parallelScheme>SCENARIO</parallelScheme>
        <!-- This is optional, required only if you want to specify a custom template for the generated sources (this is a relative path) -->
        <customVmTemplate>src/test/resources/cucumber-custom-runner.vm</customVmTemplate>
        </configuration>
       </execution>
     </executions>
   </plugin>

  • 现在在插件上面添加下面的插件,这将调用上面插件生成的运行器类

  • Now add below plugin just below above plugin which will invoke runner classes generated by above plugin

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration> 
                <forkCount>5</forkCount>
                <reuseForks>true</reuseForks>
                <includes>
                    <include>**/*IT.class</include>
                </includes>
            </configuration>
        </plugin>
    

  • 以上两个插件将为并行运行的黄瓜测试发挥神奇作用(前提是您的机器还具有高级硬件支持).

  • Above two plugins will do magic for cucumber test running in parallel (provided you machine also have advanced hardware support).

    严格提供 n</forkCount> 此处的n"与 1) 高级硬件支持和 2) 您可用的节点成正比,即向 HUB 注册的浏览器实例.

    Strictly provided <forkCount>n</forkCount> here 'n' is directly proportional to 1) Advanced Hardware support and 2) you available nodes i.e. registered browser instances to HUB.

    一个主要和最重要的变化是你的 WebDriver 类必须SHARED,你应该不要实现 driver.quit() 方法,因为关闭是要小心通过关闭钩子.

    One major and most important changes is your WebDriver class must be SHARED and you should not implement driver.quit() method, as closing is take care by shutdown hook.

    import cucumber.api.Scenario;
    import cucumber.api.java.After;
    import cucumber.api.java.Before;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;
    
    public class SharedDriver extends EventFiringWebDriver {
        private static WebDriver REAL_DRIVER = null;
    
    
    
        private static final Thread CLOSE_THREAD = new Thread() {
            @Override
            public void run() {
                REAL_DRIVER.close();
            }
        };
    
        static {
            Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
        }
    
        public SharedDriver() {
            super(CreateDriver());
        }
    
        public static WebDriver CreateDriver() {
            WebDriver webDriver;
            if (REAL_DRIVER == null)
                webDriver = new FirefoxDriver();
            setWebDriver(webDriver);
            return webDriver;
        }
    
        public static void setWebDriver(WebDriver webDriver) {
            this.REAL_DRIVER = webDriver;
        }
    
        public static WebDriver getWebDriver() {
            return this.REAL_DRIVER;
        }
    
        @Override
        public void close() {
            if (Thread.currentThread() != CLOSE_THREAD) {
                throw new UnsupportedOperationException("You shouldn't close this WebDriver. It's shared and will close when the JVM exits.");
            }
            super.close();
        }
    
        @Before
        public void deleteAllCookies() {
            manage().deleteAllCookies();
        }
    
        @After
        public void embedScreenshot(Scenario scenario) {
            try {
                byte[] screenshot = getScreenshotAs(OutputType.BYTES);
                scenario.embed(screenshot, "image/png");
            } catch (WebDriverException somePlatformsDontSupportScreenshots) {
                System.err.println(somePlatformsDontSupportScreenshots.getMessage());
            }
        }
    }
    

  • 考虑到您要执行 50 个以上的线程,即相同的浏览器实例数不会注册到 HUB,但如果没有足够的内存,Hub 就会死掉,因此为避免这种危急情况,您应该使用 -DPOOL_MAX 启动 hub=512(或更大),如 grid2 文档中所述.

    非常大(>50 个节点)的集线器安装可能需要通过在 java 命令行上设置 -DPOOL_MAX=512(或更大)来增加码头线程.

    java -jar selenium-server-standalone-.jar -role hub -DPOOL_MAX=512

    这篇关于如何并行执行黄瓜特征文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆