This Scenario can be mentioned as an answer to the Question : Explain a challenging scenario/task you recently faced and how did you overcome it.
I am documenting the answer below with code snippet.
Answer: I recently handled a mega-menu in my current application. The structure of the menu was complex.
Explain the scenario first:
I am currently working on E commerce application. We have Mega -Menu structure which contains list of products structured in multi layered pop ups.
After clicking on the menu, Categories list is displayed. Categories like, mobile phones, cables etc.
After hovering on any specific Category, another pop up called as sub categories is displayed. Sub categories displays list like smart phone, basic phone, with camera , no-camera etc.
This Sub category list contains list of all the vendors list like: ‘Apple’,”1+’, ‘Samsung’ ‘oppo’ etc
Explain the Challenge and solution:
All these menu pop ups are very volatile, i.e. they go away as soon as mouse pointer is moved. I used Actions to handle it. Also, each pop up has to be validated after hovering.
Other than that, categories, sub categories and vendors links had to be identified at run time by iterating the links in corresponding pop ups respectively.
Code:
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 |
@Test public void AutomateMegaMenuStucture_ForLoop() { try { String category = "Cables"; String sub_category = "Computer Cables"; String vendor = "Apple"; LOG.info("========>Execution Triggered========="); System.setProperty("webdriver.chrome.driver", "E:\\_AkashStuff\\Automation\\dependencies\\chromedriver\\chromedriver.exe"); //1. Invoke Browser WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); LOG.info("Chrome Browser Invoked"); //2. Navigate to URl driver.get("https://ca.ingrammicro.com/"); LOG.info("Navigated to URl: https://ca.ingrammicro.com/ "); //3. Click on the Product Link driver.findElement(By.id("product-activate")).click(); LOG.info("Clicked on Product link"); //4. Check if Category Block is displayed WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("products-menu-container"))); LOG.info("Clicked on Product link"); //5. Get list of all the links List<WebElement> primary_menu_links = driver.findElements(By.xpath("//ul[@id = 'primary-menu-links']/li/a")); LOG.info("Fetched all the primary links"); //6. Hover on the link provided by the user for(WebElement product_link:primary_menu_links) { if (product_link.getText().contains(category) ) { Actions action = new Actions(driver); action.moveToElement(product_link).perform(); LOG.info("Hovered on product: " + category); break; } }//end for //6. Check secondary menu is displayed WebDriverWait wait1 = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("secondary-menu"))); LOG.info("Wait for Sub category pop up complete."); //7. Get List of all the links in secondary menu List<WebElement> secondry_menu_links = driver.findElements(By.xpath("//div[@id = 'secondary-menu']//li/a")); //Hover on the link for(WebElement sub_category_link:secondry_menu_links) { if (sub_category_link.getText().contains(sub_category) ) { //product_link.click(); Actions action = new Actions(driver); action.moveToElement(sub_category_link).perform(); LOG.info("Hovered on sub category: " + sub_category); break; } }//end for //8. Check tertiary menu is displayed WebDriverWait wait2 = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("products-vendor-menu"))); LOG.info("Wait for vendor pop up complete."); //Get List of all the links in secondary menu List<WebElement> tertiary_menu_links = driver.findElements(By.xpath("//div[@id = 'products-vendor-menu']//li/a")); //Hover on the link for(WebElement vendor_link:tertiary_menu_links) { if (vendor_link.getText().contains(vendor) ) { vendor_link.click(); LOG.info("Clicked on Vendor: " + vendor); break; } }//end for LOG.info("========>Execution Completed========="); }catch(Exception e) { LOG.fatal("Exception Caught : " + e.getStackTrace()); e.printStackTrace(); } } @Test public void AutomateMegaMenuStucture_UsingXPATH() { try { String category = "Cables"; String sub_category = "Computer Cables"; String vendor = "Apple"; LOG.info("========>Execution Triggered========="); System.setProperty("webdriver.chrome.driver", "E:\\_AkashStuff\\Automation\\dependencies\\chromedriver\\chromedriver.exe"); //1. Invoke Browser WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); LOG.info("Chrome Browser Invoked"); //2. Navigate to URl driver.get("https://ca.ingrammicro.com/"); LOG.info("Navigated to URl: https://ca.ingrammicro.com/ "); //3. Click on the Product Link driver.findElement(By.id("product-activate")).click(); LOG.info("Clicked on Product link"); //4. Check if Category Block is displayed WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("products-menu-container"))); LOG.info("Clicked on Product link"); //5. Find the category element WebElement category_link = driver.findElement(By.xpath("//ul[@id='primary-menu-links']/li/a[contains(text(),'" + category + "')]")); Actions action = new Actions(driver); action.moveToElement(category_link).perform(); LOG.info("Hovered on product: " + category); //6. Check secondary menu is displayed WebDriverWait wait1 = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("secondary-menu"))); LOG.info("Wait for Sub category pop up complete."); //7. Get Sub category of all the links in secondary menu WebElement sub_category_link = driver.findElement(By.xpath("//div[@id = 'secondary-menu']//li/a[contains(text(),'" + sub_category + "')]")); Actions action1 = new Actions(driver); action1.moveToElement(sub_category_link).perform(); LOG.info("Hovered on Sub category: " + sub_category); //8. Check tertiary menu is displayed WebDriverWait wait2 = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("products-vendor-menu"))); LOG.info("Wait for vendor pop up complete."); //9. Get List of all the links in tertiary menu WebElement vendor_link = driver.findElement(By.xpath("//div[@id = 'products-vendor-menu']//li/a[contains(text(),'" + vendor + "')]")); vendor_link.click(); LOG.info("Hovered on vendor: " + vendor); LOG.info("========>Execution Completed========="); }catch(Exception e) { LOG.fatal("Exception Caught : " + e.getStackTrace()); e.printStackTrace(); } } |