Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

java - 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. below is a example of are test.

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());
        }
    };
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. For example using 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]

You then create the web driver in the before hook using the environment variables rather then in a step.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...