Below Scenario is to Validate the Search functionality of an E Commerce Web Site:
URL: https://ca.ingrammicro.com/
Below Code demonstrate three important aspects of a test script: Test Steps, Checkpoints and Run Time Error Handling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
@Test public void ValidateSearch() { try { String PRODUCT = "HP"; //Browser Set Up and navigate System.setProperty("webdriver.chrome.driver", "E:\\_AkashStuff\\Automation\\dependencies\\chromedriver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("https://ca.ingrammicro.com/");//difference btw and naviogate/get //Checkpoint String expected_title = "Computer and Technology Products - Services for Business to Business Needs - Ingram Micro"; String actual_title = driver.getTitle(); Assert.assertEquals(actual_title, expected_title, "Title is correct"); //Search for item WebElement txtbx_search = driver.findElement(By.id("searchBox_Global")); txtbx_search.sendKeys(PRODUCT); driver.findElement(By.id("search-submit-anchor")).click(); //Check point expected_title = "Product Search"; actual_title = driver.getTitle(); Assert.assertEquals(actual_title, expected_title, "Product search Title is correct"); //a[contains(@rel,'#product-title-' )] //Fetch all the links Title List<WebElement> collection_product_links = driver.findElements(By.xpath("//a[contains(@rel,'#product-title-')]")); //Validate if Search result is displayed corresponding //to the string which was searched for(int i = 0; i<collection_product_links.size();i++) { String temp = collection_product_links.get(i).getText(); if ((temp.toLowerCase().contains(PRODUCT.toLowerCase()))){ Assert.assertTrue(true, PRODUCT +" is displayed on product title Product Title: " + temp); }else { Assert.assertTrue(false, PRODUCT + " is not displayed on product title Product Title: " + temp); } } ////div[@class = 'prod-number-container vpn_breakword']/span[2]/ }catch(Exception e) { Assert.assertFalse(false, "Exception thrown. Exception: " + e.toString());; } |