【问题标题】:Parallel Test run with cucumber用黄瓜进行并行测试
【发布时间】:2019-04-19 10:05:26
【问题描述】:

我目前正在尝试用黄瓜实现并行测试运行。我设法使用sure-fire插件同时运行两个不同的跑步者。现在我想检查是否可以并行运行 SingleRunner 文件多次。

例如:我有 SignUpRunnerTest.java,所以我需要在少数平台上并行运行它。这可能吗?

这是我的 Runner 文件

import cucumber.api.CucumberOptions;
import cucumber.api.cli.Main;
import cucumber.api.junit.Cucumber;

import java.util.List;

import javax.management.MXBean;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/html/", "json:target/cucumber.json", "junit:TEST-all.xml"},
        features = "src/test/java/resources/features/Search.feature", glue = {"com.browserstack.stepdefs"})
public class SignUpeRunnerTest {


}

没有跑步者方法

public class SignUpeRunnerTest {

    @Test
    public void test2() {
    Main.main(new String[]{"--threads", "4","-g", "com.browserstack.stepdefs", "src/test/java/resources/features/"});
    }

}

工厂类

`import org.openqa.selenium.WebDriver;

public final class DriverFactory {

    private static ThreadLocal<WebDriver> drivers = new ThreadLocal();

    //To quit the drivers and browsers at the end only. 
    private static List<WebDriver> storedDrivers = new ArrayList();

    static {
        Runtime.getRuntime().addShutdownHook(new Thread(){
            public void run(){
                storedDrivers.stream().forEach(WebDriver::quit);
            }
          });
    }

    private DriverFactory() {}

    public static WebDriver getDriver() {
        return drivers.get();
    }

    public static void addDriver(WebDriver driver) {
        storedDrivers.add(driver);
        drivers.set(driver);
    }

    public static void removeDriver() {
        storedDrivers.remove(drivers.get());
        drivers.remove();
    }   
}

`

步类

导入 org.openqa.selenium.By; 导入 org.openqa.selenium.WebDriver;

公共类 SearchPage { 私有静态 WebDriver webDriver;

public SearchPage(WebDriver webDriver) {
    this.webDriver = webDriver;
    DriverFactory.addDriver(webDriver);
}

private By searchTermField = By.name("q");
private By submitSearch = By.id("_fZl");

public void enterSearchTerm(String searchTerm) {
    DriverFactory.getDriver().findElement(searchTermField).sendKeys(searchTerm);
}

public void submitSearch() {
    DriverFactory.getDriver().findElement(submitSearch).click();
}

}

这是我的 POM 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.browserstack</groupId>
    <artifactId>cucumber-jvm-java-browserstack</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cucumber-jvm-java-browserstack</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber.jvm.parallel.version>2.2.0</cucumber.jvm.parallel.version>
        <surefire.maven.plugin.version>2.19.1</surefire.maven.plugin.version>
        <acceptance.test.parallel.count>4</acceptance.test.parallel.count>
    </properties>

    <dependencies>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.2.3</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>datatable</artifactId>
            <version>1.1.12</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>4.2.3</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>4.2.3</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.gfk.senbot/senbot-maven-plugin -->

  <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>4</threadCount>
                    <reuserForks>false</reuserForks>
                    <testErrorIgnore>true</testErrorIgnore>
                    <testFailureIgnore>true</testFailureIgnore>
                    <includes>
                        <include>**/*RunnerTest.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 很高兴有人能帮忙..谢谢
  • 有人吗?可以帮忙
  • 根据需要多次复制和重命名现有的运行器类文件。保留所有选项。现在你有多个相同的跑步者,只是名字不同。然后这变成了已经为你工作的情况。
  • 另外你应该看看更新黄瓜版本。你用的是很旧的。
  • @Grasshopper,谢谢你的回复,其实我不想重复所有的跑步者课程。相反,我想在 10 个不同的平台上并行测试一个运行器类。我实际上使用 Junit 和 Selenium 实现了这一点,但它在 runner 类中 reuiqre 我的 @Runwith(ParallelRunner.class) 但它已经被黄瓜 runner 类占用。我也被困在那里。

标签: cucumber cucumber-jvm maven-surefire-plugin


【解决方案1】:

info.cukes 依赖项(非常旧)仅在 v 1.2.5 之前支持 cucumber,并且在 2016 年 9 月 12 日之后不再支持此功能

另一方面,io.cucumber 依赖项支持从 v 2.0.x 到最新的 v 4.3.x 的 Cucumber(Cucumber-JVM 4.0 为您提供了很多实现并行执行的灵活性)以下是使用 io.cucumber 实现并行执行的步骤(这里您不需要为每个功能文件创建单独的运行器,也不需要使用任何旧插件,如 jvm-parallel 插件)

1.添加正确的依赖集。我在实现过程中一直关注 JUnit。

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>4.2.3</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.3</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>datatable</artifactId>
    <version>1.1.12</version>
</dependency>


<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.3</version>
    <scope>test</scope>
</dependency>

2.在POM.XML下添加Maven-Surefire-Plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <parallel>methods</parallel>
        <threadCount>1</threadCount>
        <reuserForks>false</reuserForks>
        <testErrorIgnore>true</testErrorIgnore>   
        <testFailureIgnore>true</testFailureIgnore>
        <includes>
            <include>**/*RunCukeTest.java</include>
        </includes>
    </configuration>
</plugin>

【讨论】:

  • 感谢您的回复。我希望在 Pharrell 中多次运行单个运行程序文件。因为我的要求是在不重复创建跑步者的情况下运行并行设备相同的功能。
  • 是的,对于这个实现,您将使用单个运行器实现并行执行,因此不需要像 info.cukes 中所要求的那样有多个运行器。
  • 我刚试过。仍在启动单个线程。我使用 eclipse 右键单击​​并以 junit 身份运行,并尝试运行 pom 文件。仍然是同一个线程,没有为我创建多个会话。
  • 你在你的框架下使用TestNG吗?因为 testNG 依赖导致 Surefire 忽略 JUnit 包装类。删除所有 TestNG 依赖项,或者您可以调用 2 定义 2 执行 - 对于 TestNG 和 JUnit,并根据您的需要禁用一个,我使用的是 Maven Surefire 版本 3.0.0-M3
  • 不,我不使用 TestNG。它只有 Junit + cucumber。实际上,我用 Junit 和 selenium 做了一个 Rnd,但它需要替换 @RunWith 注释,但我不能用黄瓜跑步者类已经使用它。
【解决方案2】:

使用main() method in class Main of package cucumber.api.cli 可以在不使用任何运行器的情况下执行 Cucumber 功能文件。请参阅此cli usagethis article。使用这个也许它会起作用。虽然需要一些改变。

  1. BrowserStackJUnitTest 的代码复制到一个新类中,将其重命名为NewBSTest。这个类将用于运行测试。

  2. 需要对 selenium 和 cucumber 代码访问为每个线程创建的驱动程序的方式进行一些更改。

  3. 需要使用工厂将驱动程序存储在ThreadLocal 变量NewBSTest 中。你可以参考这个class

  4. 在您创建的 NewBSTest 类中删除 第 30 行 - public WebDriver driver;。更改第 94 行,将创建​​的 RemoteWebDriver 添加到 ThreadLocal 变量中。像DriverFactory.addDriver(driver) 这样的东西。在方法tearDown()第98行中添加DriverFactory.removeDriver(),可能是第一行。改变现有的 driver.quit();到DriverFactory.getDriver().quit()

5.删除 DriverFactory 中的关闭钩子,BrowserStack 无论如何都会退出实际的驱动程序。

  1. 现在在 selenium 或 cucumber 代码中,您可以使用 DriverFactory.getDriver() 访问驱动程序。这将严重影响您现有的代码。

  2. NewBSTest 类中添加这样的测试方法。希望这将起作用,并且相同的功能将在每个配置的浏览器堆栈环境中执行。

@Test
public void test() {
Main.main(new String[]{""-g", "stepdef", "src/test/resources/features/"});
}

【讨论】:

  • 我刚刚尝试过,但它对我不起作用。我仍然看到从我的运行程序类 public void test() { Main.main(new String[]{"--threads", "4","-g", "com.browserstack.stepdefs", “src/test/java/resources/features/”}); }
  • 你现在不需要使用任何跑步者。此测试方法进入新类。还要从命令中删除线程选项。你可以在问题中添加新的课程代码吗?
  • 这个 rnd 我确实按预期工作,现在我想将它与黄瓜集成。但是,如果您对此有任何想法,我不能使用@Runwith,这会很棒drive.google.com/open?id=1Kuq1IW5fIqiSHdXXn-aLAAsaUCpTIwdy
  • 不确定我是否清楚了解您的要求。如果您使用 RunWith 或 main 方法运行几乎没有关系。也不要认为你已经正确实现了上述内容,或者你上传的代码丢失了一些东西。
  • 好的,我可以知道那里缺少什么吗?实际上,我的要求是并行运行一个运行程序文件(Ed:SignUpRunner(3 个功能)所以我需要在 3 个设备中并行运行这个 SignUp 运行程序。运行程序内的场景可以按顺序运行,但我需要启动运行程序并行。
【解决方案3】:

你可能已经得到了这个问题的答案。要并行运行测试用例,需要额外的设置,如下面的链接所示 - https://cucumber.io/docs/guides/parallel-execution/

根据此链接,如果您使用的是surefire插件,则包名应该与运行器类平行。

为了选择设备类型,您可以为功能文件添加标签,并在钩子之前进一步获取标签。我认为选择功能可能有更好的方法可用。以下逻辑对我有用-

@Before(order=2) public void LaunchBrowser(Scenario sc) { 字符串浏览器=“火狐”; // ArrayList s = (ArrayList) sc.getSourceTagNames();

    //scenarios having this tag will be ignored and not run
    if(s.contains("@chrome"))
        browser = "chrome";
    else if(s.contains("@firefox"))
        browser = "firefox";

    driverfactory=new DriverFactory();
    driver=driverfactory.init_driver(browser);
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多