Brief:
Cucumber is a behavior-driven development (BDD) testing tool which programmers use for testing other softwares. It has its own syntax and language for writing the test cases. It is also called as Given-Then-When language. The test cases are written in gherkin language which has its own writing style in which we write multiple test cases for each feature and each feature contains multiple scenario. Usually these features are written by quality analyst QA Engineers.
Then these features are given to the test programmers who convert these features and scenarios in steps which are written in specific programming language. Basically cucumber is developed in Ruby but it exist for popular languages including Java, Java Script, and Python. It is also called as story based testing tool as the test cases are written like stories and in simple native language.
Need:
It is very tedious job for programmers to write test cases or for QA's to write automation testing code. To overcome this problem, cucumber is one of the best available solutions so that QA's can write acceptance test cases in their native language in the form of features and scenarios and give it to programmers, programmers will write steps. Also for BDD, it is very convenient it will take less time to write tests. Due to automation we don't need to test at least acceptance test manually again and again. Also the business stakeholders can understand the test cases. We can also include stakeholders in the test process. It bridges developers & non-developers in a team from the requirements perspective.
How It Works:
1 . First thing you have to create a feature file which is in non technical language and it has the feature and scenario for generating feature.
Sample feature file looks like this
LogInTest.feature
@login // this is tag for feature file
Feature:As a user I want to be able to login into the system
Scenario:
Given the user is on landing page
When he provides the user name as user@email.com
And he provides the password as user123
And he clicks log-in
Then he should be logged in
In above sample the user@email.com, user123 are the scenario outlines which will pass to steps as parameter above feature will execute with steps.
2 . Second thing you need to create steps for your features that is write programmatic behavior of each scenario in this case we will take example of Java language
We will write steps for above sample feature file.
public class LogInTest {
WebDriver driver = new FirefoxDriver();
@Given("^the user is on landing page$")
public void setup() throws Throwable {
driver.get("www.landingpage.com/login");
}
@And("^he provides the user name as ([^\"]*)$")
public void he_provides_the_last_name_as(String username) throws Throwable {
driver.findElement(By.id("useremail")).sendKeys(username);
}
@And("^he clicks log-in")
public void he_clicks_log-in() throws Throwable {
driver.findElement(By.id("login")).submit();
}
@Then("^he should be logged in$")
public void he_should_be_logged_in() throws Throwable {
String message = "Log in successful";
new WebDriverWait(driver, 10).until (ExpectedConditions.visibilityOfElementLocated(By.className("toast toast-success")));
Assert.assertTrue(message.equals(driver.findElement(By.className("toast-message")).getText()));
driver.close();
}
}
3 . Third and the final thing in this we need one Cucumber test class for executing all the test cases that we have written like following is the class that will execute our log in test.
@RunWith(Cucumber.class)
@Cucumber.Options(tags ={"@login"},format = "json:target/rest.json")
public class CucumberTest {
}
Above class will execute the log in feature file and it will give result on console.
Cucumber is a behavior-driven development (BDD) testing tool which programmers use for testing other softwares. It has its own syntax and language for writing the test cases. It is also called as Given-Then-When language. The test cases are written in gherkin language which has its own writing style in which we write multiple test cases for each feature and each feature contains multiple scenario. Usually these features are written by quality analyst QA Engineers.
Then these features are given to the test programmers who convert these features and scenarios in steps which are written in specific programming language. Basically cucumber is developed in Ruby but it exist for popular languages including Java, Java Script, and Python. It is also called as story based testing tool as the test cases are written like stories and in simple native language.
Need:
It is very tedious job for programmers to write test cases or for QA's to write automation testing code. To overcome this problem, cucumber is one of the best available solutions so that QA's can write acceptance test cases in their native language in the form of features and scenarios and give it to programmers, programmers will write steps. Also for BDD, it is very convenient it will take less time to write tests. Due to automation we don't need to test at least acceptance test manually again and again. Also the business stakeholders can understand the test cases. We can also include stakeholders in the test process. It bridges developers & non-developers in a team from the requirements perspective.
How It Works:
1 . First thing you have to create a feature file which is in non technical language and it has the feature and scenario for generating feature.
Sample feature file looks like this
LogInTest.feature
@login // this is tag for feature file
Feature:As a user I want to be able to login into the system
Scenario:
Given the user is on landing page
When he provides the user name as user@email.com
And he provides the password as user123
And he clicks log-in
Then he should be logged in
In above sample the user@email.com, user123 are the scenario outlines which will pass to steps as parameter above feature will execute with steps.
2 . Second thing you need to create steps for your features that is write programmatic behavior of each scenario in this case we will take example of Java language
We will write steps for above sample feature file.
public class LogInTest {
WebDriver driver = new FirefoxDriver();
@Given("^the user is on landing page$")
public void setup() throws Throwable {
driver.get("www.landingpage.com/login");
}
@And("^he provides the user name as ([^\"]*)$")
public void he_provides_the_last_name_as(String username) throws Throwable {
driver.findElement(By.id("useremail")).sendKeys(username);
}
@And("^he clicks log-in")
public void he_clicks_log-in() throws Throwable {
driver.findElement(By.id("login")).submit();
}
@Then("^he should be logged in$")
public void he_should_be_logged_in() throws Throwable {
String message = "Log in successful";
new WebDriverWait(driver, 10).until (ExpectedConditions.visibilityOfElementLocated(By.className("toast toast-success")));
Assert.assertTrue(message.equals(driver.findElement(By.className("toast-message")).getText()));
driver.close();
}
}
3 . Third and the final thing in this we need one Cucumber test class for executing all the test cases that we have written like following is the class that will execute our log in test.
@RunWith(Cucumber.class)
@Cucumber.Options(tags ={"@login"},format = "json:target/rest.json")
public class CucumberTest {
}
Above class will execute the log in feature file and it will give result on console.