Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5489

How to Implement Cucumber with selenium

$
0
0

introduction

Cucumber is a BDD testing framework that helps to understand the gap between software developers and business developers, we can use this to implement BDD approach with Selenium.

BDD stands for Behaviour driven development– BDD mainly deals with the behaviour of the application, we only focus on the behaviour of the application developed. In BDD, all the requirements are written in simple english language, which is very easy to understand for all.

Cucumber uses Gherkin keywords that are very easy to understand and tells the flow of the tests. Gherkin keywords are used in feature files to write the test flow in simple and efficient way.

List of jar files required for cucmber:-

  1. cucumber-core
  2. cucumber-java
  3. cucumber-junit
  4. cucumber-jvm-deps
  5. cucumber-reporting
  6. gherkin
  7. junit
  8. mockito-all
  9. cobertura

 

Gherkin Keywords

Gherkin is a simple readable language that cucumber understands and describes the application flow in terms of simple steps. In cucumber, each line starts with a gherkin keyword.
Some of the Gherkin keywords available in Cucumber are:-

  • Feature
  • Scenario
  • Given
  • When,Then,And,But
  • Background
  • Scenario Outline
  • Examples

Feature File

A feature file contains the steps in which the test execution flow is defined. A feature file has .feature file extension.

for instance, let’s consider the file name to be My_feature_file in that case, user needs to save the feature file as My_feature_file.feature

Feature File example:-

Feature file to test the login functionality of a login page.

@smokeTest
Feature: To test the login functionality

Scenario: Test login with valid credentials
Given Open Firefox and navigate to Login page
When valid EmailID and password is entered
Then user should logged in successfully

Test Runner Class

Create a java class to run the feature files; these classes may or may not have any code. this class just need annotations that would make it understand that feature files would run on running this class, and we can specify the location of feature file and steps class file that will be invoked on running this class.

The @CucumberOptions annotation is used to specify the path to feature files, path to step definitions etc. whereas, the @RunWith annotation is used to specify the used test runner, a test class can have only one test runner.

package Login_cucumber;

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

@RunWith(Cucumber.class)
@CucumberOptions(tags={"@smokeTest"},
features = {"Cases"},glue={"StepsToTest"})
public class TestRunner {
}

On running the feature file, the tests will start executing, but all the test scenarios will not be executed. On running this file, cucumber will automatically change the steps of feature file into method skeletons.

Follow the steps mentioned below:-

1. Create a new package say “StepsToTest” in the same project

2. Create a class inside it.

3. Simply copy the step definition snippets from console

4. Paste the code inside it and remove the dummy code.

This class will be the actual test class in which the execution flow of the test will be mentioned.

public class LoginSteps {
   
    WebDriver driver;    

    @Given("^Open Firefox and navigate to Login page$")
    public void given_Open_Firefox_and_navigate_to_Login_page() throws Throwable
    {
       driver = new FirefoxDriver();
       driver.get("http://LoginPage.com");
       driver.manage().window().maximize();")
    }
     
    @When("^valid EmailID and password is entered$")
    public void when_valid_EmailID_and_password_is_entered() throws Throwable
    {
        driver.findElement(By.xpath(".//*[@id='login']")).sendKeys("admin");
        driver.findElement(By.xpath(".//*[@id='password']")).sendKeys("password");
    }
     
    @Then("^user should logged in successfully$") throws Throwable
    public void then_user_should_logged_in_successfully()
    {
    driver.findElement(By.xpath(".//*[@id='submit']")).click();
    System.out.println("User Logged in successfully");
    }
}

This was the basic concept of BDD through cucumber, in our next blog, we will focus on the parametrization concept in cucumber.

Please share your valuable feedback in the comment section below, Keep Testing!!


Viewing all articles
Browse latest Browse all 5489

Trending Articles