daily pastebin goal
25%
SHARE
TWEET

BasicTest

a guest Mar 25th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.junit.Test;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.support.ui.Select;
  5.  
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. import static junit.framework.TestCase.assertEquals;
  12. import static junit.framework.TestCase.assertNotNull;
  13.  
  14. public class BasicTest extends TestHelper {
  15.  
  16.  
  17.     private String username = "taavi";
  18.     private String password = "test";
  19.  
  20.     @Test
  21.     public void titleExistsTest(){
  22.         String expectedTitle = "ST Online Store";
  23.         String actualTitle = driver.getTitle();
  24.  
  25.         assertEquals(expectedTitle, actualTitle);
  26.     }
  27.  
  28.  
  29.     /*
  30.     In class Exercise
  31.  
  32.     Fill in loginLogoutTest() and login mehtod in TestHelper, so that the test passes correctly.
  33.  
  34.      */
  35.     @Test
  36.     public void loginLogoutTest(){
  37.  
  38.         login(username, password);
  39.  
  40.         // assert that correct page appeared
  41.         WebElement adminHeader = driver.findElement(By.linkText("Admin"));
  42.         assertNotNull(adminHeader);
  43.         logout();
  44.     }
  45.  
  46.     /*
  47.     In class Exercise
  48.  
  49.      Write a test case, where you make sure, that one can’t log in with a false password
  50.  
  51.      */
  52.     @Test
  53.     public void loginFalsePassword() {
  54.         login(username, "aaaaa");
  55.         WebElement invalidLoginNotice = driver.findElement(By.id("notice"));
  56.         assertNotNull(invalidLoginNotice);
  57.  
  58.     }
  59.  
  60.     //Add 2x of one product and 1x of second. Then check if there's 3 items in the cart.
  61.     @Test
  62.     public void addItemsToTheCart() throws InterruptedException {
  63.         addItem("B45593 Sunglasses_entry");
  64.         addItem("B45593 Sunglasses_entry");
  65.         addItem("Sunglasses 2AR_entry");
  66.         TimeUnit.SECONDS.sleep(1); //TODO: Find better solution
  67.  
  68.  
  69.         WebElement cartTable = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
  70.         List<WebElement> rows = cartTable.findElements(By.tagName("tr"));
  71.         Integer totalItems = 0;
  72.         for (int i = 0; i < rows.size() - 1; i++) {
  73.             String ammount = rows.get(i).findElements(By.tagName("td")).get(0).getText();
  74.             int digitSize = ammount.length()-1;
  75.             Integer ammountValue = Integer.valueOf(ammount.substring(0,digitSize));
  76.             totalItems += ammountValue;
  77.         }
  78.         assertEquals(new Integer(3), totalItems);
  79.     }
  80.  
  81.     @Test
  82.     public void changeItemQuantityInTheCart() throws InterruptedException {
  83.         Map<String, Integer> expectedCollection = new HashMap<String, Integer>();
  84.         addItem("B45593 Sunglasses_entry");
  85.         addItem("B45593 Sunglasses_entry");
  86.         addItem("Sunglasses 2AR_entry");
  87.         addItem("Sunglasses 2AR_entry");
  88.         addItem("Sunglasses 2AR_entry");
  89.         addItem("B45593 Sunglasses_entry");
  90.  
  91.         expectedCollection.put("B45593 Sunglasses",3);
  92.         expectedCollection.put("Sunglasses 2AR",3);
  93.  
  94.         TimeUnit.SECONDS.sleep(2); //TODO: Find better solution
  95.  
  96.  
  97.         addOne("B45593 Sunglasses");
  98.         expectedCollection.put("B45593 Sunglasses",expectedCollection.get("B45593 Sunglasses") + 1);
  99.  
  100.         deleteOne("Sunglasses 2AR");
  101.         expectedCollection.put("Sunglasses 2AR", expectedCollection.get("Sunglasses 2AR") - 1);
  102.  
  103.         WebElement table = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
  104.  
  105.  
  106.  
  107.  
  108.         //WebElement table = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
  109.         List<WebElement> rows = table.findElements(By.tagName("tr"));
  110.         Map<String, Integer> actualOutput = new HashMap<String, Integer>();
  111.         for (int i = 0; i < rows.size() - 1; i++) {
  112.             String ammountString = rows.get(i).findElements(By.tagName("td")).get(0).getText();
  113.             Integer ammountInteger = Integer.valueOf(ammountString.substring(0,ammountString.length()-1));
  114.             String name = rows.get(i).findElements(By.tagName("td")).get(1).getText();
  115.             actualOutput.put(name,ammountInteger);
  116.         }
  117.         assertEquals(expectedCollection,actualOutput);
  118.     }
  119.  
  120.     @Test
  121.     public void deleteItemsFromShoppingCart() throws InterruptedException {
  122.         addItem("B45593 Sunglasses_entry");
  123.         addItem("B45593 Sunglasses_entry");
  124.         addItem("Sunglasses 2AR_entry");
  125.         addItem("Sunglasses 2AR_entry");
  126.         addItem("Sunglasses 2AR_entry");
  127.         addItem("B45593 Sunglasses_entry");
  128.  
  129.  
  130.  
  131.  
  132.         TimeUnit.SECONDS.sleep(3); //TODO: Find better solution
  133.  
  134.         TimeUnit.SECONDS.sleep(3); //TODO: Find better solution
  135.  
  136.  
  137.         deleteOneRow("B45593 Sunglasses");
  138.         Map<String, Integer> expectedCollection = new HashMap<String, Integer>();
  139.         expectedCollection.put("Sunglasses 2AR",3);
  140.  
  141.         WebElement table = driver.findElement(By.xpath("//*[@id=\"cart\"]/table/tbody"));
  142.         List<WebElement> rows = table.findElements(By.tagName("tr"));
  143.         Map<String, Integer> actualOutput = new HashMap<String, Integer>();
  144.         for (int i = 0; i < rows.size() - 1; i++) {
  145.             String ammountString = rows.get(i).findElements(By.tagName("td")).get(0).getText();
  146.             Integer ammountInteger = Integer.valueOf(ammountString.substring(0,ammountString.length()-1));
  147.             String name = rows.get(i).findElements(By.tagName("td")).get(1).getText();
  148.             actualOutput.put(name,ammountInteger);
  149.         }
  150.  
  151.         assertEquals(expectedCollection,actualOutput);
  152.     }
  153.  
  154.  
  155.     @Test
  156.     public void deleteShoppingCart() throws InterruptedException {
  157.         addItem("B45593 Sunglasses_entry");
  158.         addItem("B45593 Sunglasses_entry");
  159.         addItem("Sunglasses 2AR_entry");
  160.         TimeUnit.SECONDS.sleep(1); //TODO: Find better solution
  161.  
  162.         driver.findElement(By.xpath("//*[@id=\"cart\"]/form[1]/input[2]")).click();
  163.         String result = driver.findElement(By.xpath("//*[@id=\"notice\"]")).getText();
  164.         assertEquals("Cart successfully deleted.",result);
  165.     }
  166.  
  167.  
  168.     @Test
  169.     public void searchProductsByName() {
  170.         WebElement search = driver.findElement(By.xpath("//*[@id=\"search_input\"]"));
  171.         search.click();
  172.         search.sendKeys("B45593");
  173.         int displayedCounter = 0;
  174.         int expectedCounter = 0;
  175.         List<WebElement> entries = driver.findElements(By.className("entry"));
  176.         for (WebElement elem : entries) {
  177.             if(elem.isDisplayed()){
  178.                 displayedCounter++;
  179.             }
  180.             if (elem.getAttribute("id").contains("B45593")) {
  181.                 expectedCounter++;
  182.             }
  183.         }
  184.         assertEquals(expectedCounter, displayedCounter);
  185.  
  186.  
  187.     }
  188.  
  189.     @Test
  190.     public void searchNonExistingProductsByName() {
  191.         WebElement search = driver.findElement(By.xpath("//*[@id=\"search_input\"]"));
  192.         search.click();
  193.         search.sendKeys("Sandwich");
  194.         int counter = 0;
  195.         int expectedCounter = 0;
  196.         List<WebElement> entries = driver.findElements(By.className("entry"));
  197.         for (WebElement elem : entries) {
  198.             if(!elem.isDisplayed()){
  199.                 counter++;
  200.             }
  201.             if (!elem.getAttribute("id").contains("Sandwich")) {
  202.                 expectedCounter++;
  203.             }
  204.         }
  205.         assertEquals(expectedCounter, entries.size());
  206.     }
  207.  
  208.     @Test
  209.     public void filterSunglassesResults() {
  210.         String category = "Sunglasses";
  211.         Integer expectedCounter = 0;
  212.         List<WebElement> entries = driver.findElements(By.className("entry"));
  213.         for (WebElement ele : entries) {
  214.             if (ele.findElement(By.id("category")).getText().equals("Category: " + category)) {
  215.                 expectedCounter++;
  216.             }
  217.         }
  218.         assertEquals(expectedCounter, (Integer)filter(category).size());
  219.     }
  220.  
  221.     @Test
  222.     public void filterOtherResults() {
  223.         String category = "Other";
  224.         Integer expectedCounter = 0;
  225.         List<WebElement> entries = driver.findElements(By.className("entry"));
  226.         for (WebElement ele : entries) {
  227.             if (ele.findElement(By.id("category")).getText().equals("Category: " + category)) {
  228.                 expectedCounter++;
  229.             }
  230.         }
  231.         assertEquals(expectedCounter, (Integer)filter(category).size());
  232.     }
  233.  
  234.     @Test
  235.     public void purchaseItems() {
  236.        addItem("Sunglasses 2AR_entry");
  237.        driver.findElement(By.xpath("//*[@id=\"checkout_button\"]/input")).click();
  238.        WebElement name = driver.findElement(By.xpath("//*[@id=\"order_name\"]"));
  239.        WebElement address = driver.findElement(By.xpath("//*[@id=\"order_address\"]"));
  240.        WebElement email = driver.findElement(By.xpath("//*[@id=\"order_email\"]"));
  241.        WebElement payType = driver.findElement(By.xpath("//*[@id=\"order_pay_type\"]"));
  242.        name.click();
  243.        name.sendKeys("Allan Rist");
  244.        address.click();
  245.        address.sendKeys("Liivi 2");
  246.        email.click();
  247.        email.sendKeys("test@gmail.com");
  248.        payType.click();
  249.        Select dropdownSelect = new Select(payType);
  250.        dropdownSelect.selectByValue("Purchase order");
  251.  
  252.        driver.findElement(By.xpath("//*[@id=\"place_order\"]/input")).click();
  253.  
  254.        String confirmation = driver.findElement(By.xpath("//*[@id=\"order_receipt\"]/h3")).getText().trim();
  255.        assertEquals("Thank you for your order",confirmation);
  256.     }
  257.  
  258.  
  259.  
  260. }
  261. //*[@id="category"]/text()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top