1

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" )?

  • The only thing I can think of is that since every feature (and thus every scenario) has both tags, both tagged @Before hooks are run. – douglas Sep 18 '15 at 15:17
1

Because you are using the tags (@Chrome and @Firefox) at the beginning of your feature file (Feature). And both of them will be executed for each scenario.

Another solution is to switch between the browsers via your job configurations and system property i.e. each job executes some scenarios for a specific browser. For example:

@Before("@web")
    public void beforeScenario() {
        LOG.info("BeforeScenario in hook: initializing WebDriver");
        initContext();
        }

........

public void initContext() {

        String browser = System.getProperty("browser", "local");
        if (browser.equals("firefox")) {
            Capabilities caps = DesiredCapabilities.firefox();
            try {
                context = new RemoteWebDriver(new URL(hubUrl), caps);
            } catch (MalformedURLException e) {
                throw new RuntimeException("Something wrong with hub url " + hubUrl);
            }
        } else if (browser.equals("chrome")) .....

And then you set what browser you desire in your system property via job configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.