Write your first Cucumber Code
Explore and try executing with help of the code below
This is a software tool works with the concept of BDD(Behaviour-Driven Development), which provides an understandable flow of testing with using a Gherkin.
Feature File:
Feature file is written in readable language called "Gherkin". Feature file contains scenarios. Each scenario is further written with steps. Most used keywords are "Given" | "When" | "Then" | "And" |"Feature" | "Scenario" |
#Gherkin language
Feature: Practice Automation Login Test
Scenario: 1.Positive Login Test
Given Login page
When user enter valid username "student"
And user enter valid password "Password123"
And user click submit button
Then user enters to profile page
And user is displayed "Congratulations student. You successfully logged in!"
And log out button is displayed
Scenario: 2.Negative username Test
Given Login page
When user enter invalid username "noStudent"
And user enter valid password "Password123"
And user click submit button
Then error message is displayed
And error text is "Your username is invalid!"
Scenario: 3.Negative password Test
Given Login page
When user enter valid username "student"
And user enter invalid password "Pass123"
And user click submit button
Then error message is displayed
And error text is "Your password is invalid!"
Step Definition File:
A java which is the technical expansion of Gherkin code in Feature file, in which each step is defined using Java coding with help of Selenium.
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
public class ToDoStepDefinition {
protected WebDriver driver;
@Before
public void setup() {
driver = new ChromeDriver();
}
@Given("Login page")
public void loginPage() {
driver.get("https://practicetestautomation.com/practice-test-login/");
}
@When("user enter valid username {string}")
public void userEnterValidUsername(String arg0) {
WebElement user = driver.findElement(By.xpath("//input[@id='username']"));
user.sendKeys(arg0);
String usernameInput = user.getAttribute("value");
Assert.assertTrue(usernameInput.contains(arg0));
}
@And("user enter valid password {string}")
public void userEnterValidPassword(String arg0) {
WebElement user = driver.findElement(By.xpath("//input[@id='password']"));
user.sendKeys(arg0);
String passwordInput = user.getAttribute("value");
Assert.assertTrue(passwordInput.contains(arg0));
}
@And("user click submit button")
public void userClickSubmitButton() {
WebElement user = driver.findElement(By.xpath("//button[@id='submit']"));
user.click();
}
@Then("user enters to profile page")
public void userEntersToProfilePage() {
String title = driver.getTitle();
Assert.assertTrue(title.contains("Logged In Successfully"));
}
@And("user is displayed {string}")
public void userIsDisplayed(String arg0) {
String text = driver.findElement(By.className("post-content")).getText();
Assert.assertTrue(text.contains(arg0));
}
@And("log out button is displayed")
public void logOutButtonIsDisplayed() {
WebElement button = driver.findElement(By.xpath("//a[text()='Log out']"));
Assert.assertTrue(button.isDisplayed());
}
@When("user enter invalid username {string}")
public void userEnterInvalidUsername(String arg0) {
WebElement user = driver.findElement(By.xpath("//input[@id='username']"));
user.sendKeys(arg0);
String usernameInput = user.getAttribute("value");
Assert.assertEquals(usernameInput,arg0);
}
@Then("error message is displayed")
public void errorMessageIsDisplayed() {
WebElement errorMessage = driver.findElement(By.id("error"));
Assert.assertTrue(errorMessage.isDisplayed());
}
@And("user enter invalid password {string}")
public void userEnterInvalidPassword(String arg0) {
WebElement user = driver.findElement(By.xpath("//input[@id='password']"));
user.sendKeys(arg0);
String passwordInput = user.getAttribute("value");
Assert.assertEquals(passwordInput,arg0);
}
@And("error text is {string}")
public void errorTextIs(String arg0) {
String errorMessage = driver.findElement(By.id("error")).getText();
Assert.assertEquals(errorMessage,arg0);
}
@After
public void cleanUp(Scenario scenario)
{
System.out.println("---> scenario: "+scenario.getName());
System.out.println("---> scenario: "+scenario.getStatus());
scenario.log("Karthiq is a good boy...");
driver.quit();
}
}
Runner File:
This code really binds both the feature file and Step Definition file. Moreover tags are can be easily run with help of runner code. Executing runner code is more advisable than running feature file, as multiple feature files can be run with help of runner code.
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions (features = "src/test/java/todo.feature",
glue = "ToDoStepDefinition")
public class TestRunner extends AbstractTestNGCucumberTests {
}