Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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);
};
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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’;”);
    }
}
share|improve this answer
    
Is it possible to write full fledged JS functions instead of just returning values? –  MacGyver Jan 20 '12 at 23:48
    
Query from @rainangel619 "Which package includes the class named IWebDriverExtensions,i can't find it?" –  testerab May 2 '12 at 13:32

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'");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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