Cucumber-JVM

Drupal Testing: CucumberJVM Workflow

Create Gherkin Feature File

  • Add BDD feature & scenario data

 

 

Create Test Runner File

  • glue = StepDefs
  • DryRun=true
  •  

 

Create StepDefs File under /StepDefs

  • Add Step Definitions to code files under StepDefs
  • Best to organize StepDefs into subfolders for 'feature', 'sprints', 'macros', navigation, testID
  • Duplicate method names CAN NOT exist in this StepDefs package
    • So... BDD language should be carefully planned out for maximizing re-use & avoiding redundancy of actions
    • if 5 stepdefs are created for login and the login routine changes....
      • will have to update the same routine 5 times.
  • Macros are helpful for multi-step routines that are commonly used across tests

 

 

CucumberJVM: 00 - Gherkin Language

Basic Gherkin Feature Model

Feature: User can create an article

Scenario: user logs in
Given user is on HomePage
When user navigates to LoginPage
And user enters UserName and Password
Then message displayed Login Successful

Scenario: user creates a new article
Given user is logged in and on HomePage
When user clicks CreateArticleLink
And user enters ArticleTitle
And user enters ArticleBody
And user neters ArticleTags
And user clicks NewArticleSubmitButton
Then message is displayed Article Created Successfully
And NewArticlePage title is MyNewArticle

Feature Keyword

  • Defines the functionality the test targets
  • Multiple scenarios can be created to cover this functionality
Feature: User can create an article
Feature: User can create an article

This feature will ensure a user can login and the product handles invalid credentials correctly
Feature: User can create an

CucumberJVM: 09 - Cucumber Hooks: @before & @after

 

 


@Before

public void setup(){}

 


@After

public void teardown(){}

 


Global

@Before()

 

@After()


Tagged Scenarios

@First

Scenario: first scenario

@Second

Scenario: second scenario

@Third

Scenario: third scenario

 


For @First ONLY ( NOT GLOBAL )

@Before("@First")

@After("@First")

 


Tagged Hooks

@Before() //Global

@Before("@First") //Specific

@After("@First") //Specific

@After() //Global


Ordered Hooks

@Before(order=0) //Global

@After(order=0) //Global

@Before(order=1) //Global

@After(order=1) //Global


 

 

 

 

 

 

 

 

 

CucumberJVM: 07 - DataDriven with Maps

 


Feature

Feature: Deal data creation

Scenario: Free CRM Create a new deal scenario

    #Using a HashMap
    Given user is already on Login Page
    When title of login page is Free CRM
    Then user enters username and password
        | username | password |
        | naveenk | test@123 |
    Then user clicks on login button
    Then user is on home page
    Then user moves to new deal page
    Then user enters contact details
    #FORMAT = ctrl-shift-f
        | title      | amount | probability | commission |
        | test deal1 | 1000 | 50 | 10 |
        | test deal2 | 2000 | 60 | 20 |
        | test deal3 | 3000 | 70 | 30 |
    Then close the browser

 

 

 

CucumberJVM: 05 - More Logic with Examples:, driver.switchTo() and action.moveToElement()

Another feature file with more test data

Feature: Free CRM Create Contacts

Scenario Outline: Free CRM Login Test Scenario

Given user is already on Login Page
When title of login page is Free CRM
Then user enters "<username>" and "<password>"
Then user clicks on login button
Then user is on home page
Then user moves to new contact page
Then user enters contact details "<firstname>" and "<lastname>" and "<position>"
Then close the browser

Examples:
    | username | password | firstname | lastname | position |
    | naveenk  | test@123 | ucFirst1  | ucLast1  | manager  |
    | naveenk  | test@123 | ucFirst2  | ucLast2  | qa       |

Definitions implementation

 

CucumberJVM: 04 - Data Driven Cucumber

 

Data Driven BDD

Then user enters username and password

    @Then("^user enters username and password$")
    public void user_enters_username_and_password() {
        driver.findElement(By.xpath("//input[@name='username']")).sendKeys("naveenk");
        driver.findElement(By.name("password")).sendKeys("test@123");
    }

 

Then user enters "naveenk" and "test@123"

 

//    @Then("^user enters \"(.*)\" and \"(.*)\"$")
    @Then("^user enters \"([^\"]*)\" and \"([^\"]*)\"$")
    public void user_enters_username_and_password(String username, String password) {
        driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
    }

@WithExamples

 

Subscribe to Cucumber-JVM