Javascript expanding a RadMenu Webelement Selenium Webdriver C#

139 Views Asked by At

I am using Selenium WebDriver in C#. I have a RadMenu in which I want to hover over, once I do it; it should expand a sub menu that has a particular webelement i want to click. I am having to use JavaScript to click the element but that doesn't seem to expand the menu, are there any java script commands I can use to do this. For example:

                IJavaScriptExecutor js = ts.getDriver() as IJavaScriptExecutor;
                js.ExecuteScript("arguments[0].style.display='block'",leftPane_Customer);
                js.ExecuteScript("arguments[0].click()", leftPane_Customer);
                js.ExecuteScript("arguments[0].scrollIntoView(true);",leftPane_Customer);

the .click() seems to highlight the first menu but that is as far as I can get. Can anyone offer a solution (javascript syntax included) to expand the submenu?

Thanks

1

There are 1 best solutions below

0
On

You can simulate hover event using method as follows

public static void HoverOn(this RemoteWebDriver driver, IWebElement elementToHover)
{
    var action  = new Actions(driver);
    action.MoveToElement(elementToHover).Perform();
}

However click event on dynamically toggled element could cause a lot of troubles. To get very stable simulation of click event I use the following code

public static void ClickOn(this RemoteWebDriver driver, IWebElement expectedElement)
{
    try
    {
        expectedElement.Click();
    }
    catch (InvalidOperationException)
    {
        if (expectedElement.Location.Y > driver.GetWindowHeight())
        {
            driver.ScrollTo(expectedElement.Location.Y + expectedElement.Size.Height);
            Thread.Sleep(500);
        }
        driver.WaitUntil(SearchElementDefaultTimeout, (d) => driver.IsElementClickable(expectedElement));
        expectedElement.Click();
    }
}
private static bool IsElementClickable(this RemoteWebDriver driver, IWebElement element)
{
    return (bool)driver.ExecuteScript(@"
            window.__selenium__isElementClickable = window.__selenium__isElementClickable || function(element)
            {
                var rec = element.getBoundingClientRect();
                var elementAtPosition = document.elementFromPoint(rec.left, rec.top);
                return element == elementAtPosition;
            };
            return window.__selenium__isElementClickable(arguments[0]);
    ", element);
}

This code is a part of Maintainable Selenium project. You can review project site to get more info about creating maintainable UI tests with Selenium https://github.com/cezarypiatek/MaintainableSelenium/