Providing I have the following Feature File with two scenarios that have different tags such as the below:
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"
And I also have the following Hooks
, ChromeWebController
, HomeSteps
& RunCucumberTest
classes, would it be possible to run the scenarios multiple times on different devices. For example, could the scenario with @Desktop
, @Tablet
& @Mobile
be executed three times, once on each device, whilst, the scenario with @Desktop
@Tablet
be executed two times, again, once on each device? Or would this require a different approach?
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();
}
}
Create two (basically equal to the devices) packages for device specific hook.
Hook for mobile type in package mobile
package mobile;
public class MobileHook {
@Before("@mobile")
public void before(Scenario scenario) {
System.out.println("MOBILE");
}
}
Hook for tablet type in package tablet
package tablet;
public class TabletHook {
@Before("@tablet")
public void before() {
System.out.println("TABLET");
}
}
The common glue code should be kept in a separate package say stepdefs
.
Modify the runners accordingly.
@CucumberOptions(plugin = { "summary" }, tags = "@mobile", glue = {"mobile", "stepdefs"})
public class RunCukeMobileIT
@CucumberOptions(plugin = { "summary" }, tags = "@tablet", glue = {"tablet", "stepdefs"})
public class RunCukeTabletIT
Basically reducing the visibility to the relevant hook. Guess u will need to modify the code which initializes the driver.