Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.support.ui.Select;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import static junit.framework.TestCase.assertEquals;
- import static junit.framework.TestCase.assertNotNull;
- public class BasicTest extends TestHelper {
- private String username = "taavi";
- private String password = "test";
- @Test
- public void titleExistsTest(){
- String expectedTitle = "ST Online Store";
- String actualTitle = driver.getTitle();
- assertEquals(expectedTitle, actualTitle);
- }
- /*
- In class Exercise
- Fill in loginLogoutTest() and login mehtod in TestHelper, so that the test passes correctly.
- */
- @Test
- public void loginLogoutTest(){
- login(username, password);
- // assert that correct page appeared
- WebElement adminHeader = driver.findElement(By.linkText("Admin"));
- assertNotNull(adminHeader);
- logout();
- }
- /*
- In class Exercise
- Write a test case, where you make sure, that one can’t log in with a false password
- */
- @Test
- public void loginFalsePassword() {
- login(username, "aaaaa");
- WebElement invalidLoginNotice = driver.findElement(By.id("notice"));
- assertNotNull(invalidLoginNotice);
- }
- //Add 2x of one product and 1x of second. Then check if there's 3 items in the cart.
- @Test
- public void addItemsToTheCart() throws InterruptedException {
- addItem("B45593 Sunglasses_entry");
- addItem("B45593 Sunglasses_entry");
- addItem("Sunglasses 2AR_entry");
- TimeUnit.SECONDS.sleep(1); //TODO: Find better solution
- WebElement cartTable = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
- List<WebElement> rows = cartTable.findElements(By.tagName("tr"));
- Integer totalItems = 0;
- for (int i = 0; i < rows.size() - 1; i++) {
- String ammount = rows.get(i).findElements(By.tagName("td")).get(0).getText();
- int digitSize = ammount.length()-1;
- Integer ammountValue = Integer.valueOf(ammount.substring(0,digitSize));
- totalItems += ammountValue;
- }
- assertEquals(new Integer(3), totalItems);
- }
- @Test
- public void changeItemQuantityInTheCart() throws InterruptedException {
- Map<String, Integer> expectedCollection = new HashMap<String, Integer>();
- addItem("B45593 Sunglasses_entry");
- addItem("B45593 Sunglasses_entry");
- addItem("Sunglasses 2AR_entry");
- addItem("Sunglasses 2AR_entry");
- addItem("Sunglasses 2AR_entry");
- addItem("B45593 Sunglasses_entry");
- expectedCollection.put("B45593 Sunglasses",3);
- expectedCollection.put("Sunglasses 2AR",3);
- TimeUnit.SECONDS.sleep(2); //TODO: Find better solution
- addOne("B45593 Sunglasses");
- expectedCollection.put("B45593 Sunglasses",expectedCollection.get("B45593 Sunglasses") + 1);
- deleteOne("Sunglasses 2AR");
- expectedCollection.put("Sunglasses 2AR", expectedCollection.get("Sunglasses 2AR") - 1);
- WebElement table = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
- //WebElement table = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
- List<WebElement> rows = table.findElements(By.tagName("tr"));
- Map<String, Integer> actualOutput = new HashMap<String, Integer>();
- for (int i = 0; i < rows.size() - 1; i++) {
- String ammountString = rows.get(i).findElements(By.tagName("td")).get(0).getText();
- Integer ammountInteger = Integer.valueOf(ammountString.substring(0,ammountString.length()-1));
- String name = rows.get(i).findElements(By.tagName("td")).get(1).getText();
- actualOutput.put(name,ammountInteger);
- }
- assertEquals(expectedCollection,actualOutput);
- }
- @Test
- public void deleteItemsFromShoppingCart() throws InterruptedException {
- addItem("B45593 Sunglasses_entry");
- addItem("B45593 Sunglasses_entry");
- addItem("Sunglasses 2AR_entry");
- addItem("Sunglasses 2AR_entry");
- addItem("Sunglasses 2AR_entry");
- addItem("B45593 Sunglasses_entry");
- TimeUnit.SECONDS.sleep(3); //TODO: Find better solution
- TimeUnit.SECONDS.sleep(3); //TODO: Find better solution
- deleteOneRow("B45593 Sunglasses");
- Map<String, Integer> expectedCollection = new HashMap<String, Integer>();
- expectedCollection.put("Sunglasses 2AR",3);
- WebElement table = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
- List<WebElement> rows = table.findElements(By.tagName("tr"));
- Map<String, Integer> actualOutput = new HashMap<String, Integer>();
- for (int i = 0; i < rows.size() - 1; i++) {
- String ammountString = rows.get(i).findElements(By.tagName("td")).get(0).getText();
- Integer ammountInteger = Integer.valueOf(ammountString.substring(0,ammountString.length()-1));
- String name = rows.get(i).findElements(By.tagName("td")).get(1).getText();
- actualOutput.put(name,ammountInteger);
- }
- assertEquals(expectedCollection,actualOutput);
- }
- @Test
- public void deleteShoppingCart() throws InterruptedException {
- addItem("B45593 Sunglasses_entry");
- addItem("B45593 Sunglasses_entry");
- addItem("Sunglasses 2AR_entry");
- TimeUnit.SECONDS.sleep(1); //TODO: Find better solution
- driver.findElement(By.xpath("//*[@id=\"cart\"]/form[1]/input[2]")).click();
- String result = driver.findElement(By.xpath("//*[@id=\"notice\"]")).getText();
- assertEquals("Cart successfully deleted.",result);
- }
- @Test
- public void searchProductsByName() {
- WebElement search = driver.findElement(By.xpath("//*[@id=\"search_input\"]"));
- search.click();
- search.sendKeys("B45593");
- int displayedCounter = 0;
- int expectedCounter = 0;
- List<WebElement> entries = driver.findElements(By.className("entry"));
- for (WebElement elem : entries) {
- if(elem.isDisplayed()){
- displayedCounter++;
- }
- if (elem.getAttribute("id").contains("B45593")) {
- expectedCounter++;
- }
- }
- assertEquals(expectedCounter, displayedCounter);
- }
- @Test
- public void searchNonExistingProductsByName() {
- WebElement search = driver.findElement(By.xpath("//*[@id=\"search_input\"]"));
- search.click();
- search.sendKeys("Sandwich");
- int counter = 0;
- int expectedCounter = 0;
- List<WebElement> entries = driver.findElements(By.className("entry"));
- for (WebElement elem : entries) {
- if(!elem.isDisplayed()){
- counter++;
- }
- if (!elem.getAttribute("id").contains("Sandwich")) {
- expectedCounter++;
- }
- }
- assertEquals(expectedCounter, entries.size());
- }
- @Test
- public void filterSunglassesResults() {
- String category = "Sunglasses";
- Integer expectedCounter = 0;
- List<WebElement> entries = driver.findElements(By.className("entry"));
- for (WebElement ele : entries) {
- if (ele.findElement(By.id("category")).getText().equals("Category: " + category)) {
- expectedCounter++;
- }
- }
- assertEquals(expectedCounter, (Integer)filter(category).size());
- }
- @Test
- public void filterOtherResults() {
- String category = "Other";
- Integer expectedCounter = 0;
- List<WebElement> entries = driver.findElements(By.className("entry"));
- for (WebElement ele : entries) {
- if (ele.findElement(By.id("category")).getText().equals("Category: " + category)) {
- expectedCounter++;
- }
- }
- assertEquals(expectedCounter, (Integer)filter(category).size());
- }
- @Test
- public void purchaseItems() {
- addItem("Sunglasses 2AR_entry");
- driver.findElement(By.xpath("//*[@id=\"checkout_button\"]/input")).click();
- WebElement name = driver.findElement(By.xpath("//*[@id=\"order_name\"]"));
- WebElement address = driver.findElement(By.xpath("//*[@id=\"order_address\"]"));
- WebElement email = driver.findElement(By.xpath("//*[@id=\"order_email\"]"));
- WebElement payType = driver.findElement(By.xpath("//*[@id=\"order_pay_type\"]"));
- name.click();
- name.sendKeys("Allan Rist");
- address.click();
- address.sendKeys("Liivi 2");
- email.click();
- email.sendKeys("test@gmail.com");
- payType.click();
- Select dropdownSelect = new Select(payType);
- dropdownSelect.selectByValue("Purchase order");
- driver.findElement(By.xpath("//*[@id=\"place_order\"]/input")).click();
- String confirmation = driver.findElement(By.xpath("//*[@id=\"order_receipt\"]/h3")).getText().trim();
- assertEquals("Thank you for your order",confirmation);
- }
- }
- //*[@id="category"]/text()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement