Domain.Story3.java Source code

Java tutorial

Introduction

Here is the source code for Domain.Story3.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Domain;

import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import static org.junit.Assert.*;
import org.openqa.selenium.*;

import org.junit.Test;

/**
 *
 * @author Zhang Tong
 */
public class Story3 extends BaseTest {
    /** User story 3:
     * As a user,
     * I want to add/remove products to/from shopping cart,
     * So that I can buy products
     * * * * * * * * * * * *
     * Scenario 1: I checkout my shopping cart
     * Scenario 2: I add one item to shopping cart
     * Scenario 3: I remove one item from shopping cart
     */

    // Scenario 1: 
    // Given that I am on the home page
    // When I click the "Checkout" button
    // Then I should see the shopping cart and the "Checkout" title
    @Test
    public void testCheckOut() {
        driver.get("http://store.demoqa.com/");
        //Click the shopping cart button
        driver.findElement(By.className("cart_icon")).click();
        //Wait page forward to checkout page
        waitUntil(d -> d.findElement(By.className("entry-title")).isDisplayed());

        try {
            //Find the checkout title
            String title = driver.findElement(By.className("entry-title")).getText();
            assertTrue(title.contains("Checkout"));
        } catch (NoSuchElementException nseex) {
            fail();
        }
    }

    // Scenario 2: 
    // Given that I am on the all product page
    // When I add the first item to the cart and chekout  
    // Then I should see the item in the shopping cart
    @Test
    public void testAddtoCart() {
        driver.get("http://store.demoqa.com/products-page/product-category/");
        //Find the "Add to Cart" button of the first product and click it
        driver.findElements(By.className("wpsc_buy_button")).get(0).click();
        //Wait adding to cart
        waitUntil(d -> d.findElement(By.linkText("Go to Checkout")).isDisplayed());
        //Click the "Go to Checkout" button in the alert window
        driver.findElement(By.linkText("Go to Checkout")).click();
        //Wait page forward to checkout page
        waitUntil(d -> d.findElement(By.linkText("iPhone 5")).isDisplayed());

        try {
            //Find the link to the product in the checkout page
            String itemTitle = driver.findElement(By.linkText("iPhone 5")).getText();
            assertTrue(itemTitle.contains("iPhone 5"));
        } catch (NoSuchElementException nseex) {
            fail();
        }
    }

    // Scenario 3: 
    // Given that I am on the all product page and bought one item
    // When I checkout and click the remove button  
    // Then I should see the empty shopping cart and the empty content
    @Test
    public void testRemoveItem() {
        driver.get("http://store.demoqa.com/products-page/product-category/");
        //Find the "Add to Cart" button of the first product and click it
        driver.findElements(By.className("wpsc_buy_button")).get(0).click();
        //Wait adding to cart
        waitUntil(d -> d.findElement(By.linkText("Go to Checkout")).isDisplayed());
        //Click the "Go to Checkout" button in the alert window
        driver.findElement(By.linkText("Go to Checkout")).click();
        //Wait page forward to checkout page
        waitUntil(d -> d.findElement(By.xpath("//input[@value='Remove']")).isDisplayed());
        //Click the remove button
        driver.findElement(By.xpath("//input[@value='Remove']")).click();
        //Wait the remove process finished
        waitUntil(d -> d.findElement(By.className("entry-content")).isDisplayed());

        try {
            //Find the empty infos
            WebElement emptyContent = driver.findElement(By.className("entry-content"));
            assertTrue(emptyContent.isDisplayed());
        } catch (NoSuchElementException nseex) {
            fail();
        }
    }

}