是否可以根据标签多次执行黄瓜测试?
Is it possible to execute Cucumber Tests multiple times based on tags?
假设我有以下功能文件,其中包含两个具有不同标签的场景,如下所示:
Feature: Home Feature
Tests for the Home page
@Desktop @Tablet @Mobile
Scenario: I should see the correct page title
Given I navigate to the Home page
Then the page title should read "test"
@Desktop @Tablet
Scenario: I should see the correct body text
Given I navigate to the Home page
Then the body text should read "test"
我还有下面的Hooks
,ChromeWebController
,HomeSteps
&RunCucumberTest
类,可否运行这些场景在不同的设备上多次出现。例如,@Desktop
、@Tablet
和 @Mobile
的场景是否可以执行三次,每个设备一次,而 @Desktop
@Tablet
的场景是在每个设备上再次执行两次?或者这需要不同的方法吗?
package hooks;
import controllers.ChromeWebController;
import fixtures.HomePageFixture;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
public class Hooks {
private final ChromeWebController chromeWebController;
private Scenario scenario;
public Hooks(ChromeWebController chromeWebController) {
this.chromeWebController = chromeWebController;
}
@Before
public void beforeScenarioSteps(Scenario scenario) {
this.scenario = scenario;
chromeWebController.setDeviceType(scenario.getSourceTagNames());
chromeWebController.setupChromeDriver();
scenario.log(HomePageFixture.HOME_PAGE_URL);
}
@After
public void afterScenarioSteps() {
chromeWebController.quitWebDriver();
}
}
package controllers;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeWebController {
private WebDriver webDriver;
private String deviceType;
public ChromeWebController() {
}
public void setupChromeDriver() {
ChromeOptions options = new ChromeOptions();
Map<String, String> mobileEmulation = new HashMap<>();
WebDriverManager.chromedriver().setup();
options.addArgument("--window-size=1366,768");
if (getDeviceType().equals("Tablet")) {
mobileEmulation.put("deviceName", "iPad");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
} else if (getDeviceType().equals("Mobile")) {
mobileEmulation.put("deviceName", "iPhone X");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
}
this.webDriver = new ChromeDriver(options);
}
public WebDriver getWebDriver() {
if (webDriver == null) {
throw new WebDriverException("WebDriver has not been created, please ensure setupChromeDriver has been called.");
}
return webDriver;
}
public void quitWebDriver() {
webDriver.quit();
}
public void setDeviceType(Collection<String> scenarioTags) {
if (deviceType != null) {
throw new IllegalArgumentException("deviceType can only be set once.");
}
var scenarioTagsToString = scenarioTags.toString();
if (scenarioTagsToString.contains("@Desktop")) {
deviceType = "Desktop";
} else if (scenarioTagsToString.contains("@Tablet")) {
deviceType = "Tablet";
} else if (scenarioTags.toString().contains("@Mobile")) {
deviceType = "Mobile";
} else {
throw new IllegalArgumentException("Desktop/Tablet/Mobile scenario tag has not been implemented in the feature file.");
}
}
public String getDeviceType() {
if (deviceType.equals("")) {
throw new IllegalArgumentException("deviceType has not been set.");
}
return deviceType;
}
}
package steps.home;
import controllers.ChromeWebController;
import fixtures.HomePageFixture;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class HomeSteps{
private final WebDriver webDriver;
public HomeSteps(ChromeWebController chromeWebController) {
this.webDriver = chromeWebController.getWebDriver();
}
@Given("I navigate to the Home page")
public void iNavigateToTheHomePage() {
webDriver.get(HomePageFixture.HOME_PAGE_URL);
}
@Then("The body text should read {string}")
public void theBodyTextShouldRead(String expectedText) {
String actualText = webDriver.findElement(By.id("test")).getText();
assert (actualText).equals(expectedText);
}
}
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
@CucumberOptions(publish = true)
public class RunCucumberTest extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
为设备特定的挂钩创建两个(基本上等于设备)包。
包中移动类型的挂钩 mobile
package mobile;
public class MobileHook {
@Before("@mobile")
public void before(Scenario scenario) {
System.out.println("MOBILE");
}
}
包装中平板电脑类型的挂钩 tablet
package tablet;
public class TabletHook {
@Before("@tablet")
public void before() {
System.out.println("TABLET");
}
}
公共粘合代码应该保存在单独的包中,比如 stepdefs
。
相应地修改跑步者。
@CucumberOptions(plugin = { "summary" }, tags = "@mobile", glue = {"mobile", "stepdefs"})
public class RunCukeMobileIT
@CucumberOptions(plugin = { "summary" }, tags = "@tablet", glue = {"tablet", "stepdefs"})
public class RunCukeTabletIT
基本上降低了相关挂钩的可见性。我猜你需要修改初始化驱动程序的代码。
假设我有以下功能文件,其中包含两个具有不同标签的场景,如下所示:
Feature: Home Feature
Tests for the Home page
@Desktop @Tablet @Mobile
Scenario: I should see the correct page title
Given I navigate to the Home page
Then the page title should read "test"
@Desktop @Tablet
Scenario: I should see the correct body text
Given I navigate to the Home page
Then the body text should read "test"
我还有下面的Hooks
,ChromeWebController
,HomeSteps
&RunCucumberTest
类,可否运行这些场景在不同的设备上多次出现。例如,@Desktop
、@Tablet
和 @Mobile
的场景是否可以执行三次,每个设备一次,而 @Desktop
@Tablet
的场景是在每个设备上再次执行两次?或者这需要不同的方法吗?
package hooks;
import controllers.ChromeWebController;
import fixtures.HomePageFixture;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
public class Hooks {
private final ChromeWebController chromeWebController;
private Scenario scenario;
public Hooks(ChromeWebController chromeWebController) {
this.chromeWebController = chromeWebController;
}
@Before
public void beforeScenarioSteps(Scenario scenario) {
this.scenario = scenario;
chromeWebController.setDeviceType(scenario.getSourceTagNames());
chromeWebController.setupChromeDriver();
scenario.log(HomePageFixture.HOME_PAGE_URL);
}
@After
public void afterScenarioSteps() {
chromeWebController.quitWebDriver();
}
}
package controllers;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeWebController {
private WebDriver webDriver;
private String deviceType;
public ChromeWebController() {
}
public void setupChromeDriver() {
ChromeOptions options = new ChromeOptions();
Map<String, String> mobileEmulation = new HashMap<>();
WebDriverManager.chromedriver().setup();
options.addArgument("--window-size=1366,768");
if (getDeviceType().equals("Tablet")) {
mobileEmulation.put("deviceName", "iPad");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
} else if (getDeviceType().equals("Mobile")) {
mobileEmulation.put("deviceName", "iPhone X");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
}
this.webDriver = new ChromeDriver(options);
}
public WebDriver getWebDriver() {
if (webDriver == null) {
throw new WebDriverException("WebDriver has not been created, please ensure setupChromeDriver has been called.");
}
return webDriver;
}
public void quitWebDriver() {
webDriver.quit();
}
public void setDeviceType(Collection<String> scenarioTags) {
if (deviceType != null) {
throw new IllegalArgumentException("deviceType can only be set once.");
}
var scenarioTagsToString = scenarioTags.toString();
if (scenarioTagsToString.contains("@Desktop")) {
deviceType = "Desktop";
} else if (scenarioTagsToString.contains("@Tablet")) {
deviceType = "Tablet";
} else if (scenarioTags.toString().contains("@Mobile")) {
deviceType = "Mobile";
} else {
throw new IllegalArgumentException("Desktop/Tablet/Mobile scenario tag has not been implemented in the feature file.");
}
}
public String getDeviceType() {
if (deviceType.equals("")) {
throw new IllegalArgumentException("deviceType has not been set.");
}
return deviceType;
}
}
package steps.home;
import controllers.ChromeWebController;
import fixtures.HomePageFixture;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class HomeSteps{
private final WebDriver webDriver;
public HomeSteps(ChromeWebController chromeWebController) {
this.webDriver = chromeWebController.getWebDriver();
}
@Given("I navigate to the Home page")
public void iNavigateToTheHomePage() {
webDriver.get(HomePageFixture.HOME_PAGE_URL);
}
@Then("The body text should read {string}")
public void theBodyTextShouldRead(String expectedText) {
String actualText = webDriver.findElement(By.id("test")).getText();
assert (actualText).equals(expectedText);
}
}
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
@CucumberOptions(publish = true)
public class RunCucumberTest extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
为设备特定的挂钩创建两个(基本上等于设备)包。
包中移动类型的挂钩 mobile
package mobile;
public class MobileHook {
@Before("@mobile")
public void before(Scenario scenario) {
System.out.println("MOBILE");
}
}
包装中平板电脑类型的挂钩 tablet
package tablet;
public class TabletHook {
@Before("@tablet")
public void before() {
System.out.println("TABLET");
}
}
公共粘合代码应该保存在单独的包中,比如 stepdefs
。
相应地修改跑步者。
@CucumberOptions(plugin = { "summary" }, tags = "@mobile", glue = {"mobile", "stepdefs"})
public class RunCukeMobileIT
@CucumberOptions(plugin = { "summary" }, tags = "@tablet", glue = {"tablet", "stepdefs"})
public class RunCukeTabletIT
基本上降低了相关挂钩的可见性。我猜你需要修改初始化驱动程序的代码。