views:

586

answers:

3

Hi, I've been using XPath with Selenium quite happily and even using getEval with a but of Javascript, but a colleague said wouldn't it be great to be able to use JQuery selectors in Selenium.

I've googled it, but can't find any articles that seem to work for me. Could anyone provide a comprehensive guide on how to use JQuery syntax to extract doc elements and their respective values out of selenium.

I'm using C# to write my selenium tests, so if any examples could be from a C# perspective that'd be great.

Thanks

A: 

You would need to define a new location strategy using the AddLocationStrategy method and will need to include jQuery in your user-extensions.js file.

PetersenDidIt
+4  A: 

Karl Swedberg wrote an excellent blog entry about it which can be found at http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet

We adapted this and basically in the Selenium Server jar file we modified TestRunner.html to include the jquery JavaScript:

        <script language="JavaScript" type="text/javascript" src="lib/jquery.min.js"></script>
        <script language="JavaScript" type="text/javascript">
            function openDomViewer() {
                var autFrame = document.getElementById('selenium_myiframe');
                var autFrameDocument = new SeleniumFrame(autFrame).getDocument();
                this.rootDocument = autFrameDocument;
                var domViewer = window.open(getDocumentBase(document) + 'domviewer/domviewer.html');
                return false;
            }
        </script>

Then to enable this for use in Selenium we add the location strategy:

mySelenium.addLocationStrategy("jquery",
            "var loc = locator; " +
            "var attr = null; " +
            "var isattr = false; " +
            "var inx = locator.lastIndexOf('@'); " +

            "if (inx != -1){ " +
            "   loc = locator.substring(0, inx); " +
            "   attr = locator.substring(inx + 1); " +
            "   isattr = true; " +
            "} " +

            "var found = jQuery(inDocument).find(loc); " +
            "if (found.length >= 1) { " +
            "   if (isattr) { " +
            "       return found[0].getAttribute(attr); " +
            "   } else { " +
            "       return found[0]; " +
            "   } " +
            "} else { " +
            "   return null; " +
            "}"
        );

Note the above addition of locator strategy is in Java but its just a string so should be easily replicated in C#. JQuery does make things a lot faster, especially in Internet Explorer!

Zugwalt
i found this very intersting! but the main question is: how can we use this jquery-extension, eg. type in a textarea selected by jquery?
Andreas Niedermair
@Andreas you just use the jquery= to start your locator expression and then use jquery! Example locator expression (intentionally not simplest example):"jquery=table#myParentTableID > input.input-class"
Zugwalt
thanks, that did it!
Andreas Niedermair
Hey, I battled with this for a while before realising I had to put the script tags mentioned into RemoteRunner.html rather than TestRunner.
Rodreegez
A: 

Hi,

Do we need to register mySelenium.addLocationStrategy in the selenium?

how to use "jquery=table#myParentTableID > input.input-class" in the script? I am using C# framework.

Usha
make it a new question...you can either to it via integration of the runner-script, or in your ctor of your testclass. at least you have to modify the runner.html to include the `.js`-file
Andreas Niedermair
there you go: http://dev.niedermair.name/archives/155-howto-integrate-jquery-into-selenium.html
Andreas Niedermair