Cucumber | Scenario Interface Usage

Priyank Shah
3 min readMay 1, 2019
Assumption here - we have good understanding of following tool / terminologies.
* Cucumber Tool (Basic Idea)
*
Selenium
*
Katalon Tool

Hooks

Hooks are blocks of code that can run at various points in the Cucumber execution cycle. They are typically used for setup and tear-down of the environment before and after each scenario.

@Before

Before hooks run before the first step of each scenario.

  • Common Usage:
Starting a webdriver
Setting up DB connections
Setting up test data
Setting up browser cookies
Navigating to certain page
or anything before the test
  • Annotated method style:
@Before
public void setup() {
// Do something before each scenario
}

So can I use BACKGROUND and BEFORE anywhere in any condition? Well, Be caution about it. Read my blog to clear doubt if any.

@After

After hooks run after the last step of each scenario, even when steps are failed, undefined, pending, or skipped.

  • Common Usage:
Killing the webdriver
Closing DB connections
Clearing the test data
Clearing browser cookies
Logging out from the application
Printing reports or logs
Taking screenshots on error
or anything after the test
  • Annotated method style:
@After
public void teardown(
Scenario scenario) {
// Do something after after scenario
}

The scenario parameter is optional. If you use it, you can inspect the status of the scenario.

Problem Statement:

Usage of Scenario Interface

Solution:

cucumber.api.Scenario
  • Cucumber API provides an Interface called Scenario, using which you can get can instance of this class. All challenges mentioned above can be solved through Scenario Interface.
scenario.isFailed()

See complete solution here on my blog.

scenario.embed(byte[] data, String mimeType)

See complete solution here on my blog.

scenario.isFailed(String text)

See complete solution below.

scenario.getName()
scenario.getSourceTagNames()

See complete solution Below.

Sample Code:

class Hook {

@Before
public setup(Scenario scenario)
{
scenario.getName()
}
@After
// Don't go for logout if scenario is not implemented.
public void user_is_logged_out(Scenario scenario) {
String status = scenario.getStatus()
if(status.equalsIgnoreCase('UNDEFINED')) {
return;
}
'Take screen shot in case of failure and embed with report'
if(scenario.isFailed()) {
String filePath = WebUI.takeScreenshot()
File file = new File(filePath)
scenario.embed(Files.readAllBytes(file.toPath()), "image/png")
}
'Click on Log Out Option'
// Logout from application
'Close Browser'
WebUI.closeBrowser()
}
}

In case you would like to use scenario in other function / step definition:

class myTest {
public Scenario scenario
@Before
public setupMyTest(Scenario scenario) {
this.scenario = scenario
}
@Then("How to use scenario under other function")
public void step_definition_implementation() {
this.scenario.write("sample test message")
}
}

--

--

Priyank Shah
Priyank Shah

Written by Priyank Shah

Agile Product Leader | Delivery Manager | Design Thinker (PRINCE2, CSPO™, CSM™, SFC™, ISTQB)

Responses (1)