Behavior driven development Framework using cucumber

Step 1 :  Configure Maven with Eclipse Ide

In general maven plugin in eclipse ide can load and associate required external jars with project automatically. Due to this reason most of the SDETS can use maven instead of required jars downloading and associating manually.

Open Ecliplse ide ----------------- go to Help menu ------------- Eclipse market place ------------------- enter maven for eclispe juno --------------------  click Go --------------  click "Install  for maven integration for eclipse (juno) " ------------------------ click  Next until finish ---------------- restart eclipse

Note 1: We need to follow bellow navigation to create Maven Project

In eclipse ide go to File menu --------- New ----------- other --------- Maven ------------- maven project ---------------- Next ------------ select create a simple project check box -----------  click next -------------- enter company name as a group id ------------------ enter project  name as Artifact id ---------------- click finish.

Note 2 : After completion of maven project creation we need to set compiler and JRE Version to Latest

 

Note 3 : After completion of maven project creation and verification of compiler and JRE versions we need to update corresponding project's "pom.xml" like shown bellow. 

jars files :

  1.  junit jars 4.11
  2. cucumber junit jar 1.1.5
  3. cucumber-java jar  1.1.5
  4. selenium 3.0.1

Pom.xml

----------------------

----------------------

<dependencies>       

     <dependency>

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

         <artifactId>   selenium-java </artifactId>

         <version> 3.0.1 </version> 

       </dependency>

-------------------------------------

--------------------------------------

</dependencies>

 

Note 4 : In BDDframework we need to add dependencies code related  to junit, cucumber-java, junit-cucumber, selenium webdriver jars etc in pom.xmil file.

Step 2 : Configure Cucumber plugin to Eclipse IDE (Optional)

In BDD Framework, we need to prepare features files with Jerkin sentences to reprasent test scenarios with cases. To compile those ssentences and to see errors we need to install cucumber plugin for eclipse IDE.

Steps : Help menu in Eclipse ide ------------------ install new software ------------ click add   Name : Cucumber enter url like "http://cucumber.github.com/cucumber-eclipse/update-site"  ------- click "OK" ---- select Cucumber Eclipse plugun --------------- click next  until finish ------------ Restart eclipse IDE.

Step 3 : Develop feature file 

In BDD Framework feature file is used to specify corresponding manual test scenarios with cases. In this feature file we need to define corresponding scenarios with cases by typing english sentences called as Jerkin Statements.

Steps: Right click on test/resources folder in maven project 

                    new ----- package ----- enter name to a package ----- ok ------ Right click on that package ---------- new -------------- file ----- Entr name to that file with .feature as extension ------------ click finish -------- Enter jerkin statements in that file like shown bellow

Feature: Login Test

Scenario : validate userid field for valid data

Given       navigate to gmail

When       enter user id as " some name" and click next

Then      password will be displayed

And      close site

 

Scenario : validate userid field for invalid data

Given       navigate to gmail

When       enter user id as " some name" and click next

Then     error message will be displayed

And      close site

click save. ----> follow above navigation to create multiple feature files in package test/tesources folder of maven project.

Note 1 ; One feature file is having one "Feature:" tag to specify corresponding module in sprint/s/w.

Note 2 : One feature file is having one or more scenarios. Every scenario will be defined with multiple Jerkin Statements By using

Scenario:

Given

Then

when

And

Scenario Outline:

Examples:

But

Note 3 : we are able to write comments in feature file by using "#" called as a jerkin comment.

Note 4 : we need to enclose data in sentenses with ("-")  double quotes.

Step 4 : Develop Methods class

Afteer completion of feature file creation we need to develop automation code w.r.t  sentenses in those feature files. Here methods class is having multiple methods with automation code, but every method reprasents one sentence in feature file.

Public class Gmailmethods

{

WebDriver driver;

@Given("^navigate to gmail$")

public void method1() throws Exception

  {

     System.setProperty("webdriver.chrome.driver", "path of chromedriver exe file");

    driver = new ChromeDriver();

   driver.get("http://www.gmail.com");

  Thread.sleep(5000);

   }

 @When("^ enter userId as \"(.*)\" and click next $)

public void method2(String uid) throws Exception

{

  driver.findElement(By.name("Email")).sendKeys(uid);

  driver.findElement(By.id("next")).click();

  Thread.sleep(5000);

}

 @Then("^pasword will be displayed $")

public void method3() throws Exception

{

try {

       if( driver.findElement(By.name("Passwd")).isDisplayed())

          {

            System.out.println("Test passed");

             }

     }

catch (Exception e)

  {

   System.out.println("Test Failed");

  }

}

 @And("^ close site $")

public void method4() throws Exception

{

 driver.close();

}

 @Then("^ error message will be displayed $")

public void method5()

  {

    try {

              if( driver.findElement(By.name("errormsg_0_Email")).isDisplayed())

             {

                  System.out.println("Test passed");

                }

         }

catch(Excception e)

  {

   System.out.println("Test failed");

 }

 

Step 5 : Develop Runner class :

After completion of feaature files preparation and corresponding methods class development we need to implement Runner class by using Junit. In general we can create feature file in package of test.resource folder of maven project. Methods class and Runner class can save in test/java folder's package.

GmailRunner.java

@RunWith(Cucumber.class)

@Cucumber.Option(features={"path of .feature file"})

public class GmailRunner

{

 

}