Selenium integration with Cucumber BDD

Cucumber – a Behavior Driven Development (BDD) framework which is used with Selenium for performing acceptance testing.  It allows automation of functional validation in easily readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.

Behavior Driven Development is extension of Test Driven Development and it is used to test the system rather than testing the particular piece of code.

I am using eclipse – Eclipse Java EE IDE for Web Developers. Version: Indigo Service Release 2

  1. Create new Java Project. Enter Project Name and click on Next
  2. Click on “Add External Jar” button and add all cucumber jars mention below        Cucumber1
  3. Add Selenium Jar – selenium-server-standalone-2.53.0.jar
  4. Add cucumber plugin in selenium
    1. Help > Install New Software
    2. http://cucumber.github.com/cucumber-eclipse/update-siteCucumber2
  5. Now add new file in project with extension .featureCucumber3
    1. Feature Files: Feature files are essential part of cucumber which gives information about the high level business functionality. Everybody should be able to understand the intent of feature file by reading the first Feature step.
    2. Scenario: Basically a scenario represents a particular functionality which is under test.By seeing the scenario user should be able to understand the intent behind the scenario and what the test is all about. Each scenario should follow given, when and then format. This language is called as “gherkin”.
      1. Given: As mentioned above, given specifies the pre-conditions. It is basically a known state.
      2. When: This is used when some action is to be performed. As in above example we have seen when the user tries to log in using username and password, it becomes an action.
      3. Then: The expected outcome or result should be placed here. For Instance: verify the login is successful, successful page navigation.
      4. Background: Whenever any step is required to perform in each scenario then those steps needs to be placed in Background. For Instance: If user needs to clear database before each scenario then those steps can be put in background.
      5. And: And is used to combine two or more same type of action.Example:Feature: Facebook Login FunctionalityScenario: Test Valid Facebook Login Functionality
        Given user navigates to facebook.com

        When user logs in using Username as “USER”
        And password as “password”
        Then login should be successful
        And Home page should be displayed

    3. Junit Runner:To run the specific feature file cucumber uses standard Junit Runner and specify tags in @Cucumber.import org.junit.runner.RunWith;
      import cucumber.api.CucumberOptions;
      import cucumber.api.junit.Cucumber;

      @RunWith(Cucumber.class)
      @CucumberOptions(
      features = “features”,
      glue = {“stepDefination”},
      plugin={“html:target/cucumber-html-report”}
      )
      public class TestRunner {

      }

    4. Implement feature file:public class smokeTest {WebDriver driver;

      @Given(“^Open firefox and start Gmail$”)
      public void Open_firefox_and_start_application() throws Throwable {

      System.setProperty(“webdriver.chrome.driver”,”C://chromedriver_win32//chromedriver.exe”);
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(“http://www.gmail.com”);
      }

      @When(“^Enter valid Gmail username and password$”)
      public void Enter_valid_username_and_password() throws Throwable {
      // Write code here that turns the phrase above into concrete actions
      driver.findElement(By.name(“Email”)).sendKeys(“LoginUserName”);
      driver.findElement(By.name(“Passwd”)).sendKeys(“LoginPassword”);
      driver.findElement(By.id(“submit”)).click();
      }

      @Then(“^user should be able to login successfully$”)
      public void user_should_be_able_to_login_sccussfully() throws Throwable {
      // Write code here that turns the phrase above into concrete actions
      driver.quit();
      }

  6. Data Driven Testing in cucumber using Scenario Outline: Scenario outlines are usedwhen same test has to be performed with different data set. Let’s take the same example. We have to test login functionality with multiple different set of username and password.

                  Feature: Facebook Login Functionality
                  Scenario: Test Valid Facebook Login Functionality
                  Given user navigates to facebook.com
                  When Enter valid Gmail “<username>” and “<password>”
                   Then login should be successful

Examples:

|username         |password          |

|TestUser1          |password         |

|TestUser2          |password2        |

|TestUser3           |password3        |

Examples are used to pass different arguments in tabular format. Vertical pipes are used to separate two different columns. Examples can contain many different columns.

To implement the Feature file with Examples need to update its implementation with parametrize:

@When(“^Enter valid Gmail \”([^\”]*)\” and \”([^\”]*)\”$”)

public void Enter_valid_username_and_password(String uname,String pwd) throws Throwable {

// Write code here that turns the phrase above into concrete actions

driver.findElement(By.name(“Email”)).sendKeys(uname);

driver.findElement(By.name(“Passwd”)).sendKeys(pwd);

driver.findElement(By.id(“signIn”)).click();
}

 

Advertisements

About samp79

Professional Automation Tester
This entry was posted in Selenium, Set up. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s