Selenium + C# + hover = difficult

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidraimosson
    New Member
    • May 2012
    • 5

    Selenium + C# + hover = difficult

    Hi!

    For a couple of days I've been trying to use Selenium to automate testing of a website. This fails because of a hidden menu item that is only displayed when the parent item in the menu is hovered.

    The website I'm trying to test on is found here: http://www.sweetarabia ns.com/.

    The parent menu item is "Stallions" , which is contained in a HTML LI item. This is also the item to be hovered.

    Here is my C# code being used:
    Code:
    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.Interactions;
    using OpenQA.Selenium.Interactions.Internal;
    using OpenQA.Selenium.Support.UI;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Selenium
    {
        [TestFixture]
        public partial class Default : System.Web.UI.Page
        {
            public Int16 SweetArabians1()
            {
                Int16 result;
                IWebDriver ff_driver = new FirefoxDriver();
    
                ff_driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                ff_driver.Navigate().GoToUrl("http://www.sweetarabians.com/");
    
                var stallions_li = ff_driver.FindElement(By.ClassName("page-item-6"));
                Actions action1 = new Actions(ff_driver);
                action1.MoveToElement(stallions_li).Perform();
    
                var stallions_link = stallions_li.FindElement(By.LinkText("Stallions"));
                Actions action2 = new Actions(ff_driver);
                action2.MoveToElement(stallions_link).Perform();
    
                WebDriverWait wait = new WebDriverWait(ff_driver, TimeSpan.FromSeconds(10));
                var hero_link = wait.Until((d) => { return d.FindElement(By.LinkText("Sweet Hero")); });
    
                ff_driver.FindElement(By.LinkText("Sweet Hero")).Click();
    
                # Wait a few seconds and then do some testing of the to-be-loaded page here...
    
                if (ff_driver != null)
                {
                    ff_driver.Close();
                };
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                SweetArabians1();
            }
        }
    }
    This doesn't make the submenu appear at all and I don't understand what I'm doing wrong. Does anybody have a clue? Any help is appreciated.

    Best regards

    David
  • davidraimosson
    New Member
    • May 2012
    • 5

    #2
    I've tried to simulate a hover on the following simple HTML code as well, without success:

    Code:
    <!DOCTYPE html>
    <html><head>
    <title>Test</title>
    <style type="text/css">
    <!--
    
    #test {
    	background: #00ff00;
    	display: block;
    	height: 20px;
    	width: 200px;
    }
    
    #test:hover > div {
    	display: block;
    }
    
    #inside {
    	display: none;
    }
    
    -->
    </style>
    </head>
    <body>
    <div id="test">
    	Hover me!<br>
    	<div id="inside">
    		<a href="about:blank">Go to blank page</a>
    	</div>
    </div>
    </body>
    </html>

    Comment

    • davidraimosson
      New Member
      • May 2012
      • 5

      #3
      After some further struggling I think I know what the problem is. Namely that the FirefoxDriver uses synthetic events instead of native events. Because of that the hovering was never triggered. I got it to work with the InternetExplore rDriver which always uses native events.

      If I could find out how to use native events with the FirefoxDriver, which should be possible, then I suppose it would solve the problem as well.

      Comment

      Working...