Cucumber Selenium WebDriver Integration

Cucumber Behavior Driven Development (BDD), Selenium Functional Testing, Selenium Integration with Selenium WebDriver and Automation Testing Using Cucumber with Selenium.
Post Reply
ramya
Posts: 89
Joined: Fri Nov 23, 2018 2:55 am

Cucumber Selenium WebDriver Integration

Post by ramya » Wed Dec 05, 2018 6:34 am

Explain Cucumber Selenium WebDriver Integration ?

AyubHussain
Posts: 138
Joined: Fri Nov 23, 2018 2:48 am

Re: Cucumber Selenium WebDriver Integration

Post by AyubHussain » Wed Dec 05, 2018 6:36 am

Cucumber Selenium WebDriver Integration:

Cucumber framework can be used to test the web-based applications along with Selenium WebDriver. The test cases are written in simple feature files which are easily understood by managers, non-technical stakeholders and business analysts. And those feature file steps are implemented in step definition file. If you are using maven then you have to add dependencies for Cucumber and WebDriver.

So here is the sample test case we have implemented using Cucumber and WebDriver. As given below, the scenario in feature file is self-explanatory.

Feature: Login Feature File
@selenium
Scenario: Login scenario test for Gmail

Given navigate to Gmail page
When user logged in using username as “userA” and password as “password”
Then home page should be displayed

WebDriver Implementation in Cucumber stepDefinitions:

public class stepDefinition {
WebDriver dr;
@Given("^navigate to gmail page$")
public void navigate(){
dr=new FirefoxDriver();
dr.get("http://www.gmail.com");
}
@When ("^user logged in using username as \"(.*)\" and password as \"(.*)\"$")
public void login(String username,String password){
dr.findElement(By.xpath("//*[@id='Email']")).sendKeys(username);
dr.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(password);
dr.findElement(By.xpath("//*[@id='signIn']")).click();
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Then("^home page should be displayed$")
public void verifySuccessful(){
String expectedText="Gmail";
String actualText= dr.findElement(By.xpath("//*[@id='gbq1']/div/a/span")).getText();
Assert.assertTrue("Login not successful",expectedText.equals(actualText));
}
}

In this test, we have used the Firefox as the browser to test the Gmail login functionality.
Clearly, WebDriver object is a class variable and used across the class.

Given statement initializes the browser and navigates to the page.
When statement logs into the application using the username as “userA” and password as “password”. Both the values ‘username’ and ‘password’ are passed from feature file and both the values to be used in the same order.
Then Statement only validates the conditions after logging into the application.

This is a sample test describing the usage of Cucumber and Selenium. You can create multilayer architecture depending upon your project requirement.

Post Reply

Return to “Selenium Integration with Cucumber”