I've looked at resources like here and here, but these solutions are not working for me.
What I want to do: without involving much heavy machinery (unless I have to) and without using a third-party service (not an option), I want to run my features/tests against several different browsers without having to manually change the WebDriver initialization.
So for instance, I have two test runners right now that look like this:
@RunWith(Cucumber.class)
@CucumberOptions(
format = {"pretty", "html:target/html/"},
features = {"src/test/resources"},
tags = "@Firefox"
)
public class FirefoxTest {
}
and another test runner for Chrome. I have four feature files that begin like this:
@Chrome @Firefox
Feature: I want to test this..
And then I have StepDefinition classes that look like this:
@Before("@Firefox")
public void setUpFirefox() {
System.out.println("in page object firefox");
driver = getDriver("Firefox");
pageObject = PageFactory.initElements(driver, PageObject.class);
}
@Before("@Chrome")
public void setUpChrome() {
System.out.println("in page object chrome");
driver = getDriver("Chrome");
pageObject = PageFactory.initElements(driver, PageObject.class);
}
And an AbstractStepDefinition class that looks like this:
public class AbstractPageStepDefinitions {
protected static WebDriver driver;
protected WebDriver getDriver(String browser) {
//System.out.println("browser: " + browser);
if (driver == null) {
if (browser.equals("Chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\580782\\Desktop\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
}
else if (browser.equals("Firefox")) {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
}
return driver;
}
When I run the Firefox test runner, my System.out.println statements print:
in page object firefox
in page object chrome
Why is the @Before("@Chrome") hook being called in each step definition class when I specify with @CucumberOptions( tags = "@Firefox" )?