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

 

 

DryRun Feature File

  • Run the Feature File as Cucumber Test
  • This will
    • 'wire up' the feature internally ( cucumber.json )
    • locate required TestSteps(ie: methods) within the 'StepDefs' package
    • flag any missing TestSteps & provide a suggested method name
      • great for saving time typing each method name - simply copy/paste suggestion into the definitions file(s)
    • indicate missing runner or other 'structural' problems

Copy Method Names into StepDefs File

  • Cucumber plugin will output sample code for missing StepDefs
  • copy these into one or more StepDefinition files
  •  

DryRun Test Runner

  • With DryRun=True, now run the TestRunner as JUnit
  • This will go through the actions of the test without actually executing the test step methods
  • This will flag any 'wiring' issues between the feature, stepdefs, and testrunner.

Add Method Logic

  • Add logic to any new methods

DryRun Test Runner

  • With DryRun=True, now run the TestRunner as JUnit

DryRun=False Test Runner

  • With DryRun=False, now run the TestRunner as JUnit

Add Feature to TestNG.xml ( or create a new TestNGRunner.xml )

  • Run TestNG.xml as TestNG Test

Ensure TestNG.xml is included in Maven pom.xml so that Maven '-test will call the newly added features/tests

 

 

 

Walkthrough

 

Create new feature file 'Features/NewBddFeature.feature

Add Gherkin Data

Feature: New BDD Featureless Feature

Scenario: So When I Do Nothing, Nothing Happens

Given user1 is already on Login Page
Given user1 is doing nothing
When user1 still does nothing
Then user1 sees nothing happens
Then close the browser
   
  • Cucumber will
    • parse the feature file for valid syntax
    • highlight steps that have no corresponding StepDefs

 

Create TestRunner File 'bdd/Runners/NewBddFeatureRunner.java'

  •  
package com.cs.cmsauto.bdd.Runners;

public class NewBddFeatureRunner {

}

 

Add TestRunner Details

package com.cs.cmsauto.bdd.Runners;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        
        features=  "src/test/java/com/cs/cmsauto/bdd/Features/NewBddFeature.feature"
        ,glue= {"com.cs.cmsauto.bdd.StepDefs"} // can also reference just 'StepDefs', but seems better to be explicit with this reference
        
        ,format= {"pretty","html:test-output", "json:json-output/cucumber.json","junit:junit-xml-output/cucumber.xml"}
        ,monochrome = true
        ,strict = true //true = fail test suite if ANY test step not defined
        ,dryRun = true //true = check feature-definition-runner setup is valid... NO test logic executed
        
        )

//,glue= {"com.cs.cmsauto.bdd.StepDefs/shared/menu_admin"}

public class NewBddFeatureRunner {
//nothing goes here, its just a constructor for the class
}

 

Create StepDefs File

 

package com.cs.cmsauto.bdd.StepDefs;

public class NewBddFeatureDefs {

}

 

 

Run Feature File As 'Cucumber Feature'

  • The plugin will issue output to the Console
  • Copy the method names into the respected StepDefs files
  • in this example 'close the browser' step already has a stepdef written, so only the others will have suggested method names output to the console

 

Feature: New BDD Featureless Feature

  Scenario: So When I Do Nothing, Nothing Happens # W:/w/git/CmsAutoUC/src/test/java/com/cs/cmsauto/bdd/Features/NewBddFeature.feature:3
    Given user is already on Login Page
    Given user1 is doing nothing
    When user1 still does nothing
    Then user1 sees nothing happens
    Then close the browser                        # MenuAdminLoginDefs.close_the_browser()

1 Scenarios (1 undefined)
5 Steps (2 skipped, 3 undefined)
0m0.000s

You can implement missing steps with the snippets below:

@Given("^user(\\d+) is doing nothing$")
public void user_is_doing_nothing(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^user(\\d+) still does nothing$")
public void user_still_does_nothing(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^user(\\d+) sees nothing happens$")
public void user_sees_nothing_happens(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

 

NewBddFeatureDefs.java

  •  
package com.cs.cmsauto.bdd.StepDefs;

public class NewBddFeatureDefs {


    @Given("^user(\\d+) is doing nothing$")
    public void user_is_doing_nothing(int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^user(\\d+) still does nothing$")
    public void user_still_does_nothing(int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^user(\\d+) sees nothing happens$")
    public void user_sees_nothing_happens(int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    
}

 

Errors will be produced until the Cucumber imports are made

  •  

 

And the 'throws' & 'throw new' logic is removed

  •  
    @Given("^user(\\d+) is doing nothing$")
    public void user_is_doing_nothing(int arg1) {
    }

 

NewBddFeatureDefs.java Ready for DryRun

package com.cs.cmsauto.bdd.StepDefs;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class NewBddFeatureDefs {

    

    @Given("^user(\\d+) is doing nothing$")
    public void user_is_doing_nothing(int arg1) {
    }

    @When("^user(\\d+) still does nothing$")
    public void user_still_does_nothing(int arg1) {
    }

    @Then("^user(\\d+) sees nothing happens$")
    public void user_sees_nothing_happens(int arg1) {
    }
    
}

???Check NewBddFeatureRunner.java for dryRun=true & run the feature as Cucumber Feature again.

 

Run Runner as JUnit Test

  • if no errors occur, ready for login in the StepDefs
Feature: New BDD Featureless Feature

  Scenario: So When I Do Nothing, Nothing Happens # src/test/java/com/cs/cmsauto/bdd/Features/NewBddFeature.feature:3
    Given user1 is already on Login Page          # NewBddFeatureDefs.user_is_already_on_Login_Page()
    Given user1 is doing nothing                  # NewBddFeatureDefs.user_is_doing_nothing(int)
    When user1 still does nothing                 # NewBddFeatureDefs.user_still_does_nothing(int)
    Then user1 sees nothing happens               # NewBddFeatureDefs.user_sees_nothing_happens(int)
    Then close the browser                        # MenuAdminLoginDefs.close_the_browser()

1 Scenarios (1 skipped)
5 Steps (5 skipped)
0m0.000s

 

Run Runner as JUnit Test dryRun=false

  • Test logic should be executed.
  • In this feature:
    • the browser will be opened
    • the 3 new methods do nothing
    • the browser will be closed
  •