Skip to main content

Cucumber - Execution of test cases and reporting




Before going through this blog please checkout blog on  Cucumber Fundamentals

Cucumber is testing tool which implements BDD(behaviour driven development).It offers a way to write tests that anybody can understand, regardless of there technical knowledge.

It users Gherkin (business readable language) which helps to describe behaviour without going into details of implementation

It's helpful for business stakeholders who can't easily read code (

Why cucumber tool,  is  called  cucumber , I have no idea if you ask me I could have named it "Potato"(goes well with everything and easy to understand 😂)

Well, According to its founder.....

My wife suggested I call it Cucumber (for no particular reason), so that's how it got its name. I also decided to give the Given-When-Then syntax a name, to separate it from the tool. That's why it's called Gherkin (small variety of a cucumber that's been pickled. It's a little cucumber that's been pickled in a brine, vinegar, or other solution and left to ferment for a period of time.)

We have 3 files created under the framework.

1. Feature file-It contains high level description of the test scenario in Gherkin language(Plain English text) once this is run it generated a code which can be copied to step definition file

It consist of --

Feature-details of current test script which is to be execute

Scenario- expected out come test script

Scenario Outline- In order to achieve data drive approach we use scenario outline

Note- Either you used scenario or scenario outline(can't be used together)

Keywords---Given, When Then, And (You can also use * instead of keywords)

2. Step definition file- It maps the test case steps in the feature files to code

3. Runner class- Cucumber uses Junit  class will have Junit annotation@RunWith and @CucumberOptions used for exciting the test cases by accessing you feature file and step definition file

4. Dependencies Required- There are few depedencies required in oder to run cucumber, as there are depdencies mentioned in  maven repository   Don't use them when tagged with io.cucumber here it doesn't work info.cukes is a safe option

Let's start with implementation on you fav shopping website Amazon.

I have created 3 packages for feature file, step definiton file and Runner class

Create feature file always with .feature extension



Once this is done you can create class test runner and step definition under respective package


Next step is to  define our feature file with scenarios You can used either "Scenario" or "Scenario outline " (for parameterisation)  

An example of scenario

Feature: To test login feature file 


Scenario: Amazon Login

Given user launching Chrome browser

Given user navigates to application

When user enters username as "priyanka@gmail.com"

When user clicks on continue

When user enters password as "<India@123>"

When user clicks on Login 

Then user verify login success status

And user close browser


For achieving  data drive approach we use  "Scenario Outline" with keyword "Examples"

Scenario Outline:

Given user launching Chrome browser

Given user navigates to application

When user enters username as "<username>"

When user clicks on continue

When user enters password as "<password>"

When user clicks on Login 

Then user verify login success status

And user close browser



Examples:

| username | password |

| johns@outlook.com | mercury25 |

| priyanka@gmail.com | India@123 |


To comment something on feature file you need to use # as shown in below screenshot
t
In the above code John is a fake login  and Priyanka is a genuine login
so the code will try to enter John username and password and give error
while with second login and password it will launch the Amazon cart page

Once this is done you can run the  feature file in order to generate code for stepdefintion class


This code will be generated for all you scenarios  in console copy and paste generated code in your step definition file and start writing you own test scripts

An example of code generated once feature file is run
Generated step definition file after adding the code for amazon login

package Stepdefinition;
//A step definition is the actual code implementation of the feature mentioned in the feature file.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Stepdef {
WebDriver driver;
//here we are itializing the chrome browser
@Given("^user launching Chrome browser$")
public void user_launching_Chrome_browser() throws Throwable {
     System.out.println("Launching chrome Browser");
 System.setProperty("Webdriver.chrome.driver", "chromedriver");
   driver = new ChromeDriver();
     
}
// fetching the Amazon link
@Given("^user navigates to application$")
public void user_navigates_to_application() throws Throwable {
           driver.get(""https://www.amazon.in/ap/signin?")
}

@When("^user enters username as \"([^\"]*)\"$") 
public void user_enters_username_as(String username) throws Throwable {
System.out.println("enters username"+ " "+ username );//username is picked from the feature file
      driver.findElement(By.xpath("//input[@name='email']")).sendKeys(username);
       
//Javascript executor is used in order confirm that it click the web element
}
@When("^user clicks on continue$")
public void user_clicks_on_continue() throws Throwable {
 WebElement bt1 = driver.findElement(By.xpath("//input[@id='continue']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", bt1);
}
@When("^user enters password as \"([^\"]*)\"$")
public void user_enters_password_as(String password) throws Throwable {
System.out.println("enters password");
      driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);//picked from the feature file
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}
@When("^user clicks on Login$")
public void user_clicks_on_Login() throws Throwable {
WebElement bt2= driver.findElement(By.xpath("//input[@id='signInSubmit']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", bt2);
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
//Here we are trying to fetch the title after successful login if after login title which is "Amazon.in Shopping Cart" is is matching with the expected output  then we can say "user on correct webpage" if it has different title then it will print "user on the wrong webpage"
@Then("^user verify login success status$")
public void user_verify_login_success_status() throws Throwable {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
String title= driver.getTitle();
System.out.println(title);
if(title.contentEquals("Amazon.in Shopping Cart")) {
System.out.println("User on the corrrect web page");
}
else{
System.out.println("User on wrong webpage");
}
}

@Then("^user close browser$")
public void user_close_browser() throws Throwable {
  driver.quit();
}

}




Once you are done with the stepdefinition then go to runner class and copy and paste the code

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

@RunWith(Cucumber.class)
@CucumberOptions(
features="mention path of your feature file",
glue={"Stepdefinition"})
     
public class Testrunner {
}
To find out path of "feature file" right click on feature go to properties and copy the path





Once this is done You can run the runner class by right clicking and running it as Junit test

As per the code we have picked John as a fake user and priyanka as genuine user so the result should be 
in first case "user in wrong webpage" and in later case "user on the correct webpage"
You can also check the execution here


Reporting

Now you can check the reporting part:-
you just need to add format under @CucumberOptions  in runner class

format={"pretty","html:test-output","json:json_output/cucumber.json","junit:junit/junit_xml/cucumber.xml"})


"html:test-output" will create a folder test-ouput and result of your run

"json:json_output/cucumber.json"- create folder json_output and file cucumber.json

"junit:junit/junit_xml/cucumber.xml"create folder Junit under which junit_xml for result

    


Code would be like

@RunWith(Cucumber.class)

@CucumberOptions(

features="/Users/priyankac/Documents/workspace/Testproj/src/main/java/Features/abc.Feature",

glue={"Stepdefinition"}, format={"pretty","html:test-output","json:json_output/cucumber.json","junit:junit/junit_xml/cucumber.xml"})

     

public class Testrunner {

}


Run the test  runner class again
                                then go to the project and click on "refresh"


All 3 folder will  be created

Open Json_output and click on file it will give you detailed result for your test run



In index.html format report you click on "test-output" folder go to "index.xml" right click ->copy the path and paste it in the browser


The execution report will look like this






Thank you



Comments

Post a Comment

Popular posts from this blog

Jmeter 5.4.1- Config Elements - Part-03

  Part-01- Installation of Jmeter and HTTP's Recorder click  here Part 02--Previous blog on Assertion Config elements in Jmeter are used to configure or modify the samplers requests made to the server. These elements are added at the same or higher level of the samplers that we want to configure  Let' start with  CSV data config As the name suggest it used to read data from CSV first we need to put data in variables and then use the variables in sampler request. create a new test plan add CSV data set config Add a Thread Group and then add Sampler "Java Request"  Create a CSV file  with some data (Name and Data) and save it  Now go to Jmeter CSs data set config browse and upload the css file create Make few more changes in place of  variable name - Name and Dept Ignore first line - True Delimeter - \t (as suggested) Now move on the Sampler-" Java Request" and rename it with header elements of CSV As we have Name and d...

Beginners tutorial -:working with JMeter in Mac and windows - Part-01

  Prequisite   you should have Java downloaded in your system with Home path set under environment variables.(as of today Java version 8 and higher are required fro jmeter ) for help check out this link Note Always run the jmeter on your secondary browser,  if you give the primary browser for proxy settings then your internet connection will be disrupted for the browser as well as system For ex if you have chrome and firefox and your primary or default browser is chrome then do all the proxy setting in firefox so it won't hamper the system Internet connection  if you have safari as your default browser in your mac os then set proxy in chrome/firefox  MAC Download jmeter from the link  here click on the hypelink under section Binaries  "Apache JMeter( 5.3 ). tgz" file  for Mac   Tar file will get downloaded Double click on the tar file to unzip  once you open the folder  got to bin and search for jmeter.sh file this is a executa...