How to make a scenario that depends on another scenario using cucumber, selenium and java

15,277

Solution 1

Take a look into cucumber hooks, this allows you to set up global 'before' and 'after' steps, which will run for every scenario without having to specify them in your feature files.

Because they run for every scenario, they're ideal for something like initialising the driver at the start of each test. It may be suitable for running your logon, but if there's a chance you'll have a scenario which doesn't involve logging on then it wouldn't be the way to go (alternative further down). The same applies for the after scenario, which is where you could perform the log off and shut down your driver. As an example:

/**
 * Before each scenario, initialise webDriver.
 */
@Before
public void beforeScenario() {
    this.application.initialiseWebDriver();
}

/**
 * After each scenario, quit the web driver.
 */
@After
public void afterScenario() {
    this.log.trace("afterScenario");
        this.application.quitBrowser();
    }

In my example, I'm simply starting the driver in the before scenario, and closing it in the after, But in theory these before and after methods could contain anything, you just need to have them in your step definitions class and annotate them with the '@Before' and '@After' tags as shown.

As well as these, you can also have multiple before and after tags which you can call by tagging the scenario. As an example:

/**
 * Something to do after certain scenarios.
 */
@After("@doAfterMethod")
public void afterMethod() {
    this.application.afterThing();
}

You can set up something like this in your step defs, and as standard it won't run. However, you can tag your scenario with '@doAfterMethod' and it will run for the tagged scenarios, which makes this good for a common scenario that you will need at the end of tests, but not all of them. The same would work for methods to run before a scenario, just change the '@After' to '@Before'.

Bear in mind that if you do use these, the global Before and After (so in this example the driver initialisation and quitting) will always be the first and last things to run, with any other before/afters in between them and the scenario.

Further Reading: https://github.com/cucumber/cucumber/wiki/Hooks https://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Solution 2

There is no support for creating a scenario that depends on another scenario in Cucumber-JVM. I think still is supported in the Ruby implementation in Cucumber. It is, however, a practice that is dangerous. The support for calling a scenario from another scenario will not be supported in future versions of Cucumber.

That said, how do you solve your problem when you want to reuse functionality? You mention logout, how do you handle that when many scenarios requires the state to be logged out for a user? The solution is to implement the functionality in a helper method or helper class that each step that requires a user to be logged out calls.

This allow each scenario to be independent of all other scenarios. This in turn will allow you to run the scenarios in a random order. I don't think the execution order for the scenarios is guaranteed. I definitely know that it is discussed to have the JUnit runner to run scenarios in a random order just to enforce the habit of not having scenarios depend on other scenarios.

Your other question, how to set up WebDriver before a scenario and how to tear it down, is solved using the Before and After hooks in Cucumber. When using them, be careful not to import the JUnit version of Before and After.

Solution 3

You can set test dependency with qaf bdd. You can use dependsOnMethods or dependsOnGroups in scenario meta-data to set dependency same as in TestNG, because qaf-BDD is TestNG BDD implementation.

Share:
15,277

Related videos on Youtube

Asmaa Elhussainy
Author by

Asmaa Elhussainy

Updated on June 04, 2022

Comments

  • Asmaa Elhussainy
    Asmaa Elhussainy almost 2 years

    I am learning cucumber through some tutorials and there is something that I don't know how to do.. I need to make a scenario that depends on another scenario (for example log out scenario I have to be logged in first before logging out) so what should I do? should I write steps of login at log out scenario (at the feature file) or there is a way to call the whole log in scenario at the log out scenario

    also I need to know should I set up driver before each scenario and quit the driver after each scenario?

  • Shubham Jain
    Shubham Jain over 5 years
    Can we use them with cucumber? ... Doet it require TestNG with cucumber? .... If we can use this with existing cucumber and with JUNIT, can please add more information like dependency , sample example or any link which has all details.. it will help
  • user861594
    user861594 over 5 years
    if you are stating new project you don't need any cucumber dependency, refer qaf-step-by-step tutorial. if you have existing tests refer migrating from cucumber jvm to qaf.
  • Shubham Jain
    Shubham Jain over 5 years
    thanks@user861594 ... I will defiantly try it out and let you know my experience .. voting you up :)