简体   繁体   English

有没有办法为所有 cucumber 测试使用单个示例文件?

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

We are currently using multiple examples but they all contain the same examples.我们目前正在使用多个示例,但它们都包含相同的示例。 We are looking for a way to use the same example across all test without explicitly defining them in each feature file since they are only used to describe the devices we need to test on.我们正在寻找一种在所有测试中使用相同示例的方法,而无需在每个功能文件中明确定义它们,因为它们仅用于描述我们需要测试的设备。 We can not use qaf due to us depending on cucumber events.我们不能使用 qaf,因为我们取决于 cucumber 事件。 below is a example of are test.下面是一个测试的例子。

WAS_dev.feature 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 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 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团队API.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());
        }
    };
}

You are running a your tests against a matrix of os, os version and browser.您正在针对操作系统、操作系统版本和浏览器的矩阵运行测试。 Typically this matrix configured in CI and provided to the test execution via environment variables.通常,此矩阵在 CI 中配置并通过环境变量提供给测试执行。 For example using Gitlab CI Matrix :例如使用Gitlab CI 矩阵

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]

You then create the web driver in the before hook using the environment variables rather then in a step.然后,您在 before 挂钩中使用环境变量而不是在一个步骤中创建 web 驱动程序。

    @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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM