有没有办法对所有黄瓜测试使用一个示例文件?

Is there a way to use a single example file for all cucumber test?

我们目前正在使用多个示例,但它们都包含相同的示例。我们正在寻找一种在所有测试中使用相同示例的方法,而无需在每个功能文件中明确定义它们,因为它们仅用于描述我们需要测试的设备。由于我们的黄瓜事件,我们不能使用 qaf。以下是 are 测试的示例。

WAS_dev.feature

@dev
Feature: Do activations show on desktop in dev

  Scenario Outline: Do activations show in <os> <osVersion> on <browser>
    Given The make OS is "<os>" the version is "<osVersion>" and the browser is "<browser>" in dev on WAS
    When I visit activation "34" on dev
    And Click the play button on dev
    Then The Activation Should Show
    Then I Should be able to click the activation

    Examples:
      | os      | osVersion      | browser  |
      | Windows | 10             | Chrome   |
      | Windows | 10             | Firefox  |
      | Windows | 10             | Edge     |
      | OS X    | Big Sur        | Chrome   |
      | OS X    | Big Sur        | Edge     |
      | OS X    | Big Sur        | Firefox  |
      | OS X    | Big Sur        | Safari   |

WASDevSteps.java

package endtoendtest.steps.dev;

import endtoendtest.poms.dev.WASDev;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.testproject.sdk.drivers.web.RemoteWebDriver;
import org.junit.Assert;

import java.util.concurrent.TimeUnit;

import static endtoendtest.helpers.CreateBrowserOptions.createDriver;
import static endtoendtest.plugins.teams.helpers.browserstack.BrowserStackRequest.setScenario;

public class WASDevSteps {

    private Scenario scenario;

    private RemoteWebDriver driver;

    private WASDev wasDev;

    @Before
    public void before(Scenario scenario){
        this.scenario = scenario;
        setScenario(scenario);
    }

    @Given("The make OS is {string} the version is {string} and the browser is {string} in dev on WAS")
    public void theMakeOSIsTheVersionIsAndTheBrowserIs(String os, String osVersion, String browser) throws Exception {
        driver = createDriver(scenario, "desktop", os, osVersion, browser);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @When("I visit activation {string} on dev")
    public void visitPage(String activation){
        wasDev = new WASDev(driver, activation);
    }

    @And("Click the play button on dev")
    public void clickPlay(){
        wasDev.clickPlay();
    }

    @Then("The Activation Should Show")
    public void verifyActivationShows(){
        Assert.assertTrue(wasDev.ActivationIsShowing());
    }

    @Then("I Should be able to click the activation")
    public void clickActivation(){
        wasDev.clickActivation();
    }

}

WASDev.java

package endtoendtest.poms.dev;

import io.testproject.sdk.drivers.web.RemoteWebDriver;
import org.junit.Assert;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class WASDev {

    private RemoteWebDriver driver;

    @FindBy(how = How.CSS, using = "[aria-label='Play']")
    private WebElement playButton;

    @FindBy(how = How.XPATH, using = "//span[contains(text(), 'More Jourdan Dunn Here')]")
    private WebElement mobileActivation;

    @FindBy(how = How.XPATH, using = "//p[contains(text(), 'More Jourdan Dunn Here')]")
    private WebElement desktopActivation;

    public WASDev(RemoteWebDriver driver, String activation){
        String url = "https://areDevOrg.io";
        this.driver = driver;
        url = url + activation;
        driver.get(url);
        PageFactory.initElements(driver, this);
    }

    public void clickPlay(){
        playButton.click();
    }

    public boolean ActivationIsShowing(){
        Dimension screenSize = driver.manage().window().getSize();
        if(screenSize.getHeight() > screenSize.getWidth()){
            return mobileActivation.isDisplayed();
        } else {
            return desktopActivation.isDisplayed();
        }
    }

    public void clickActivation(){
        Dimension screenSize = driver.manage().window().getSize();
        if(screenSize.getHeight() > screenSize.getWidth()){
            mobileActivation.click();
        } else {
            desktopActivation.click();
        }
    }
}

TeamsAPI.java

package endtoendtest.plugins.teams;

import endtoendtest.plugins.teams.adaptivecards.CreateMessage;
import endtoendtest.plugins.teams.objects.TestResult;
import endtoendtest.plugins.teams.helpers.testproject.TestProjectRequest;

import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.*;

import java.io.IOException;
import java.net.URI;

import static endtoendtest.plugins.teams.helpers.TeamsRequest.postMessage;
import static endtoendtest.plugins.teams.helpers.browserstack.BrowserStackRequest.*;

public class TeamsAPI implements ConcurrentEventListener {


    private TestResult testResult;

    private URI testURI;

    private static String buildName;

    private static String buildId;

    private static String projectURL;

    public static void setBuildName(String bsBuildName){
        buildName = bsBuildName;
    }

    public static void setBuildId(String bsBuildId){
        buildId = bsBuildId;
    }

    public static String getBuildName(){
        return buildName;
    }

    public static void setProjectURL(String tpURL){
        projectURL = tpURL;
    }

    @Override
    public void setEventPublisher(EventPublisher eventPublisher) {
        eventPublisher.registerHandlerFor(TestRunStarted.class, TestRunStarted);
        eventPublisher.registerHandlerFor(TestCaseStarted.class, testCaseStarted);
        eventPublisher.registerHandlerFor(TestStepFinished.class, testStepFinished);
        eventPublisher.registerHandlerFor(TestCaseFinished.class, testCaseFinished);
    }

    private final EventHandler<TestRunStarted> TestRunStarted = event -> testResult = new TestResult();

    private final EventHandler<TestCaseStarted> testCaseStarted = event -> {
        if(testURI == null){
            testURI = event.getTestCase().getUri();
        }
        if(event.getTestCase().getUri() != testURI){
            try {
                postMessage(CreateMessage.createMessageJSON(testResult));
            } catch (IOException e) {
                e.printStackTrace();
            }
            testResult = new TestResult();
            testURI = event.getTestCase().getUri();
        }
    };

    private final EventHandler<TestStepFinished> testStepFinished = event -> {
      if(event.getResult().getError() != null){
          PickleStepTestStep step = (PickleStepTestStep) event.getTestStep();
          takeScreenShot(step.getStep().getKeyword() + step.getStep().getText() + " Screenshot");
      }
    };

    private final EventHandler<TestCaseFinished> testCaseFinished = event -> {
        try {
            getBuilds();
            TestProjectRequest.setProjectURL();
            System.out.println(projectURL);
            testResult.setTestProjectURL(projectURL);
            testResult.setBrowserStackUrl("https://automate.browserstack.com/dashboard/v2/builds/" + buildId);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(event.getResult().getError() != null){
            testResult.setNumberFailed(testResult.getNumberFailed() + 1);
        } else {
            testResult.setNumberPassed(testResult.getNumberPassed() + 1);
        }
        try {
            stopTest();
        } catch (IOException e) {
            e.printStackTrace();
        }
    };

    private final EventHandler<TestRunFinished> testRunFinished = event -> {
      System.out.println("failed test = " + testResult.getNumberFailed());
        try {
            postMessage(CreateMessage.createMessageJSON(testResult));
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    };
}

您正在 运行 对 os、os 版本和浏览器的矩阵进行测试。通常,此矩阵在 CI 中配置并通过环境变量提供给测试执行。例如使用 Gitlab CI Matrix:

test:
  stage: test
  script:
    - mvn test
  parallel:
    matrix:
      - OS: Windows
        OS_VERSION: 10
        BROWSER: [Chrome, Firefox, Edge]
      - OS: OS X
        OS_VERSION: Big Sur
        BROWSER: [Chrome, Firefox, Edge, Safari]

然后您使用环境变量在 before 挂钩中创建网络驱动程序,而不是在一个步骤中创建。

    @Before
    public void before(Scenario scenario){
        this.scenario = scenario;
        setScenario(scenario);
        String os = System.getenv("OS");
        String osVersion = System.getenv("OS_VERSION");
        String browser = System.getenv("BROWSER");
        driver = createDriver(scenario, "desktop", os, osVersion, browser);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }