Navigate, Validate Menu
Создать проект, с использованием Java и Selenium Webdriver,
содержащий регрессионный тест, который бы проверял
работоспособность главного меню сайта http://store.demoqa.com/
Тест должен содержать следующие проверки:
1. Все пункты и подпункты контекстного меню доступны
2. При выборе пунктов, открываются соответствующие им страницы
3. При открытии страниц Home и All Product соответствующие пункты меню подсвечены
Solution:
import org.skyscreamer.jsonassert.*; public class MenuTests { private static WebDriver _driver = null; private static String _expectedJsonMenu = "[{\"name\":\"Home\",\"link\":\"http://store.demoqa.com/\",\"sub-menu\":[]},{\"name\":\"Product Category\",\"link\":\"http://store.demoqa.com/products-page/product-category/\",\"sub-menu\":[{\"name\":\"Accessories\",\"link\":\"http://store.demoqa.com/products-page/product-category/accessories/\"},{\"name\":\"iMacs\",\"link\":\"http://store.demoqa.com/products-page/product-category/imacs/\"},{\"name\":\"iPads\",\"link\":\"http://store.demoqa.com/products-page/product-category/ipads/\"},{\"name\":\"iPhones\",\"link\":\"http://store.demoqa.com/products-page/product-category/iphones/\"},{\"name\":\"iPods\",\"link\":\"http://store.demoqa.com/products-page/product-category/ipods/\"},{\"name\":\"MacBooks\",\"link\":\"http://store.demoqa.com/products-page/product-category/macbooks/\"}]},{\"name\":\"#0 (no title)\",\"link\":null,\"sub-menu\":[{\"name\":\"#0 (no title)\",\"link\":null},{\"name\":\"#0 (no title)\",\"link\":null}]},{\"name\":\"All Product\",\"link\":\"http://store.demoqa.com/products-page/product-category/\",\"sub-menu\":[]}]"; private static WebElement getParentElement(WebElement element) { if (_driver instanceof JavascriptExecutor) return (WebElement)((JavascriptExecutor)_driver).executeScript( "return arguments[0].parentElement", element); throw new IllegalStateException("This driver does not support JavaScript!"); } private static String getJsonMenu() { JsonArray menu = new JsonArray(); Actions action = new Actions(_driver); List<WebElement> items = _driver.findElements( By.cssSelector("#menu-main-menu > li > a")); for (WebElement item : items) { JsonObject menu_item = new JsonObject(); if (item.isDisplayed()) { menu_item.addProperty("name", item.getText()); menu_item.addProperty("link", item.getAttribute("href")); } action.moveToElement(item).perform(); List<WebElement> subItems = getParentElement(item).findElements( By.cssSelector(".sub-menu > li > a")); JsonArray sub_menu = new JsonArray(); for (WebElement subItem : subItems) { if (subItem.isDisplayed()) { JsonObject sub_menu_item = new JsonObject(); sub_menu_item.addProperty("name", subItem.getText()); sub_menu_item.addProperty("link", subItem.getAttribute("href")); sub_menu.add(sub_menu_item); } } menu_item.add("sub-menu", sub_menu); menu.add(menu_item); } return menu.toString(); } @Test public void verifyMenuStructure() throws JSONException { String actualJsonMenu = getJsonMenu(); JSONAssert.assertEquals(_expectedJsonMenu, actualJsonMenu, JSONCompareMode.STRICT); } @Test public void verifyMenuNavigation() { String message = "\n"; Actions action = new Actions(_driver); JsonArray menu = (JsonArray) new JsonParser().parse(_expectedJsonMenu); for (JsonElement item : menu) { String itemName = getJsonValue(item, "name"); String itemLink = getJsonValue(item, "link"); WebElement menu_item = _driver.findElement(By.xpath( "//*[@id = 'menu-main-menu']/li/a[contains(text(), '" + itemName + "')]")); menu_item.click(); if (itemLink.equals("null") == false && waitUrl(itemLink) == false) { message += "Menu: " + itemName + "\n"; message += "ExpectedUrl: " + itemLink + "\n"; message += "ActualUrl: " + _driver.getCurrentUrl() + "\n\n"; } if (itemName.equals("Home") || itemName.equals("All Product")) { List<WebElement> elems = _driver.findElements(By.className("current-menu-item")); if (elems.size() == 1) { if (elems.get(0).getText().equals(itemName) == false) { message += "ExpectedSelected: " + itemName + "\n"; message += "ActualSelected: " + elems.get(0).getText() + "\n\n"; } } else { message += "ExpectedSelected: " + itemName + "\n"; message += "ActualSelected: " + elems.size() + "\n\n"; } } for (JsonElement subItem : item.getAsJsonObject().getAsJsonArray("sub-menu")) { String subItemName = getJsonValue(subItem, "name"); String subItemLink = getJsonValue(subItem, "link"); menu_item = _driver.findElement(By.xpath( "//*[@id='menu-main-menu']/li/a[(text()='" + itemName + "')]")); action.moveToElement(menu_item).perform(); menu_item.findElement(By.xpath( "//*[contains(@class,'sub-menu')]/li/a[(text()='" + subItemName + "')]")).click(); if (subItemLink.equals("null") == false && waitUrl(subItemLink) == false) { message += "Menu: " + subItemName + "\n"; message += "ExpectedUrl: " + subItemLink + "\n"; message += "ActualUrl: " + _driver.getCurrentUrl() + "\n\n"; } } } if (message.length() > 1) fail(message); } }
verifyMenuNavigation:
ExpectedSelected: All Product ActualSelected: 2
verifyMenuStructure:
[0].name Expected: House got: Home ; [1].name Expected: Product got: Product Category ; [1].sub-menu[0].link Expected: http://store.demoqa.com/products-page/product-category/things/ got: http://store.demoqa.com/products-page/product-category/accessories/
Note:
Write JSON unit tests in less code. Great for testing REST interfaces.
http://jsonassert.skyscreamer.org/