黄瓜平行试验

时间:2019-04-19 10:05:27

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

我目前正在尝试使用黄瓜进行并行测试。我使用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/"});
    }

}

工厂类

`导入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>

3 个答案:

答案 0 :(得分:0)

info.cukes依赖性(相当老),仅在黄瓜1.2.5之前支持,并且在2016年9月12日之后不再对此提供支持

另一方面,io.cucumber依赖性从v 2.0.x开始支持Cucumber,直到现在为止支持最新的v 4.3.x (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>

答案 1 :(得分:0)

通过使用main() method in class Main of package cucumber.api.cli,可以在不使用任何运行程序的情况下执行黄瓜功能文件。请参考此cli usagethis article。使用此也许它将起作用。不过需要一些更改。

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

  2. 需要通过硒和黄瓜代码访问为每个线程创建的驱动程序的方式进行一些更改。

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

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

5. 删除在DriverFactory中的关闭挂钩,BrowserStack仍将退出实际的驱动程序。

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

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

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

答案 2 :(得分:0)

您可能已经得到了这个问题的答案。要并行运行测试用例,需要进行以下链接中给出的其他设置- https://cucumber.io/docs/guides/parallel-execution/

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

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

@Before(订单=2) public void LaunchBrowser(Scenario sc) { String browser="firefox"; // 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);
    
}
相关问题