- Navigate to URL: https://ca.ingrammicro.com
- Search for product: Apple
- Assert that search result is displayed.
- Create a method which takes arrays of SKU’s
- This method should find all the number of SKU’s in the list.
- If any SKU is not found than print error message and come out.
- if all the sku is found than check corresponding check box.
- Click compare
- validate Compare screen is displayed.
- Assert that correct products are compared by validating the product titles.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
package product.ecommerce.ingrammicro.tc; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.Reporter; public class CompareTwoProducts { public static void main(String[] args) { ArrayList<String> listofSKU= new ArrayList<String>(); listofSKU.add("a"); listofSKU.add("b"); listofSKU.add("c"); } //Approach is to first get all the Product SKUs Text in to a Collections List //Find all the Input Checkbox in to a List Collection //Search the SKU in the First Collection, find the index and get the checkbox from //the 2nd collections based on the index of the searched SKU public void CompareTwoProductsUsingForLoopCollections(String searchString,String sku1, String sku2) { try { String PRODUCT = searchString; //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"); //Find list of all the Checkbox List<WebElement> col_of_chkbx = driver.findElements(By.xpath("//div[@class='search-results']//input[@type='checkbox']")); List<WebElement> col_of_skus = driver.findElements(By.xpath("//div[@class='prod-number-container vpn_breakword']//span[2]")); for(int i=0;i<=col_of_skus.size();i++) { if (col_of_skus.get(i).getText().trim().equalsIgnoreCase(sku1)) { col_of_chkbx.get(i).click(); } if (col_of_skus.get(i).getText().trim().equalsIgnoreCase(sku2)) { col_of_chkbx.get(i).click(); } } }catch(Exception e) { Assert.assertFalse(false, "Exception thrown. Exception: " + e.toString());; } } //Using XPATH public void CompareTwoProductsUsingXPATH(String searchString,String sku1, String sku2) { try { String PRODUCT = searchString; //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"); //SKU 1 Check box WebElement sku1_checkbx = driver.findElement(By.xpath("//input[@type = 'checkbox' and @value = '"+sku1+"']")); //sku2 WebElement sku2_checkbx = driver.findElement(By.xpath("//input[@type = 'checkbox' and @value = '"+sku2+"']")); sku1_checkbx.click(); sku2_checkbx.click(); }catch(Exception e) { Assert.assertFalse(false, "Exception thrown. Exception: " + e.toString());; } } //Using XPATH Axes public boolean CompareTwoProductsUsingXPATHAxes(String searchString,ArrayList<String> skuList) { try { String PRODUCT = searchString; //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"); //span[contains(text(),'387DDH')]/ancestor::div[@class='prod-details'] //sku2 for (int i=0;i<skuList.size();i++) { //Error Validation if any Product is Not Found try { WebElement sku_cckbx = driver.findElement(By.xpath("//span[contains(text(),'"+skuList.get(i)+"')]/parent::div/parent::div/parent::div/parent::div/preceding-sibling::div[@class='prod-compare-checkbox']/input")); //or driver.findElement(By.xpath("//span[contains(text(),'"+skuList.get(i)+"')]/ancestor::div[@class='prod-details']")); sku_cckbx.click(); Reporter.log("SKU selected: " + skuList.get(i)); }catch(Exception e) {// NoSuchElement Exception here e.printStackTrace(); return false; } } //Code to Click on Compare and Compare the values of the Products }catch(Exception e) { Assert.assertFalse(false, "Exception thrown. Exception: " + e.toString());; } } } |