Some times we encounter to test different types of Web Sites which have some dynamic features and automating such features are little bit difficult. One of such example is CSS Drop Down Menus & Sub-menus.
Following code will help to handle CSS Drop Down Menus & Sub-menus:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DropDownCSSMenu {
public String baseURL = "http://demo.lateralcode.com/css-drop-down-menus/";
public WebDriver driver = new FirefoxDriver();
public Actions action = new Actions(driver);
@BeforeTest
public void launchBrowser()
{
driver.get(baseURL);
}
@AfterTest
public void closeBrowser()
{
driver.quit();
}
@Test
public void test() throws InterruptedException
{
WebElement home = driver.findElement(By.linkText("Home"));
WebElement about = driver.findElement(By.linkText("About"));
WebElement contact = driver.findElement(By.linkText("Contact"));
action.moveToElement(home).build().perform();//MouseOver of Menu
driver.findElement(By.xpath("//*[@id='menu']/ul/li[1]/ul/li[1]/a")).click(); //Clicking Sub Menu
Thread.sleep(3000);
action.moveToElement(about).build().perform();//MouseOver of Menu
driver.findElement(By.xpath("//*[@id='menu']/ul/li[2]/ul/li[1]/a")).click(); //Clicking Sub Menu
Thread.sleep(3000);
action.moveToElement(contact).build().perform();//MouseOver of Menu
driver.findElement(By.xpath("//*[@id='menu']/ul/li[3]/ul/li[1]/a")).click(); //Clicking Sub Menu
Thread.sleep(3000);
}
}
Following code will help to handle CSS Drop Down Menus & Sub-menus:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DropDownCSSMenu {
public String baseURL = "http://demo.lateralcode.com/css-drop-down-menus/";
public WebDriver driver = new FirefoxDriver();
public Actions action = new Actions(driver);
@BeforeTest
public void launchBrowser()
{
driver.get(baseURL);
}
@AfterTest
public void closeBrowser()
{
driver.quit();
}
@Test
public void test() throws InterruptedException
{
WebElement home = driver.findElement(By.linkText("Home"));
WebElement about = driver.findElement(By.linkText("About"));
WebElement contact = driver.findElement(By.linkText("Contact"));
action.moveToElement(home).build().perform();//MouseOver of Menu
driver.findElement(By.xpath("//*[@id='menu']/ul/li[1]/ul/li[1]/a")).click(); //Clicking Sub Menu
Thread.sleep(3000);
action.moveToElement(about).build().perform();//MouseOver of Menu
driver.findElement(By.xpath("//*[@id='menu']/ul/li[2]/ul/li[1]/a")).click(); //Clicking Sub Menu
Thread.sleep(3000);
action.moveToElement(contact).build().perform();//MouseOver of Menu
driver.findElement(By.xpath("//*[@id='menu']/ul/li[3]/ul/li[1]/a")).click(); //Clicking Sub Menu
Thread.sleep(3000);
}
}