Showing posts sorted by relevance for query cucumber. Sort by date Show all posts
Showing posts sorted by relevance for query cucumber. Sort by date Show all posts

How to install and configure cucumber -java

How to install Cucumber - java in eclipse

How to install cucumber java?

Prerequisite for installing Cucumber 


1. jdk (Latest jdk recomanded).
3.Eclipse plugins

   i. TestNG plugin - http://beust.com/eclipse
   ii. Cucumber eclipse plugin - http://cucumber.github.com/cucumber-eclipse/update-site

You need to download few cucumber related jars.

1. cucumber-core-1.2.5
2. cucumber-html-0.2.3
3. cucumber-java-1.2.5
4. cucumber-jvm-deps-1.0.5
5. cucumber-testng-1.2.5
6. gherkin-2.12.2
7. selenium-server-standalone-3.4.0
8. testng-6.11


To configure cucumber java you need to follow few steps

Step 1  :  Open eclipse and configure workspace 

how to install cucumber- java

Step 2  : Create a new project in eclipse  

You can create an new project from File > New >Java Project . You need to provide a name for that project , say "TestCucumber" and tap on Finish button. You will find "TestCucumber" is created in left side panel of eclipse IDE. 

how to install cucumber- java



Step 3 :  Configure java build path from eclipse .

You need to configure java build path . Means you need to add TestNG library and required external jars with this new project. 

a. Left click on the project. 



b.  Click on the Build Path > Configure Build Path ..




c.  Click on Add library and and TestNG and Click Next > Finish . TestNG will be added for this project.



d. Click on Add External JARs,, and navigate to downloaded location where there cucumber related jars are there. 

add external jars for cucumber

e.  Click on Open the Click on Ok button .. All required jars will be added with the project.

Step 4:  Create 3 package in side the TestCucumber/src 

a.  com.Cucumber.features :  This package consist all feature files. 
b  com.Cucumber.steps : Actual java program where selenium steps are written. 
c. com.Cucumber.Runners : This file consist configuration for the test. 

There I am creating a sample Cucumber project 

Sample feature file

Feature: Login Application
  As a user
  I want to login to the application

  Scenario: Valid user name and password
    Given I launch the url "http://www.fb.com"
    When I enter password and username
    And I click on login button
    Then I should see the Error message

You need to create a file under com.Cucumber.features package . File extension should be .feature
Cucumber feature file


Name of the sample feature file is demo.feature

Now you need to create actual selenium file under com.Cucumber.steps package.

Sample step java file 


package com.Cucumber.steps;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

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

public class DemoLoginSteps {
 WebDriver driver;
 @Given("^I launch the url \"([^\"]*)\"$")
 public void i_launch_the_url(String arg1) throws Throwable {
     // Write code here that turns the phrase above into concrete actions
  System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.get("http://www.fb.com");
     
 }

 @When("^I enter password and username$")
 public void i_enter_password_and_username() throws Throwable {
     // Write code here that turns the phrase above into concrete actions
  driver.findElement(By.name("email")).sendKeys("test");
  driver.findElement(By.name("pass")).sendKeys("cucumber");
  
 }

 @When("^I click on login button$")
 public void i_click_on_login_button() throws Throwable {
     // Write code here that turns the phrase above into concrete actions
 driver.findElement(By.xpath("//*[@data-testid='royal_login_button']")).click();
 }

 @Then("^I should see the login page$")
 public void i_should_see_the_Error_message() throws Throwable {
  try{
     // Write code here that turns the phrase above into concrete actions
   if(driver.findElement(By.name("email")).isDisplayed()){
    Assert.assertTrue(true);
   }else
    Assert.assertTrue(false);
  }catch(Exception NoSuchElementException){
    Assert.assertTrue(false);
  }finally{
   driver.quit();
  }
     
 }

}


Name of the step file is DemoLoginSteps

You can generate step code automatically, but you need to clear few lines of code like " throw new PendingException(); "from each methods and you need to write down the actual  selenium steps as per your requirements. Auto generated file is nothing but a skeleton of the methods.

How to generate step file automatically 

You just need to left click on the feature file and click on the Run As Cucumber feature . You will file unimplemented methods are displayed on eclipse console screen.
How to generate test step in cucumber
Create a RunTest Class under om.Cucumber.Runners package (You need to create om.Cucumber.Runners package under src folder)

Sample runner class


CucumberOptions(
     features = "src/com/Cucumber/features/Login.feature", 
     glue = "com.Cucumber.steps", 
     plugin = { "pretty", "html:target/cucumber-report" }, 
     monochrome = true)

public class RunTest extends AbstractTestNGCucumberTests {

}
Name of runner class is RunTest

How to write runner class using testng


feature  : Location of feature file in your project
glue : package name for the step java file
plugin : Reporting purpose
monochrome :This option can either set as true or false. If it is set as true, it means that the console output for the Cucumber test are much more readable. And if it is set as false, then the console output is not as readable as it should be. For practice just add the code ‘monochrome = true‘ in TestRunner class.

Create a testng file under project to run the RunTest file.

Testng XML

Cucumber with testng

You can run the test case without writing the xml file but testng xml will help you to create CI integration very easily.

How to run test case without testng xml 

 You need to run left click on the feature file and choose Run As Cucumber Feature.

How to run test case using testng xml

You just need to left click on the testng xml file and choose Run As TestNG suite.

How to use Datatable in cucumber


How to implement Datatable in cucumber

How to use Datatable in Cucumber


In my previous post How to install and configure cucumber -java  I have discussed how to install Cucumber and how to run cucumber program in java. In the previous post, I did not show how to parameterize the data. Means previously we passed parameters in the step line. Now we declared the variable in the feature file. So we are using Tables as arguments to Steps. 

I have created one feature file named: dataDriven.feature under com.Cucumber.features package.

how to user cucumber datatable

dataDriven.feature is almost same as the previous demo.feature . The only difference is the "when" .
previously it was " When I enter password and username" and now 

When I provide password and username
      | testname | [email protected] |




dataTable feature file 
 Feature: Login Application  
  As a user  
  I want to login to the application  
  Scenario: Valid user name and password  
   Given I launch the url "https://www.fb.com"  
   When I provide password and username  
    | testname | [email protected] |  
   And I click on login button  
   Then I should see the login page  

Now we need to write down the step file. As you see there are very few changes in scenario/feature file so we can re use all the methods except the step written under "@when"

The implementation of the @when step


 @When("^I provide password and username$")  
  public void i_enter_password_and_username(DataTable rawdata) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  List<List<String>> data = rawdata.raw();  
  driver.findElement(By.name("email")).sendKeys(data.get(0).get(0));  
  driver.findElement(By.name("pass")).sendKeys(data.get(0).get(1));   
  }   


The complete steps 

 package com.Cucumber.steps; import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebDriverException;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.testng.Assert;  
 import cucumber.api.DataTable;  
 import cucumber.api.Scenario;  
 import cucumber.api.java.After;  
 import cucumber.api.java.en.Given;  
 import cucumber.api.java.en.Then;  
 import cucumber.api.java.en.When;  
 public class DemoLoginSteps {  
  WebDriver driver;  
  @Given("^I launch the url \"([^\"]*)\"$")  
  public void i_launch_the_url(String arg1) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");  
  driver = new ChromeDriver();  
  driver.get("http://www.fb.com");  
  }  
  @When("^I click on login button$")  
  public void i_click_on_login_button() throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  driver.findElement(By.xpath("//*[@data-testid='royal_login_button']")).click();  
  }  
  @Then("^I should see the login page$")  
  public void i_should_see_the_Error_message() throws Throwable {  
  try{  
    // Write code here that turns the phrase above into concrete actions  
   if(driver.findElement(By.name("email")).isDisplayed()){  
   Assert.assertTrue(true);  
   }else  
   Assert.assertTrue(false);  
  }catch(Exception NoSuchElementException){  
   Assert.assertTrue(false);  
  }finally{  
   driver.quit();  
  }  
  }  
  @When("^I provide password and username$")  
  public void i_enter_password_and_username(DataTable rawdata) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  List<List<String>> data = rawdata.raw();  
  driver.findElement(By.name("email")).sendKeys(data.get(0).get(0));  
  driver.findElement(By.name("pass")).sendKeys(data.get(0).get(1));   
  }  
 }  




If you want to execute cucumber test from testng xml or just left click on feature file and Run As Cucumber Feature.


Cucumber scenario outline with examples


Cucumber scenario outline with examples


Scenario outline basically replace the value with the datatable value.  Here each row of the data table consider as a new scenario.  For example suppose I want to login into the www.facebook.com site.  As of now we have execute only one scenario. We have provided username and password for login into the facebook site.  If you closely look into the site you can see there 3 different ways to login into the application.

  • We cant provide email id and password.
  • We can provide phone number and password.
  • We can provide user name and password. 
Now we can achieve above mentioned scenarios in 3 different scenario with 3 different input type 

Scenario 1:

Feature: Login Application
  As a user
  I want to login to the application

  Scenario: Valid user email id and password
    Given I launch the url "https://www.fb.com"
    When I enter password and email
    And I click on login button
    Then I should see the login page

Scenario 2:


Feature: Login Application
  As a user
  I want to login to the application

  Scenario: Valid user phone number and password
    Given I launch the url "https://www.fb.com"
    When I enter password and phone no.
    And I click on login button
    Then I should see the login page

Scenario 3:

Feature: Login Application
  As a user
  I wa
  Scenario: Valid user name and password
    Given I launch the url "https://www.fb.com"
    When I enter password and username
    And I click on login button
    Then I should see the login pagent to login to the application

Here you can see the scenario statements are same for all there scenario, only difference is that the parameter(user name / phone number/ email id). Here is the scenario outline feature comes into the picture. 

We can design this login feature in such a way where  scenario will be only one but test data will be 3 and the scenario will be execute 3 times. 

Till now we have used Scenario keyword in feature file but iteration purpose we should use Scenario Outline instead of Scenario.


Feature file : ScenarioOutline.feature

Feature: Login Application
  As a user
  I want to login to the application

  Scenario Outline: Valid user name and password
    Given I launch the url "https://www.fb.com"
    When I provide "<username>" and "<password>"
    And I click on login button
    Then I should see the login page

    Examples: 
      | username          | password |
      |        987654210 | [email protected] |
      | username1          | [email protected] |

Here the Examples annotation  describe the range of iteration , means how many times the test case will execute. In this example the test case will execute 3 times.

Complete steps 
 package com.Cucumber.steps;  
 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebDriverException;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.testng.Assert;  
 import cucumber.api.DataTable;  
 import cucumber.api.Scenario;  
 import cucumber.api.java.After;  
 import cucumber.api.java.en.Given;  
 import cucumber.api.java.en.Then;  
 import cucumber.api.java.en.When;  
 public class DemoLoginSteps2 {  
  WebDriver driver;  
  @Given("^I launch the url \"([^\"]*)\"$")  
  public void i_launch_the_url(String arg1) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");  
  driver = new ChromeDriver();  
  driver.get("https://www.fb.com");  
  }  
  @When("^I provide \"([^\"]*)\" and \"([^\"]*)\"$")  
  public void i_provide_and(String arg1, String arg2) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  driver.findElement(By.name("email")).sendKeys(arg1);  
  driver.findElement(By.name("pass")).sendKeys(arg2);  
  }  
  @When("^I click on login button$")  
  public void i_click_on_login_button() throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  driver.findElement(By.xpath("//*[@data-testid='royal_login_button']")).click();  
  }  
  @Then("^I should see the login page$")  
  public void i_should_see_the_Error_message() throws Throwable {  
  try{  
    // Write code here that turns the phrase above into concrete actions  
   if(driver.findElement(By.name("email")).isDisplayed()){  
   Assert.assertTrue(true);  
   }else  
   Assert.assertTrue(false);  
  }catch(Exception NoSuchElementException){  
   Assert.assertTrue(false);  
  }finally{  
   driver.quit();  
  }    
  }  
 }  


You can run the test from feature file or from testng xml. If you want to execute using testng the you need to write runner class first and then testng xml.

Runner class
 package com.Cucumber.Runners;  
 import cucumber.api.CucumberOptions;  
 import cucumber.api.testng.AbstractTestNGCucumberTests;  
 @CucumberOptions(  
  features = "src/com/Cucumber/features/ScenarioOutline.feature",   
  glue = "com.Cucumber.steps",   
  plugin = { "pretty", "html:target/cucumber-report" },   
  monochrome = true)  
 public class RunTest extends AbstractTestNGCucumberTests {  
 }  


TestNgXML







What is the "Background" keyword in Cucumber feature file


What is the "Background" keyword in Cucumber feature file


When we are writing Feature file in cucumber,  we write multiple Scenarios. All scenarios start with a particular point. Suppose  I am writing the feature file called home_page_facebook and the number of scenarios is there to check the home page functionality. Now if you think about any scenario then you need to login first on the facebook page to reach to the home page. So it is better to write all common or repeated step in one place rather than in all scenarios. To achieve this situation we need to add "Background" keyword in the feature file. Let's understand with an example.  

Feature file : home_page_facebook.feature

Feature: In order to test the home page of the application as a registered user  I want to specify the features of the home page. 

Scenario :  Home page default contents.
Given user on application landing page.
When user enter password
And user enter username
And user click on login button 
Then user navigates to application home page.
And user validate default contents of home page

Scenario : Top banner settings option.
Given user on application landing page.
When user enter password
And user enter username
And user click on login button 
Then user navigates to application home page.
When user click on the setting other 
Then user get the logout option


There is 2 scenario where you can see login steps are common to both the scenarios.  So we can eliminate the common section and put it into a commonplace.  

So I am rewriting the feature file with the Background keyword. 


Feature file : home_page_facebook.feature

Feature: In order to test the home page of the application as a registered user  I want to specify the features of the home page. 

Background: Flow till home page 
Given user on application landing page.
When user enter password
And user enter username
And user click on login button 
Then user navigates to application home page.

Scenario :  Home page default contents.
Then user validate default contents of home page

Scenario : Top banner settings option.
When user click on the setting other 
Then user get the logout option

Now you can see login steps are in the commonplace.
There we use Background keyword.  All steps mentioned in the Background keyword will be executed before every scenario or scenario outline. 

I have provided all my glue code and cucumber feature which I have written in eclipse.

cucumber Background keyword

Step definition 

 package com.Cucumber.steps;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import cucumber.api.java.After;  
 import cucumber.api.java.en.Given;  
 import cucumber.api.java.en.Then;  
 import cucumber.api.java.en.When;  
 public class home_page_facebook {  
      WebDriver driver;  
      @Given("^user on application landing page$")  
      public void user_on_application_landing_page() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");  
           driver = new ChromeDriver();  
           driver.get("http://www.facebook.com/");  
           driver.manage().window().maximize();  
      }  
      @When("^user enter password$")  
      public void user_enter_password() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("pass")).sendKeys("giveourfacebookpassword");  
      }  
      @When("^user enter username$")  
      public void user_enter_username() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("email")).sendKeys("yourusername");  
      }  
      @When("^user click on login button$")  
      public void user_click_on_login_button() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("loginbutton")).click();  
      }  
      @Then("^user navigates to application home page\\.$")  
      public void user_navigates_to_application_home_page() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
        Thread.sleep(3000);  
      }  
      @Then("^user validate default contents of home page$")  
      public void user_validate_default_contents_of_home_page() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
        if(driver.findElement(By.name("q")).isDisplayed())  
        {  
             System.out.println("home page search box diplayed");  
        }else{  
             System.out.println("home page search box not diplayed");  
        }  
      }  
      @When("^user click on the setting other$")  
      public void user_click_on_the_setting_other() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
           driver.findElement(By.id("pageLoginAnchor")).click();  
           Thread.sleep(3000);  
      }  
      @Then("^user get the logout option$")  
      public void user_get_the_logout_option() throws Throwable {  
        // Write code here that turns the phrase above into concrete actions  
        if(driver.findElement(By.xpath("//*[text()='Log out']")).isDisplayed()){  
             System.out.println("logout diplayed");  
        }else{  
             System.out.println("logout not diplayed");  
        }  
      }  
      @After  
       public void tearDown(){  
           driver.quit();  
      }  
 }  


Feature file


 Feature: In order to test the home page of the application as a registered user  
  I want to specify the features of the home page.  
  Background: Flow till home page  
   Given user on application landing page  
   When user enter password  
   And user enter username  
   And user click on login button  
   Then user navigates to application home page.  
  Scenario: Home page default contents  
   Then user validate default contents of home page  
  Scenario: Top banner settings option  
   When user click on the setting other  
   Then user get the logout option  

In my previous cucumber tutorial, I have talked about how to setup cucumber in eclipse and how to run cucumber project. Here is the link

https://www.automation99.com/2017/06/how-to-install-cucumber.html


Basic of maven tool on Automation front



Basic of maven tool on Automation front


A build tool is a tool that automates everything related to building the software project. Maven, is an innovative software project management tool, provide new concept of a project object model(POM) file to manage project's build , dependency and documentation, this session is to target the beginners in helping them understand the basic of maven tool on Automation front

 Why Automation Build Tool 

  1. Accelerate the compile and link processing.
  2. It will minimize the human errors while building a software.
  3. Reduce redundant tasks.
Maven is a 
  1. Building tool.
  2. Dependency management tool
  3. Project management tool  
Core concept in Maven depends on following points 
  1. Dependencies and repositories 
  2. Build Life Cycle 
  3. Phases
What is Dependencies

Dependencies are required jars which are required to build the project.
Include all Jars and plugins in project.

Maven Repository 

3 types of maven repository 
  1. Local Repository : Local repository is a folder location in development machine and it gets crated when you run any maven command for the first time. (By default folder name is .m2)
  2. Central Repository : Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.  http://repo1.maven.org/maven2/
  3. Remote repository :Some time maven dose not find required file in central and due to this build process gets failed.  To overcome this problem Maven provides concept of Remote Repository which is developer's own custom repository containing required libraries or other project jars.  
Sample POM for Remote repository 

how to use Remote repository in POM

Build Life Cycle

Maven has mainly 3 build in life cycles
  1. Clean
  2. Site
  3. Default
What is POM 
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project
  • Maven reads POM file to build the project. 
  • Specifies project information, plugins, goals, dependencies and profiles. 
Sample POM 


Sample pom for cucumber


Sample project structure for Maven project  


maven project sample

In my next post I'll explain how to configure MAVEN project and sample selenium maven project. 


your ad