Behavior Driven Development (BDD) with Cucumber

Installation & Setup

1. Add Cucumber Plugin in Eclipse

2. Add Dependencies

Required Dependencies:
xml
Copy code
<dependencies>

  <dependency>

    <groupId>info.cukes</groupId>

    <artifactId>cucumber-java</artifactId>

    <version>1.2.5</version>

    <scope>test</scope>

  </dependency>

  <dependency>

    <groupId>info.cukes</groupId>

    <artifactId>cucumber-jvm</artifactId>

    <version>1.2.5</version>

    <type>pom</type>

  </dependency>

  <dependency>

    <groupId>info.cukes</groupId>

    <artifactId>cucumber-junit</artifactId>

    <version>1.2.5</version>

    <scope>test</scope>

  </dependency>

  <dependency>

    <groupId>info.cukes</groupId>

    <artifactId>cucumber-jvm-deps</artifactId>

    <version>1.0.5</version>

  </dependency>

  <dependency>

    <groupId>net.masterthought</groupId>

    <artifactId>cucumber-reporting</artifactId>

    <version>1.0.0</version>

  </dependency>

  <dependency>

    <groupId>info.cukes</groupId>

    <artifactId>gherkin</artifactId>

    <version>2.12.2</version>

  </dependency>

  <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>3.8.1</version>

    <scope>test</scope>

  </dependency>

  <dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-java</artifactId>

    <version>3.5.3</version>

  </dependency>

  <dependency>

    <groupId>info.cukes</groupId>

    <artifactId>cucumber-picocontainer</artifactId>

    <version>1.2.5</version>

    <scope>test</scope>

  </dependency>

</dependencies>


3. Download the Natural Plugin in Eclipse

Cucumber Files in Eclipse

1. Feature File

  • File Extension: .feature

Example:
gherkin
Copy code
Feature: Facebook Feature


Scenario: Facebook login Test scenario

  Given user is already on login page

  When title of login page is Facebook

  Then user enters username and user enters password

  And user clicks on Login button

  And user is on home page


  • Gherkin Language Tags:

    • Feature: Name of the feature

    • Scenario: High-level scenario name which will be mapped

    • Given: Precondition of test case

    • When: Test case scenarios

    • Then: Acceptance criteria of test case

    • And: Adds extra steps for given, when, then, etc.

2. Step Definition Class

Example:
java
Copy code
@Given("^user is already on login page$")

public void user_already_on_Login_Page() {

  System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

  driver = new ChromeDriver();

  driver.get("https://www.facebook.com");

  driver.manage().window().maximize();

}


3. Test Runner Class

Example:
java
Copy code
@RunWith(Cucumber.class)

@CucumberOptions(

  features = "path/to/login.feature",

  glue = {"stepDefinitions"},

  format = {"pretty", "html:test-output"}

)

public class TestRunner {

}


  • Cucumber Options:

    • dryRun: Checks the mapping between feature file and step definition file.

    • features: Sets the path of the feature file or package name in which feature files are present.

    • glue: Gets the package name of the step definition files.

    • monochrome: Makes the output more readable.

    • format: Changes the format of the output and generates different types of reports.

    • strict: Fails the execution if any step definition is missing for any of the tags of the feature file.

4. Data-Driven Testing Using Cucumber

Simple Data Driven without Using Example Keyword

Feature File:
gherkin
Copy code
Then user enters "username" and user enters "password"

Step Definition Class:
java
Copy code
@Then("^user enters \"(.*)\" and the user enters \"(.*)\"$")

public void user_enters_username_and_user_enters_password(String username, String password) {

  driver.findElement(By.id("email")).sendKeys(username);

  driver.findElement(By.id("pass")).sendKeys(password);

}


With Examples and Scenario Outline Keyword

Feature File:
gherkin
Copy code
Scenario Outline: Facebook login Test scenario

  Given user is already on login page

  When title of login "<Username>" and user enters "<password>"

  Then user clicks on Login button

  Then user is on home page


Examples:

  |username|password|

  |user1|pass1|

  |user2|pass2|

Using Data Tables

Feature File:
gherkin
Copy code
Scenario: Facebook login Test scenario

  Given user is already on login page

  When title of login page is Facebook

  Then user enters username and user enters password

    |user1|pass1|

    |user2|pass2|

  Then user clicks on Login button

  And user is on home page

  Then close the browser and driver


Step Definition Class:
java
Copy code
@Then("^user enters username and user enters password$")

public void user_enters_username_and_user_enters_password(DataTable data) throws InterruptedException {

  List<List<String>> credentials = data.raw();

  driver.findElement(By.id("email")).sendKeys(credentials.get(0).get(0));

  driver.findElement(By.id("pass")).sendKeys(credentials.get(0).get(1));

  driver.findElement(By.id("email")).clear();

  driver.findElement(By.id("pass")).clear();

  driver.findElement(By.id("email")).sendKeys(credentials.get(1).get(0));

  driver.findElement(By.id("pass")).sendKeys(credentials.get(1).get(1));

  Thread.sleep(3000);

}


Using Tables with Map

Feature File:
gherkin
Copy code
Scenario: Facebook login Test scenario

  Given user is already on login page

  When title of login page is Facebook

  Then user enters username and user enters password

    |username|password|

    |user1|pass1|

    |user2|pass2|

  Then user clicks on Login button

  And user is on home page

  Then close the browser and driver


Step Definition Class:
java
Copy code
@Then("^user enters username and user enters password$")

public void user_enters_username_and_user_enters_password(DataTable data) throws InterruptedException {

  for (Map<String, String> credentials : data.asMaps(String.class, String.class)) {

    driver.findElement(By.id("email")).sendKeys(credentials.get("username"));

    driver.findElement(By.id("pass")).sendKeys(credentials.get("password"));

    driver.findElement(By.id("email")).clear();

    driver.findElement(By.id("pass")).clear();

  }

}


5. Cucumber Tags

Example:
gherkin
Copy code
Feature: Tag use


@SmokeTest @Regression

Scenario: Login to Facebook

  Given Browser opened one


@SmokeTest

Scenario: Login to Orkut

  Given Browser opened two


@EndtoEnd

Scenario: Login to Gmail

  Given Browser opened three


@EndtoEnd @Regression

Scenario: Login to WhatsApp

  Given Browser opened four


@EndtoEnd

Scenario: Login to LinkedIn

  Given Browser opened five


@EndtoEnd @Regression

Scenario: Login to TikTok

  Given Browser opened six

6. Hooks in Cucumber

  • Global Hooks: @Before and @After

Example:
java
Copy code
@Before

public void setup() {

  System.out.println("Start driver");

}


@After

public void tearDown() {

  System.out.println("Close the browser");

}


  • Local or Tagged Hooks:

Example:
java
Copy code
@Before("@SmokeTest")

public void smokeTestSetup() {

  System.out.println("Smoke test setup");

}


@After("@Regression")

public void regressionTearDown() {

  System.out.println("Regression test teardown");

}


7. Passing a JSON Object to Cucumber

Feature File:
gherkin
Copy code
Scenario: Passing JSON object

  Given user sends the following payload:

    """

    {

      "key": "value",

      "key2": "value2"

    }

    """

  Then I get the correct response

Step Definition Class:
java
Copy code
@Given("user sends the following payload:")

public void user_sends_the_following_payload(String jsonPayload) {

  // Here you can use the jsonPayload to send a request

}



Post a Comment

0 Comments