What is the "Background" keyword in Cucumber feature file
When we are writing Feature file in cucumber, we write multiple Scenarios. All scenarios start with a particular point. Suppose I am writing the feature file called home_page_facebook and the number of scenarios is there to check the home page functionality. Now if you think about any scenario then you need to login first on the facebook page to reach to the home page. So it is better to write all common or repeated step in one place rather than in all scenarios. To achieve this situation we need to add "Background" keyword in the feature file. Let's understand with an example.
Feature file : home_page_facebook.feature
Feature: In order to test the home page of the application as a registered user I want to specify the features of the home page.
Scenario : Home page default contents.
Given user on application landing page.
When user enter password
And user enter username
And user click on login button
Then user navigates to application home page.
And user validate default contents of home page
Scenario : Top banner settings option.
Given user on application landing page.
When user enter password
And user enter username
And user click on login button
Then user navigates to application home page.
When user click on the setting other
Then user get the logout option
There is 2 scenario where you can see login steps are common to both the scenarios. So we can eliminate the common section and put it into a commonplace.
So I am rewriting the feature file with the Background keyword.
Feature file : home_page_facebook.feature
Feature: In order to test the home page of the application as a registered user I want to specify the features of the home page.
Background: Flow till home page
Given user on application landing page.
When user enter password
And user enter username
And user click on login button
Then user navigates to application home page.
Scenario : Home page default contents.
Then user validate default contents of home page
Scenario : Top banner settings option.
When user click on the setting other
Then user get the logout option
Now you can see login steps are in the commonplace.
There we use Background keyword. All steps mentioned in the Background keyword will be executed before every scenario or scenario outline.
Step definition
package com.Cucumber.steps;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.After;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class home_page_facebook {
WebDriver driver;
@Given("^user on application landing page$")
public void user_on_application_landing_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.facebook.com/");
driver.manage().window().maximize();
}
@When("^user enter password$")
public void user_enter_password() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.id("pass")).sendKeys("giveourfacebookpassword");
}
@When("^user enter username$")
public void user_enter_username() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.id("email")).sendKeys("yourusername");
}
@When("^user click on login button$")
public void user_click_on_login_button() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.id("loginbutton")).click();
}
@Then("^user navigates to application home page\\.$")
public void user_navigates_to_application_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
Thread.sleep(3000);
}
@Then("^user validate default contents of home page$")
public void user_validate_default_contents_of_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
if(driver.findElement(By.name("q")).isDisplayed())
{
System.out.println("home page search box diplayed");
}else{
System.out.println("home page search box not diplayed");
}
}
@When("^user click on the setting other$")
public void user_click_on_the_setting_other() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.id("pageLoginAnchor")).click();
Thread.sleep(3000);
}
@Then("^user get the logout option$")
public void user_get_the_logout_option() throws Throwable {
// Write code here that turns the phrase above into concrete actions
if(driver.findElement(By.xpath("//*[text()='Log out']")).isDisplayed()){
System.out.println("logout diplayed");
}else{
System.out.println("logout not diplayed");
}
}
@After
public void tearDown(){
driver.quit();
}
}
Feature file
Feature: In order to test the home page of the application as a registered user
I want to specify the features of the home page.
Background: Flow till home page
Given user on application landing page
When user enter password
And user enter username
And user click on login button
Then user navigates to application home page.
Scenario: Home page default contents
Then user validate default contents of home page
Scenario: Top banner settings option
When user click on the setting other
Then user get the logout option
In my previous cucumber tutorial, I have talked about how to setup cucumber in eclipse and how to run cucumber project. Here is the link
https://www.automation99.com/2017/06/how-to-install-cucumber.html
3 comments
commentsPretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. seorango
ReplyThank you so much for this post. Very well explained in simple language.
Reply