Test Automation With Cucumber

Test Automation

What Is Cucumber

As per my understanding cucumber is a framework that is built tagetting the user acceptance testing scenarios. In other words it is forcused on making the human readable test cases as example scnarios. So anyone who is checking or creating the script can understand the scope, failed and passed scenarios and where did the fail scnarios were.

Other than than cucumber + selenium will provide you few awesome reports ( this might make you automate everyting :D )

The Basic Ingredients

  • IDEA or Eclipse
  • Java - gradle
  • Selenium
  • Cucumber
  • JUnit or TestNG

How It Works


File Structure

Do you remember Selenium ?

If you do well It's almost the same. But a bit more interesting. In other words what this does is letting any user make an script which executes your selenium test methods. In this setup I'm forcusing more on Cucumber with Selenium + Junit + Java ( You can use TestNG but with Junit it's much easier to set it up.)

Hooks.java : Hooks file is where we store our pre and post executable methods. For example:

  • Setting up Driver
  • Open Browser
  • Set up Database connection
  • Close Databse Connection
  • Terminate Driver
To do this task in Hooks.java class you need to impoet the "cucumber.api.java.Before;" and "cucumber.api.java.After;" annotations. Rest is almost the same like TestNG or JUnit. You can use the imported annotations and set methods to be executed at the Start or End of the test execution.
Let's check the hooks file in out project:

package stepDefinition;

import cucumber.api.java.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Hooks {
public static WebDriver driver;
private String OS = null;

private String getOsName(){
if(OS == null) { OS = System.getProperty("os.name"); }
return OS;
}

@Before
public void setupdriver() throws Throwable{
if(getOsName().matches(".*Windows.*")){
System.out.println("Gecko driver is set with the firefox driver : " + getOsName() );
System.setProperty("webdriver.gecko.driver", "ExtenalLib\\geckodriver_ff53.exe"); }
else {
System.out.println("Gecko driver is set with the firefox driver : " + getOsName());
System.setProperty("webdriver.gecko.driver","ExtenalLib\\geckodriverlinux");
}
driver = new FirefoxDriver();
}
}

What's on the hooks class : Normally in our QA's use windows and Servers run in Linux. Since we use a selenium version which is grater than 3. we need to use the gecodriver.(Download the gecodriver which is relevent to your firefox version) gecodriver for windows and linux have two separate executable files. So we need to check the OS and select set the gecodriver accordingly. That, we have configured in the Hooks class. So It will be executed all the time Before a test executes.

TestSteps.java : Test steps file will have all the test methods with a human readable term assigned to access it. It is the same thing we do in TestNG using the @Test annotation. But here the test steps are broken in to few.
Let's check the file in our project:

package stepDefinition;

import cucumber.api.java.en.Given;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import services.*;
import data.*;
import java.util.concurrent.TimeUnit;

public class TestStepsForWeb {
WebDriverWait wait = new WebDriverWait(BeforeAndAfterHooks.driver,60);

@Given("^User is on Web Home Page in env: \"([^\"]*)\"$")
public void UserSelectAnEnv(String Env) throws Throwable {
EnvSelection SelectedEnv = new EnvSelection();
SelectedEnv.SelectEnv(Env);
}

@Given("^click on the quote button$")
public void click_on_the_quote_button() throws Throwable {
BeforeAndAfterHooks.driver.findElement(By.xpath("html/body/div[2]/main/div[1]/div[1]/a")).click();
}
}

What happens in the TestSteps : This is a very simple step. All you have to do is import the Given or Then etc. ( cucumber.api.java.en.Given ) from cucumber and define the step in a way that users can understand. For example if you check the above code using
"@Given("^User is on Web Home Page in env: \"([^\"]*)\"$")"
You can let the user use the sentence "User is on Web Home Page in env: live_UK" and execute the test method below it. It will take pass 'live_UK' as a String to the methods 'public void UserSelectAnEnv(String Env)' so the Env = "live_UK". Just like that you can use any kind of complex method and let a user use a meaning full sentence to call the method in the script. BOOM !! That's all about the teststeps class.

TestRunner.java : As far as I feel this is the most important class here. The reason is this makes the mappings in between.
Let's check the class first:

package cucumber;

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

@RunWith(Cucumber.class)

@CucumberOptions(
glue={"stepDefinition"},
tags = {"@Test"},
features = ("src\\test\\java\\Feature"),
plugin = {"html:CucumberReports/cucumber-html-report"}
)
public class testRunner { }

What happens in the TestRunner : In this class the most important part is @cucumberOptions. In cucumber options you can glue (connect) the test steps classes. When you add the test feature file it will look for the step definitions and methods in feature. In our project we have added more options to cater the need of reporting. But this setup will work fine with your first project ;)

Feature.feature (The Feature File): Guess what ! You are the last part of your cucumber. Feature file is where you script the tests using the your predefined steps in TestSteps.java file.
NOTE: By any chance if you have inseted a step which is not in the TestSteps.java file, in the error log you will have the method with an empty body so you can copy and pase and create the body in TestSteps.java

Let's take a look at a Feature file

Feature: Testing Environments ------> Feature Name

@live------> Tag (optional)
Scenario: Test live------> Scenario Name
Given User is on Web Home Page in env: "live"------> Test Steps
Then terminate Driver

More About the Featue File : In the Feature file the order that you have to create it is <>Feature : Feature Name>>>> Scenario : Scenario Name >>>> Given : Test Steps. Other than that you can add tags and configure that in the TestRunner.java class under CucumberOptions selecting which of the tagged test should be executed. As the last tip for a scenario you can use multiple sets of data and pass them as Example scenarios making a data table in the feature file. In the WebFlow menu icon you can try it out and get to know how to create such a file :)

That's all about it ;) HAPPY SCRIPTING !!

Comments

Popular posts from this blog

QA Engineering Performance Analysis: Don’t underestimate the power of the 99th percentile

Test Automation With Robot Framework