Java tutorial
/* * 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.junit.Before; import org.openqa.selenium.*; import org.junit.Test; /** * * @author Zhang Tong */ public class Story2 extends BaseTest { /** User story 2: * As a user, * I want to view the products, * So that I can decide to buy which products * * * * * * * * * * * * * Scenario 1: I click the "All Product" button * Scenario 2: I search for the product I want */ // Scenario 1: // Given that I am on the main page // When I click the "All Product" button // Then I should see all the products with the title "Product Category" @Test public void testshowAllProduct() { driver.get("http://store.demoqa.com/"); //Find the "All Product" button and click WebElement liElement = driver.findElement(By.id("menu-item-72")); liElement.findElement(By.tagName("a")).click(); //Wait page forward to all product page waitUntil(d -> d.findElement(By.className("entry-title")).isDisplayed()); try { //Find the entryTitle String titleName = driver.findElement(By.className("entry-title")).getText(); assertTrue(titleName.contains("Product Category")); } catch (NoSuchElementException nseex) { fail(); } driver.manage().deleteAllCookies(); } // Scenario 2: // Given that I am on the main page // When I put "Magic" in the search form // Then I should get the introduction of the Magic Mouse @Test public void testSearchProduct() { driver.get("http://store.demoqa.com/"); //Clear the search form driver.findElement(By.className("search")).clear(); //Input "Magic" in the search form driver.findElement(By.className("search")).sendKeys("Magic" + Keys.RETURN); //Wait page forward to product detail page waitUntil(d -> d.findElement(By.linkText("Magic Mouse")).isDisplayed()); try { //Find the "Magic Mouse" Product link WebElement ProductGroup = driver.findElement(By.linkText("Magic Mouse")); assertTrue(ProductGroup.isDisplayed()); } catch (NoSuchElementException nseex) { fail(); } driver.manage().deleteAllCookies(); } }