Share
Go down
rodale_janet
Active particpant
Active particpant
Posts : 10
Join date : 2014-03-07

Using Selenium Webdriver to test drop down menu (menu within menu)

on Tue Apr 29, 2014 10:21 pm
I am having a problem getting my test for a drop down menu to work without my mouse being on the page as the test runs.   Can some one guide me in the right direction??   I have doe a lot of research on the internet and have tried several things but nothing seems to work.   I am testing on a Windows 7 PC.

I have attached the code to this topic.
Code:
package Zeus_Bicycling_Main_Header;

/*  Test Name:        Zeus Bicycling Main Header
 *  Author  :        Janet Frank
 *  Date    :        April 22, 2014
 *  Description:
 *     This program will execute all of the links in the main header.
 *      The urls are checked to be sure they go to a valid web page.
 *      If any errors occur an email is sent to the developer and QA.  If the
 *      test is successful an email is sent to the QA team to let them know that
 *      the test completed.
 * 
 *  Methods used:
 *      checkUrl        : checks to make sure that the link goes to a valid web page
 *      executeXpath    : executes the link named in the passed parameter urlName
 *      wait_fiveSeconds: will have the program wait five seconds before executing next command
 *     
 *  External classes used:
 *     SendMail:    sends an email to those specified in the parameter to_email
 *
 */

import java.io.File;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;
import org.junit.runner.JUnitCore;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.interactions.*;

public class ZeusBicyclingMainHeader {
   
     private WebDriver driver;
     private String baseUrl;
     private boolean acceptNextAlert = true;
     private StringBuffer verificationErrors = new StringBuffer();
     private boolean errorsFound = false;
     private String[] to_email = {"janet.frank@rodale.com", "jfayefrank@gmail.com"};
     private FirefoxProfile profile = new FirefoxProfile();
    
   public static void main(String[] args) {
         JUnitCore jCore = new JUnitCore();
          jCore.run(ZeusBicyclingMainHeader.class);
    }

   @Before
   public void setUp() throws Exception {
          baseUrl = "http://stage7.bicycling.com/";
          profile.setEnableNativeEvents(true);
     /*     String Xport = System.getProperty("lmportal.xvfb.id",":1");
          final File firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "/usr/bin/firefox"));
          FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
          firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);
          driver = new FirefoxDriver(firefoxBinary, null); */
          driver = new FirefoxDriver(profile);
          driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
   }

   @After
   public void tearDown() {
      if (errorsFound == false) {
         new SendMail("janet.frank@rodale.com",to_email,"Main Header","Main Header test completed successfully");
      }
       driver.quit();
   }

   @Test
   public void test() {
       driver.get(baseUrl);
      
/*      driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
       driver.get("http://www.jetairways.com/EN/IN/Home.aspx");
       Actions actObj = new Actions(driver);
       actObj.moveToElement(driver.findElement(By.xpath("//*[@id='PlanYourTravel']/span/b"))).perform();
       wait_fifteenSeconds();
       actObj.moveToElement(driver.findElement(By.xpath("//li[@title='Flights']/a"))).perform();
       wait_fifteenSeconds();
       driver.findElement(By.xpath("//a[@title='Book Online']")).click(); */
       
// ***  Execute News and Racing Links ***
           executeCSSDropDown();
          executeXpath("News & Racing","2","News & Racing","2");
          executeXpath("Pro Cycling","2","News & Racing","2");
          executeXpath("Tour de France","5","News & Racing","2");
          validateUrl("Tour de France");
         driver.navigate().back();
         wait_fiveSeconds();
          executeXpath("Tour de France archives","2","News & Racing","2");
          validateUrl("Tour de France Archives");
          driver.navigate().back();
         wait_fiveSeconds();
          executeXpath("AToC","2","News & Racing","2");
          executeXpath("USPCC","2","News & Racing","2");
          executeXpath("Cyclocross","2","News & Racing","2");
            
// *** Execute Bikes and Gear Links ***
   /*       executeCSSDropDown();
          executeXpath("Bikes & Gear","2","Bikes & Gear","2");
          executeXpath("Bike and Gear Features","2","Bikes & Gear","2");
          executeXpath("Buyer's Guide","2","Bikes & Gear","2");
          executeXpath("Bike Reviews","2","Bikes & Gear","2");
          executeXpath("Apparel Reviews","2","Bikes & Gear","2");
          executeXpath("Component Reviews","2","Bikes & Gear","2");
          executeXpath("Review Finder","2","Bikes & Gear","2");
          executeXpath("Store Finder","2","Bikes & Gear","2"); */
         
 // *** Execute Training and Nutrition Links ***
   /*       executeCSSDropDown();
          executeXpath("Training & Nutrition","2");
          wait_fiveSeconds();
          executeXpath("Training and Fitness","3");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Training & Nutrition","2");
          executeXpath("Training Plans","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Training & Nutrition","2");
          executeXpath("Nutrition","4");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Training & Nutrition","2");
          executeXpath("Weight Loss","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Training & Nutrition","2");
          executeXpath("Injury Prevention","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Training & Nutrition","2");
          executeXpath("Calorie Counter","2");

// *** Execute Repair Links ***
          executeCSSDropDown();
          executeXpath("Repair","5");
          wait_fiveSeconds();
          executeXpath("Maintenance and Repair","3");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Repair","5");
          executeXpath("Repair Videos","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Repair","5");
          executeXpath("Tool Reviews","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Repair","5");
          executeXpath("Shop Finder","2");
         
// *** Execute Ride Links ***
          executeCSSDropDown();
          executeXpath("Ride","3");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Featured Rides","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Ride","3");
          executeXpath("City Guides","2");
         
// *** Execute Video Links ***
          executeCSSDropDown();
          executeXpath("Video","4");
          wait_fiveSeconds();
          executeXpath("Racing","4");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Tour Talk","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Tour de France","7");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Bikes and Gear","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Training and Fitness","4");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Maintenance and Repair","4");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Mountainbike","2");
          wait_fiveSeconds();
          executeCSSDropDown();
          executeXpath("Video","4");
          executeXpath("Bike Skill","2");
      
// *** Execute Subscribe Link **
          executeXpath("Subscribe","3");
         
// *** Execute Blogs Link
          executeXpath("Blogs","2"); */
   
   }

   private void checkUrl (String urlName)
   {
        String pageSource = driver.getPageSource();
        String msg = urlName + " link appears to be broken";
       if ( pageSource.contains("Server not found") )  {
          new SendMail("janet.frank@rodale.com",to_email,"Broken Link",msg);
          errorsFound = true;
          
       }
   }
   
   private void validateUrl (String urlName)
   {
        String pageTitle = driver.getTitle();
        String msg = urlName + " link appears to be broken";
        System.out.println(pageTitle);
       if ( !pageTitle.contains(urlName) )  {
          new SendMail("janet.frank@rodale.com",to_email,"Bad Link",msg);
          errorsFound = true;
          
       }
   }
   private void wait_fiveSeconds ()
   {
       try
       {
          Thread.sleep(5000);
       }
       catch (Exception e)
       {
          System.out.println("Could not execute sleep command");
       }
   }
   
   private void wait_fifteenSeconds()
   {
       try
       {
          Thread.sleep(15000);
       }
       catch (Exception e)
       {
          System.out.println("Could not execute sleep command");
       }
   }
   
   private void executeLink (String urlName)
   {
      String msg = "";
      try
       {
          driver.findElement(By.id(urlName)).click();
            this.checkUrl(urlName);
       }
       catch (Exception e)
       {
          msg = urlName + " test was not successful";
          new SendMail("janet.frank@rodale.com",to_email,urlName,msg);
          errorsFound = true;
       }
       wait_fifteenSeconds();
       driver.navigate().back();
        }


   private void executeXpath (String urlName, String value, String mainMenu, String mainMenuValue)
   {
      String msg = "";
      String xPath = "(//a[contains(text(),'" + mainMenu + "')])[" + mainMenuValue + "]";
      String subXpath = "(//a[contains(text(),'" + urlName + "')])[" + value + "]";
       Actions action = new Actions(driver);
       Action hoverClick;
        System.out.println(xPath);
        System.out.println(subXpath);
        WebDriverWait wait = new WebDriverWait(driver, 15);
      try
       {
          WebElement toElement = driver.findElement(By.xpath(xPath));
         WebElement subElement = driver.findElement(By.xpath(subXpath));
         if (mainMenu != urlName)
            {
                hoverClick = action.moveToElement(toElement).build();
               hoverClick.perform();
               wait_fifteenSeconds();
               hoverClick = action.moveToElement(subElement).click().build();
            }
         else         
         {
                hoverClick = action.moveToElement(toElement).click().build();
         }
                 hoverClick.perform();
               checkUrl(urlName);
       }
      
       catch (Exception e)
       {
          System.out.println(e);
          msg = urlName + " test was not successful";
          new SendMail("janet.frank@rodale.com",to_email,urlName,msg);
          errorsFound = true;
       }
   //    driver.navigate().back();
       wait_fiveSeconds();
   }
   
   private void executeCSSDropDown ()
   {
       try {
             driver.findElement(By.cssSelector("a.dropdown-toggle"));
          }
          catch (Exception e)  {
             new SendMail("janet.frank@rodale.com",to_email,"CSS Selector","Error in cssSelector execution");
             errorsFound = true;
          }
       wait_fiveSeconds();
   }
}
Back to top
Permissions in this forum:
You cannot reply to topics in this forum