public abstract class BasePage { protected WebDriver driver; final String HOMEPAGE = "https://www.ultranauts-store.com/"; protected BasePage (WebDriver driver) { this.driver = driver; } public void goToHomepage() { driver.get(HOMEPAGE); } }
public class Navigation extends BasePage { final By SEARCH_BOX = By.cssSelector(".search-field"); final By PRODUCT_GRID_TITLE = By.cssSelector(".product-tile .tile-body a"); protected Navigation(WebDriver driver) { super(driver); } public void search(String s) { WebElement searchField = driver.findElement(SEARCH_BOX); searchField.sendKeys(s); searchField.sendKeys(Keys.ENTER); } }
public class AccountPage extends BasePage { final String LOGIN_PAGE = HOMEPAGE + "account"; final By LOGIN_USER = By.cssSelector("#login-form-email"); final By LOGIN_PASS = By.cssSelector("#login-form-password"); final By LOGIN_SUBMIT = By.cssSelector(".btn-login"); protected AccountPage (WebDriver driver) { super(driver); } public void login(String user, String pass) { driver.findElement(LOGIN_USER).sendKeys(user); driver.findElement(LOGIN_PASS).sendKeys(user); driver.findElement(LOGIN_SUBMIT).click(); } }
public class CheckoutPage extends BasePage { final String CHECKOUT_PAGE = HOMEPAGE + "checkout"; final By CHECKOUT_NEXT = By.cssSelector("#btn-next"); final By CHECKOUT_FIRST_NAME = By.cssSelector("#first-name"); final By CHECKOUT_LAST_NAME = By.cssSelector("#last-name"); final By CHECKOUT_EMAIL = By.cssSelector("#email"); final By CHECKOUT_ADDRESS1= By.cssSelector("#address1"); final By CHECKOUT_CITY = By.cssSelector("#city"); final By CHECKOUT_STATE = By.cssSelector("#state"); final By CHECKOUT_POSTAL = By.cssSelector("#postal"); final By CHECKOUT_PHONE = By.cssSelector("#phone"); protected CheckoutPage (WebDriver driver) { super(driver); } protected void next() { driver.findElement(CHECKOUT_NEXT).click(); } public void populateCustInfo(Map<String, String> customerList) { driver.findElement(CHECKOUT_FIRST_NAME).sendKeys(customerList.get("first")); driver.findElement(CHECKOUT_LAST_NAME).sendKeys(customerList.get("last")); driver.findElement(CHECKOUT_EMAIL).sendKeys(customerList.get("email")); driver.findElement(CHECKOUT_ADDRESS1).sendKeys(customerList.get("address1")); driver.findElement(CHECKOUT_CITY).sendKeys(customerList.get("city")); driver.findElement(CHECKOUT_STATE).sendKeys(customerList.get("state")); driver.findElement(CHECKOUT_POSTAL).sendKeys(customerList.get("zip")); driver.findElement(CHECKOUT_PHONE).sendKeys(customerList.get("phone")); } }
Feature: Product Search
As a customer, I want to search for a product and see matching products
Scenario: User searches for a product
Given the User is on the homepage
When the User searches for "strawberry"
Then products containing "strawberry" are displayed
@Given("the User is on the homepage") public void the_user_is_on_the_homepage() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the User searches for {string}") public void the_user_searches_for(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("products containing {string} are displayed") public void products_containing_are_displayed(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); }
public class SearchStepDefs { WebDriver driver; Navigation nav; @Before public void setup() { System.setProperty("webdriver.chrome.driver", "/Users/Shared/webdriver/chromedriver"); driver = new ChromeDriver (); nav = new Navigation(driver); } @Given("the User is on the homepage") public void the_user_is_on_the_homepage() { nav.goToHomepage(); } @When("the User searches for {string}") public void the_user_searches_for(String searchTerm) { nav.search(searchTerm); } @Then("at least {int} products are displayed") public void at_least_products_are_displayed(Integer minCount) { long resultCount = driver.findElements(nav.PRODUCT_GRID_TITLE).stream().count(); Assert.assertTrue(resultCount == 1); } @After public void shutdown() { driver.quit(); } }
Feature: Enhanced Product Search
As a customer, I want to search for a product and see matching products
Scenario Outline: User searches for a product
Given the User is on the homepage
When the User searches for <product>
Then there are <count> product results displayed
Examples:
| product | count |
| "strawberry" | 6 |
| "cashew" | 3 |
| "truffle" | 22 |
@Given("the User is on the homepage") public void the_user_is_on_the_homepage() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the User searches for {string}") public void the_user_searches_for(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("there are {int} product results displayed") public void there_are_product_results_displayed(Integer int1) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); }
public class EnhancedSearchStepDefs { WebDriver driver; Navigation nav; @Before public void setup() { System.setProperty("webdriver.chrome.driver", "/Users/Shared/webdriver/chromedriver"); driver = new ChromeDriver (); nav = new Navigation(driver); } @Given("the User is on the homepage") public void the_user_is_on_the_homepage() { nav.goToHomepage(); } @When("the User searches for {string}") public void the_user_searches_for_product(String searchTerm) { nav.search(searchTerm); } @Then("there are {int} product results displayed") public void there_are_product_results_displayed(Integer expectCount) { long resultCount = driver.findElements(nav.PRODUCT_GRID_TITLE).stream().count(); Assert.assertTrue(resultCount >= expectCount); } @After public void shutdown() { driver.quit(); } }
Feature: Account Login
As a user, I want to login to my account
Background:
Given The user is on the account page
Scenario: a valid User logs in
When the User submits valid login information
Then they are taken to the "Account" page
And the "User Welcome" message is displayed
Scenario: an invalid User logs in
When the User submits invalid login information
Then they are taken to the "Login" page
And the "Login Error" message is displayed
@Given("The user is on the account page") public void the_user_is_on_the_account_page() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the User submits valid login information") public void the_user_submits_valid_login_information() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the User submits invalid login information") public void the_user_submits_invalid_login_information() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("they are taken to the {string} page") public void they_are_taken_to_the_page(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("the {string} message is displayed") public void the_message_is_displayed(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); }
public class AccountStepDef { WebDriver driver; AccountPage account; @Before public void setup() { System.setProperty("webdriver.chrome.driver", "/Users/Shared/webdriver/chromedriver"); driver = new ChromeDriver (); account = new AccountPage (driver); } @Given("The user is on the account page") public void the_user_is_on_the_account_page() { driver.get(account.LOGIN_PAGE); } @When("the User submits valid login information") public void the_user_submits_valid_login_information() { account.login("tester", "password123"); } @When("the User submits invalid login information") public void the_user_submits_invalid_login_information() { account.login("bogus","badpass"); } @Then("they are taken to the {string} page") public void they_are_taken_to_the_page(String pageTitle) { Assert.assertTrue(driver.getTitle().contains(pageTitle)); } @Then("the {string} message is displayed") public void the_message_is_displayed(String statusMsg) { Assert.assertTrue(driver.getPageSource().contains(statusMsg)); } @After public void shutdown() { driver.quit(); } }
Feature: Enhanced Account Login
As a user, I want to login to my account
Scenario Outline: a User attempts to login
Given the User is on the account page
When the User enters <username> and <password> login
Then they are taken to the <redirect> page
And the <status> message is displayed
Examples:
| username | password | redirect | status |
| "customer" | "password123" | "Account" | "Welcome to your account" |
| "bogus" | "badpass" | "Login" | "Invalid username or password" |
| "admin" | "secret1! | "Config" | "Welcome administrator" |
@Given("the User is on the account page") public void the_user_is_on_the_account_page() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the User enters {string} and {string} login") public void the_user_enters_and_login(String string, String string2) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("they are taken to the {string} page") public void they_are_taken_to_the_page(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("the {string} message is displayed") public void the_message_is_displayed(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); }
public class AccountStepDef { WebDriver driver; AccountPage account; @Before public void setup() { System.setProperty("webdriver.chrome.driver", "/Users/Shared/webdriver/chromedriver"); driver = new ChromeDriver (); account = new AccountPage (driver); } @Given("the User is on the account page") public void the_user_is_on_the_account_page() { driver.get(account.LOGIN_PAGE); } @When("the User enters {string} and {string} login") public void the_user_enters_and_login(String user, String pass) { account.login(user,pass); } @Then("they are taken to the {string} page") public void they_are_taken_to_the_page(String pageTitle) { Assert.assertTrue(driver.getTitle().contains(pageTitle)); } @Then("the {string} message is displayed") public void the_message_is_displayed(String statusMsg) { Assert.assertTrue(driver.getPageSource().contains(statusMsg)); } @After public void shutdown() { driver.quit(); } }
Feature: Customer Checkout
As a customer, I want to populate the checkout page with
my information and continue to the payment page
Scenario: A customer completes the checkout page
Given the Customer has selected Checkout from the cart page
When the Customer populates the page with their information:
| first | Tess |
| last | Tester |
| email | checkout@ultranauts.co |
| address1 | 6715 W Colfax Ave |
| city | Lakewood |
| state | CO |
| zip | 80214 |
| phone | 303-232-5115 |
Then the Customer clicks next
Then the Customer is on the payment page
@Given("the Customer has selected Checkout from the cart page") public void the_customer_has_selected_checkout_from_the_cart_page() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the Customer populates the page with their information:") public void the_customer_populates_the_page_with_their_information(io.cucumber.datatable.DataTable dataTable) { // Write code here that turns the phrase above into concrete actions // For automatic transformation, change DataTable to one of // E, List<E>, List<List<E>>, List<Map<K,V>>, Map<K,V> or // Map<K, List<V>>. E,K,V must be a String, Integer, Float, // Double, Byte, Short, Long, BigInteger or BigDecimal. // // For other transformations you can register a DataTableType. throw new io.cucumber.java.PendingException(); } @Then("the Customer clicks next") public void the_customer_clicks_next() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("the Customer is on the payment page") public void the_customer_is_on_the_payment_page() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); }
public class CheckoutStepDefs { WebDriver driver; CheckoutPage checkout; @Before public void setup() { System.setProperty("webdriver.chrome.driver", "/Users/Shared/webdriver/chromedriver"); driver = new ChromeDriver (); checkout = new CheckoutPage (driver); } @Given("the Customer has selected Checkout from the cart page") public void the_customer_has_selected_checkout_from_the_cart_page() { driver.get(checkout.CHECKOUT_PAGE + "?sku=12345-67"); } @When("the Customer populates the page with their information:") public void the_customer_populates_the_page_with_their_information(DataTable customerData) { Map<String, String> customerList = customerData.asMap(String.class, String.class); checkout.populateCustInfo(customerList); } @Then("the Customer clicks next") public void the_customer_clicks_next() { checkout.next(); } @Then("the Customer is on the payment page") public void the_customer_is_on_the_payment_page() { Assert.assertTrue(driver.getTitle().contains("Payment")); } @After public void shutdown() { driver.quit(); } }
@API Feature: API Tests Background: Given User gets an authentication token Scenario Outline: <testid> <group_name> - <test_details> Given Test data is setup from Excel for test <testid> When API Request is sent Then API Response code is verified And API Response body is verified @Group1 Examples: | testid | group_name | test_details | | 1A | User Data | GET New Users | | 1B | User Data | GET Admin Users | | 1C | User Data | POST Create User | @Group2 Examples: | testid | group_name | test_details | | 2D | Location Data | GET All Locations | | 2E | Location Data | GET Trending Locations | | 2F | Location Data | POST Create Location | @Group3 Examples: | testid | group_name | test_details | | 3G | Product Data | GET All Products | | 3H | Product Data | GET Out of Stock Products | | 3I | Product Data | POST Create Product |
public class ApiStepDefs { @Given("User gets an authentication token") public void user_gets_an_authentication_token() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Given("Test data is setup from Excel for test") public void test_data_is_setup_from_excel_for_test(String testId) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("API Request is sent") public void api_request_is_sent() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("API Response code is verified") public void api_response_code_is_verified() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("API Response body is verified") public void api_response_body_is_verified() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } }