4

I've been switching over our company's automation scripts from Selenium IDE to Selenium 2.0 WebDriver. The problem is that we have a crap load of user extension custom commands because of all the AJAX our sites do. So there are a lot of custom commands (selectors). I can't figure out how to get user extensions working without the IDE (when just using webdriver). Can this be done? If not, what is the alternative so we can still use jQuery in the new method? If you have a simple example of the alternative, even better.

Here is an example of how we used jQuery in the IDE user extensions (js file added in options in Selenium IDE). We had to make sure our site was injecting the jQuery script. In the IDE, we had a custom command named "getUlLiCount" and "getUlLiCountAndWait" (this one is created by default).

function jQuery(selector)
{
    return selenium.browserbot.getUserWindow().jQuery(selector);
}

Selenium.prototype.doGetUlLiCount = function(ulName, varStore) { 

    this.doStore(jQuery('#' + ulName + ' > li').size(),varStore);
};

2 Answers 2

2

The way I handle this is to create WebDriver extensions in an IWebDriverExtensions Class so that the JQuery or Javascript methods are always available through your driver object. The driver object is cast to IjavaScriptExecutor allowing you to execute the JavaScript or Jquery.

You can also create any other extension methods you need in he same class.

These two examples use JQuery or JavaScript to show hidden elements on the page.

//Extensions Class
public static class IWebDriverExtensions
{
    public static void ShowHiddenWithJQuery(this IWebDriver driver, string ElementLocator)
    {
    //Jquery implementation to show hidden element, where ElementLocator is passed into     
    //the method along with a locator e.g. ‘div#divname’
    ((IJavaScriptExecutor)driver).ExecuteScript(“$(‘” + ElementLocator + “‘).show();”);
    }

    {
    public static void ShowHiddenWithJavaScript(this IWebDriver driver, string     
    ElementLocator)
    //Alternative using Javascript
    ((IJavaScriptExecutor)driver).ExecuteScript(“document.getElementById(`” +           
    ElementLocator + “`).style.display = ‘block’;”);
    }
}
2
  • Is it possible to write full fledged JS functions instead of just returning values? Jan 20, 2012 at 23:48
  • Query from @rainangel619 "Which package includes the class named IWebDriverExtensions,i can't find it?"
    – testerab
    May 2, 2012 at 13:32
0

They have an interface to allow you to execute JavaScript. The contents of your JavaScript function would be passed in the ExecuteScript parameter. Here I have "return '123'" as my argument. The first line is casting our IWebDriver object to interface type "IJavaScriptExecutor".

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string UlLiCount = (string)js.ExecuteScript("return '123'");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.