I want to select a value from the drop-down menu. My code selects only the first value from the drop-down menu, and I want you to click on all the values from the drop-down menus and check whether they are hand-loaded with the correct URL address and title tab. I do not see any mistake, and shows me that the test passed the test but did not do what I wanted. I use PageObject and PageFactory. Please help.
This is my code for the first value from the drop-down menu:
public void clickOnAccessories(){
WebElement element=driver.findElement(By.xpath("//a[text()='Product Category']"));
String mouseOver = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor)driver).executeScript(mouseOver, element);
waitForElementToBeDisplayed(driver.findElement(By.xpath("//*[text()='Accessories']")), 500);
Assert.assertTrue(driver.getCurrentUrl().equals("http://store.demoqa.com/products-page/product-category/accessories/"));
Assert.assertTrue(driver.getTitle().contains("Accessories"));
}
This is my HTML code:
<li id="menu-item-33" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-has-children menu-item-33 has_children">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/">
<span></span>
Product Category
</a>
<ul class="sub-menu" style="display: none;">
<li id="menu-item-34" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-34">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/accessories/" style="padding-left: 10px;">
<span></span>
Accessories
</a>
</li>
<li id="menu-item-35" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-35">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/imacs/" style="padding-left: 10px;">
<span></span>
iMacs
</a>
</li>
Here is the code for hovering mouse on a Menu and then click a sub-menu link-
Actions action = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
Thread.sleep(2000);
action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//a[contains(.,'iMacs')]"))).click().build().perform();
If you want to iterate all submenu of a Menu then either you have to get all menu name in string array and then pass the name in xpath
one by one
Actions action = new Actions(driver);
String[] submenus = {"Accessories", "iMacs", "iPads"};
for(int i=0;i<submenus.length;i++)
{
WebElement we = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
Thread.sleep(2000);
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]"))).click().build().perform();
Thread.sleep(3000);
}