2016-12-08 83 views
3

我在src/test/resources/feature /中有以下功能文件(獨立的功能文件),我想並行運行它們。就像:一個功能文件必須在Chrome中執行,另一個功能文件必須在firefox中執行@Tags名稱。使用作爲亞軍AbstractTestNGCucumberTests如何執行並行的黃瓜功能文件

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 

有人可以幫助我實現this.I'm黃瓜使用的Java版本1.2.2,和。另外,讓我知道如何通過使用功能文件動態創建Test Runner並使它們並行運行。

+0

什麼是你想在這裏實現?什麼是最終目標? – k1133

+0

我想與不同的瀏覽器並行執行功能文件。我有一個應用程序,我需要做瀏覽器兼容性測試。到目前爲止,我們已經執行序列來驗證瀏覽器的兼容性。我剛剛聽說了關於cucumber-jvm-parallel插件,但沒有太多知道如何實現它。並且聽說它會動態地創建一個跑步者,並幫助我們在場景或功能方面執行並行執行。 – ArrchanaMohan

+0

你可以試試maven和surefire插件。它將使用多個線程來分別運行每個可用的運行器。谷歌cucmber的Java Maven並行。用於鏈接文章,但不知何故被刪除 – Grasshopper

回答

6

更新: 4.0.0版本可在maven中央存儲庫進行一系列更改。 for more details go here.

更新: 2.2.0版本可在maven中央存儲庫中找到。

您可以使用開源插件cucumber-jvm-parallel-plugin,它比現有解決方案有許多優點。可在行家repository

<dependency> 
    <groupId>com.github.temyers</groupId> 
    <artifactId>cucumber-jvm-parallel-plugin</artifactId> 
    <version>2.1.0</version> 
    </dependency> 
  1. 首先,你需要添加這個插件與所需的配置項目中的POM文件。

    <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> 
    
  2. 現在,添加以下插件僅低於上述插件,它會調用由上述插件

    <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> 
    
  3. 以上兩個插件將在並行運行(黃瓜測試使用魔法產生的亞軍類提供你機器還擁有先進的硬件支持)。

  4. 嚴格規定<forkCount>n</forkCount>此處'n'與1)高級硬件支持和2)您可用的節點,即註冊的瀏覽器實例到HUB成正比。

  5. 一個主要的和最重要的變化是你的webdriver類必須SHARED,你應該實現driver.quit()方法,如關閉通過關閉掛鉤照顧。

    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()); 
         } 
        } 
    } 
    
  6. 考慮要執行超過50個線程的IE瀏覽器實例相同的,註冊到HUB,但如果它沒有得到足夠的內存,因此避免你應該開始樞紐這一危局中心會死如grid2 documentation中所述,-DPOOL_MAX = 512(或更大)。

    Really large (>50 node) Hub installations may need to increase the jetty threads by setting -DPOOL_MAX=512 (or larger) on the java command line.

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

+0

非常感謝sugat Mankar的投入。它非常有用。 – ArrchanaMohan

+0

我真的不明白。爲什麼我需要添加'cucumber-jvm-parallel-plugin',如果它仍然需要在'maven-surefire-plugin'中指定叉子? – olyv

4

黃瓜不支持開箱即用的並行執行。 我試過了,但它不友善。

  1. 我們必須使用maven的能力來並行調用它。請參閱link
  2. 此外還有一個github項目,它使用自定義插件並行執行。 參考cucumber-jvm-parallel-plugin
+0

謝謝bharath kumar。它有很多幫助。 – ArrchanaMohan

+0

這似乎是將平行黃瓜json輸出文件整理成一個報告的方式:https://github.com/rajatthareja/ReportBuilder。另外,對於Ruby來說,將parallel_cucumber模塊放入Rakefile似乎並行運行並沒有問題。 – djangofan

1

如果你期待的是能夠並行運行多個功能,那麼你可以嘗試做如下:

由於從TestNG的默認dataprovider-thread-count10現在你已指示TestNG的並行運行features,你應該能看到自己的特徵文件會並行執行。

但我明白,黃瓜報告本質上不是線程安全的,所以你的報告可能會出現亂碼。

+0

感謝您的輸入krishnana mahadevan。 – ArrchanaMohan