Testbytes IN website

Selenium Automation Testing With Cucumber Integration

October 19th, 2018
Selenium Automation Testing With Cucumber Integration

Selenium is a Web Automation Tool and can be used to do regressive automated testing to test any web application.
app testing
Selenium is a power tool though it lacks an ability to bridge the gap between business analyst and technical personnel.
If we follow the old requirement gathering process and obsolete software life cycle then there are maximum chances of building a completely different product as to what client expected.
This happens because of mis-interpretations and Cucumber solves them all. Cucumber is BDD driven framework which test any application on the basis of its behaviour.
It uses Simple Gherkin language which is easy to understand and communication gap would be eliminated.

Video Courtesy : Execute Automation
In this video we will be talking about an introduction to Cucumber and also an high level introduction on BDD and Gherkin.
Prerequisites for using Cucumber
 Before using Cucumber you should have some prerequisites in your system else Cucumber won’t run. Let’s see what those prerequisites are.
1. Selenium Jars.
2. Cucumber Jars : You can download from here. For other jars, just search in Maven repository and either you can download them or add their dependencies in your pom.xml. Following Jars are required:

  • cucumber-core
  • cucumber-java
  • cucumber-junit
  • cucumber-jvm-deps
  • cucumber-reporting
  • gherkin
  • junit
  • mockito-all
  • cobertura

3. Download Cucumber Eclipse plugin in Eclipse from Eclipse Marketplace.
First Project with Cucumber and Selenium
 Now let’s start with Cucumber and Selenium.

  • Create a Java Project with name “CucumberProject” in Eclipse.
  • Add all jars what you have downloaded in earlier section.
  • You can add Jars by clicking on Project -> Properties -> Java Build Path.
  • Now, you are all set to start with Cucumber Feature File. One can make Feature file by first creating a features folder inside the Project. Now, you can create a file inside the folder “Features” with name as “FirstTest.feature”.
  • Your first file is created and now you can start writing scenarios in it to test your web application.

Feature: Reset functionality on login page of Application
Scenario: Verification of Reset button
Given Open Google Chrome and launch the site
When Enter Username and Password
Then Reset the credential
And Close the Browser
The above Scenario explains you about the reset functionality. Line 1 tells you to open a browser which is Google Chrome and launch a a particular application. Next line says to enter the credentials in application. 3rd line says to reset the credentials and then close the browser.

  • Now, you need a test runner to run the feature file because feature file can’t run by itself. First you have to make a RunnerTest Package and then create a testRunner.java class in it.

package RunnerTest ;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features=”Features”,glue={“StepDefinition”})
public class testRunner
{
}

Also Read : 15 Top Selenium WebDriver Commands For Test Automation

Annotations are used to start executing the test. @RunWith annotations helps in executing the test and @CucumberOptions  annotations is used to set properties of your cucumber tests.

  • Now your Feature file is ready and runner file as well. Now comes the test of actual implementation of the steps defined in the scenario. The file in which you will write the implementation of the steps of scenario is called Step Definition file.

Make a package named as StepDefinition in your Project Structure and add “TestSteps.java” file in it. Here, you will actually write your test script to test the application.
package StepDefinition;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class TestSteps {
WebDriver driver;
@Given(“^Open Google Chrome and launch the site$”)
public void open_the_Firefox_and_launch_the_application() throws Throwable
{
System.setProperty(“webdriver.chrome.driver”,”Path of Chrome.exe file”);
driver = new ChromeDriver();
driver.manage().window().maximize();
Driver.get(“URL”);
System.out.println(“This Step open Chrome and launch the site.”);
}
@When(“^Enter Username and Password$”)
public void enter_the_Username_and_Password() throws Throwable
{
driver.findElement(By.name(“uid”)).sendKeys(“username”);
driver.findElement(By.name(“password”)).sendKeys(“password”);
System.out.println(“This step enter the credentials on the login page.”);
}
@Then(“^Reset the credential$”)
public void Reset_the_credential() throws Throwable
{
driver.findElement(By.name(“btnReset”)).click();
System.out.println(“This step click on the Reset button.”);
}
@Then(“^Close the Browser$”)
Public void Close_the_browser() throws Throwwable
{
driver.close();
System.out.println(“This step closes the browser”);
}
}
There are 5 main annotation tags which are uses in Cucumber:

  • Given : This is used for a pre condition.
  • When : This is used for an action which occurs after a pre condition.
  • Then : This occurs for a reaction for an event.
  • And: This adds a particular step in addition to the defined above.
  • But : This adds a negative step.

 
banner
Whatever tags you will be using in Feature file; you will have to use the same tags in the step definition file. In this way each scenario would be mapped with the test steps defined in the step definition file.

  • Now comes the main step, how to run these files. You can run the feature files by right clicking on Test Runner file and click on “Run with” JUnit Test. When you will execute your test feature file you will observe that the Selenium test scripts start running and System.out.println is printing on the console.

Thus, you ran your tests script for a set of data with Cucumber. But if there is a scenario where you want data driven testing or you want to run the same scenario for three times and then only you can pass the data from feature file itself.
Feature: Reset functionality on login page of Application with 2 sets of data
Scenario: Verification of Reset button
Given Open Google Chrome and launch the site
When Enter Username and Password
|username | password |
|user1    | pass1    |
|user2    | pass2    |
Then Reset the credential
And Close the Brows
@When(“^Enter the Username \”(.*)\” and Password \”(.*)\”$”)
public void enter_the_Username_and_Password(String username,String password) throws Throwable
{
driver.findElement(By.name(“uid”)).sendKeys(username);
driver.findElement(By.name(“password”)).sendKeys(password);
}
Only this step needs to be edited and the whole scenario would run for two number of times. This is called data driven testing with Cucumber.
Conclusion
Cucumber is a great tool used for BDD driven testing. This helps to make business analyst, client and technical people at a same level and thus bugs would decrease and the application will become more stable.
You just need to create simple Feature files which is written in English language ( Gherkin ) and thus your test scripts are ready.

Also Read10 Key Factors for Successful Test Automation

Testbytes IN website
Recent Posts
Subscribe
Please enter valid email address

Contact Us
Please type your Name
Please enter valid email address
Please enter phone no.
Please enter message
Testbytes IN website

Search Results for:

Loading...

Contact Us