To run Cucumber test, we need to define a Cucumber runner using @RunWith annotation.
package me.simplejavautomation;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/features",
glue = { "me.simplejavautomation.stepdefs" })
public class CucumberTestRunner {
}
Cucumber.class is derived from JUnit runner. There are two attributes you need to define in CucumberOptions. One is 'features' to let Cucumber runner know where features are, it's the directory path of features. Another one is 'glue' to let Cucumber know where your Step Definitions are, it's the java package.import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/features",
glue = { "me.simplejavautomation.stepdefs" })
public class CucumberTestRunner {
}
Second is to define features using Gherkin. It's a text file with .feature as the extension, and store in your features directory.
Feature: Sign In
Scenario: Login Failure
Given User is on Home Page
And Sign In link is displayed
When User clicks the Sign In link
And User enters wrong "username" and "password"
Then User should see the failed message
Scenario: Login Failure
Given User is on Home Page
And Sign In link is displayed
When User clicks the Sign In link
And User enters wrong "username" and "password"
Then User should see the failed message
The third is to define the step definition which is the glue between features and Java functions.
package me.simplejavautomation.stepdefs;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class SignInSteps {
private WebDriver driver;
@Given("User is on Home Page")
public void user_is_on_Home_Page() {
driver = new ChromeDriver();
driver.get("https://www.airnewzealand.co.nz/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Given("Sign In link is displayed")
public void sign_In_link_is_displayed() {
}
@When("User clicks the Sign In link")
public void user_clicks_the_Sign_In_link() {
driver.findElement(By.id("login-button")).click();
}
@When("User enters wrong username\"(.*)\" and password\"(.*)\"")
public void user_enters_wrong_username_and_password(String username, String password) {
driver.findElement(By.id("xv_username")).sendKeys(username);
driver.findElement(By.id("header-myairnz-password")).sendKeys(password);
driver.findElement(By.cssSelector("input[name='login']")).click();
}
@Then("User should see the failed message")
public void user_should_see_the_failed_message() {
String alertMessage = driver.findElement(By.cssSelector("div[class='airnz-Alert__message']"))
.findElement(By.xpath(".//p")).getText();
assertEquals("To sign in to our website you now need to be ...", alertMessage);
}
}
After you have done this, just simply execute JUnit Test under CucumberTestRunner class in your eclipse. You will see that it will follow the steps defined in features and execute them one by one.import static org.junit.Assert.assertEquals;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class SignInSteps {
private WebDriver driver;
@Given("User is on Home Page")
public void user_is_on_Home_Page() {
driver = new ChromeDriver();
driver.get("https://www.airnewzealand.co.nz/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Given("Sign In link is displayed")
public void sign_In_link_is_displayed() {
}
@When("User clicks the Sign In link")
public void user_clicks_the_Sign_In_link() {
driver.findElement(By.id("login-button")).click();
}
@When("User enters wrong username\"(.*)\" and password\"(.*)\"")
public void user_enters_wrong_username_and_password(String username, String password) {
driver.findElement(By.id("xv_username")).sendKeys(username);
driver.findElement(By.id("header-myairnz-password")).sendKeys(password);
driver.findElement(By.cssSelector("input[name='login']")).click();
}
@Then("User should see the failed message")
public void user_should_see_the_failed_message() {
String alertMessage = driver.findElement(By.cssSelector("div[class='airnz-Alert__message']"))
.findElement(By.xpath(".//p")).getText();
assertEquals("To sign in to our website you now need to be ...", alertMessage);
}
}
No comments:
Post a Comment