В настоящее время мы используем несколько примеров, но все они содержат одни и те же примеры. Мы ищем способ использовать один и тот же пример во всех тестах без явного определения их в каждом файле функций, поскольку они используются только для описания устройств, на которых нам нужно тестировать. Мы не можем использовать qaf из-за нас в зависимости от событий огурца. ниже приведен пример теста.

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());
        }
    };
}
0
zachary edgell 25 Фев 2021 в 21:06

1 ответ

Лучший ответ

Вы запускаете свои тесты по матрице ОС, версии ОС и браузера. Обычно эта матрица настраивается в 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);
    }
1
M.P. Korstanje 28 Фев 2021 в 00:06