Cucumber BDD Framework
Tags: selenium, java, cucumber, gherkin
Published: 2021-11-17 18:27:42


#Basics of Behaviour Driver Development
Often there is a Problem of Secifying the needs of the Business, communicate it to Development and Testing. There are mismatches between their perceptions. 

-> write a Requirement in a formal standard/ template in a language which is a common words. Dev, QA and Business can express the Requriment in that language. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynq7lx90ohovrmofeeio.png)

###Scenario template 
In order to (achieve something/ Business Outcome)
as a (user)
i want to do (this). 

Example: 
In oder to pay credit card payment
as a NetBanking sole owner who has credit section access
i want to navigate to credit card section, enter amount and process my payment. 

Dev uses this scenario to develop & tester uses it to write testcases(positiv & negativ /Payment is not happening) 

##Testcases
Given (what you need to have to perform an action)
When (performs action) 
Then (desired outcome for the user) 

Example: 
Given: An account with sero balance
When: i navigate to Credit card Payment Section and click to submit by giving amount
Then: it should show a warning - funds

Given : An account with sufficent balance who does not have credit card
When: I navigate to Credit card Payment Section and amount
Then :You don't have to access /warning message 

Business values are easy detectable. (in scenario)
Testcases are countable for each scenario. 
Using a standard template for both manual and automation testing. 
 
#Cucumber Framework Architecture & Core Functionalities
##Setup 
Install Cucmber Plugin into eclipse from eclipse market place
Cucumber expects a Selenium scaletton, optains by Maven 
Open a Maven Project with quickstart template (maven-architect-template) 
Artifactid and GroupId(Projectname)
Maven: libaries in pom.xml: Cucumber JVM Java && Cucumber JVM JUnit

##Cucmber Core Functionalities
*Feature File* - "When i click on button"
*StepDefinition File*  - mapped Code to click button 
*JUnit TestRunner* - triggers all Testcase (related Documents) 

####Feature File 
under src/test/java create Package features 
inside create "file" - zB Login.feature (provide .feature)

```java
Feature: Application Login 

Scenario: Home page default login 
Given User is on landing page 
When User is logging in with username and password
Then home page is populated 
And all infos about you bills is displayed
``` 
AddOn in Eclipse to highlight the cucumer Syntax: Natural 0.7... 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4zeznk5jf4dbidjh6iq.png)
-> no defition found (no code implementation yet) 

####StepDefinition File
under src/test/java create Package stepDefinition
under this, stepDefition Java Class 
```java
@Given("^User is on landing page$")
public void user_is_on_landing_lage()
{ //navigate to the landing page }
@When("^User is logging in with username and password$")
public void user_is_logging_in(){...}
@Then("^home page is populated$")
public void home_page_is_populated(){...}
@And("^all infos about you bills is displayed$")
public void all_infos_about_bills_are_displayed(){...}

```
*create mapping stepDefinition* 
install Extention from Chrome Webstore: Tidy Gherkin app 
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7kie62mh9zjc8nvg50j.png) chrome://apps ->  click, an window will open where you can paste your feature file and get the tidy cucumber java (or ...) code
(adjust package-/classname accourdingly to your code) 

or just run tests and copy code from the konsole
 
jump from a featurefile-sentence -> to stepDefinition: Crtl + Click

###Running Tests with TestRunner File (JUnit) 
preferable under the same roof with the stepDefinitions Package src/test/java create Package cucumberOptions and in there class TestRunner 
Annotations: 
```java
@RunWith(Cucumber.class)
@CUcumberOptions(
    features = "src/test/java/features", 
    glue="stepDefinitions")

```
packages stepDefitions + cucumberOptions should have same parents 
 
variables
feature file: put it in double quotes -Code: display it as regular expression in Annotation of function + passed as arguments in Method (so nr. of arguments should match nr. of parameters) 

##Parameterization

```java
Scenario Outline:
Given ... 
When User enters <username> and <password>
Then ...
Examples: 
|username  | password|
|Elfriede  | 1234    |
|else      | 2345    |
```
Arrows < ... > makes Cucumber look in the Example Section for the values. it matches with the column names + takes the first row - second test will take second row 
test will run the nr of rows which are filled 

###Running Tests with TestRunner File (TestNG, Maven) 
get dependencies from Maven with searching for "Cucumber Testng lastet maven dependency" for pom.xml 
Cucumber JVN: TestNG .... (cucumber java, cucumber Junit + cucumber Testng versions should be the same) 

you need to extend the TestRunner class with AbstractTestNGCucumberTests as it is explained [here](https://github.com/cucumber/cucumber-jvm/tree/main/testng)

```java
@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/java/features", 
    glue="stepDefinitions")
public class TestRunner extends AbstractTestNGCucumberTests {
}
```