10

I'm trying to get element with jquery and Selenium IDe 1.0.8.

<td>storeValue</td>
<td>$('#result').find('img').filter('[alt=&quot;NameOfPhoto&quot;]').eq(0)</td>
<td></td>

And in log I get

[error] Element $('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0) not found 

When I put this command in firebug I get this element :/

Why it doesn't work ?

EDIT: Alternatively for example you can give me code how to get id of first object whith JAVA tag at main page of stackoverflow.

TAG:

<a rel="tag" title="show questions tagged 'java'" class="post-tag" href="/questions/tagged/java">java</a>

and the example result from :

<div id="question-summary-4303985" class="question-summary narrow">

is:

question-summary-4303985
user278618
  • 19,306
  • 42
  • 126
  • 196

6 Answers6

9

Based on the other posts I tried the following and it worked.

Add the code below to user-extensions.js:

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

You can then access any jQuery function by using the keyword jQuery instead of the $. Just make sure that the page you are testing is referencing the jQuery js file.

Johann Strydom
  • 1,482
  • 14
  • 18
  • Great! This worked for me! i was trying what was suggested in this answer http://stackoverflow.com/questions/2814007/how-do-i-add-a-jquery-locators-to-selenium-remote-control/2819798#2819798 got an error that jQuery is not a function, then added the above script like you said. Worked! Thanks! – Senthil Kumar Jul 28 '11 at 14:07
8

To use jQuery with Selenium IDE, it's location is below. (Be sure to have loaded jQuery in your page)

this.page().getCurrentWindow().wrappedJSObject.jQuery()

You can store the function location in a Selenium variable.

<tr>
    <td>store</td>
    <td>this.page().getCurrentWindow().wrappedJSObject.jQuery</td>
    <td>jq</td>
</tr>

Then you can use them in your Tests like:

<tr>
    <td>assertEval</td>
    <td>${jq}('div').attr('foo')</td>
    <td>bar</td>
</tr>

Above matches <div foo='bar' /> using jQuery.


Edit: Alternatively you could access it by:

this.browserbot.getUserWindow().jQuery
selenium.browserbot.getUserWindow().jQuery

source of alternate: http://cssgreut.wordpress.com/2010/12/20/run-selenium-ide-tests-with-jquery-selectors/

Quang Van
  • 11,475
  • 12
  • 57
  • 61
0

try using jQuery instead of $(). The dollar sign has a different meaning in selenium.

EDIT

As far as I can tell you can't use jQuery to select elements. Google searches turned up nothing. Just use Xpath.

shoebox639
  • 2,312
  • 15
  • 14
0

Example of storing the current date with JavaScript and echo it into the Selenium log console by using ${}:

<tr>
    <td>store</td>
    <td>javascript{new Date}</td>
    <td>foo</td>
</tr>
<tr>
    <td>echo</td>
    <td>${foo}</td>
    <td></td>
</tr>

As you can see to use store with an result of an JavaScript expression add javascript{} arround the expression.

Your example using javascript{storedVars['bar']} to output the stored variable.

<tr>
    <td>store</td>
    <td>javascrip{jQuery('#result').find('img').filter('[alt="NameOfPhoto"]').eq(0)}</td>
    <td>bar</td>
</tr>
<tr>
    <td>open</td>
    <td>javascript{storedVars['bar']}</td>
    <td></td>
</tr>

JavaScript in Selenium:

With javascript{alert('hello')} you can run JavaScript/jQuery in the value fields.

There is also an extension for Selenium to show which vars are stored: http://reallysimplethings.wordpress.com/2010/09/28/the-stored-variables-viewer-plugin-for-selenium-ide-v1-3-released/

powtac
  • 40,542
  • 28
  • 115
  • 170
  • It doesn'w work :/ I get: error [error] Unexpected Exception: message -> jQuery is not a function, fileName -> chrome://selenium-ide/content/selenium/scripts/selenium-api.js, lineNumber -> 2490, stack -> ("javascript{jQuery('#result').find('img').filter('[alt=\"NameOfPhoto\"]').eq(0)}") – user278618 Nov 29 '10 at 12:03
  • Check the official documentation about how to store vars. – powtac Dec 02 '10 at 11:14
  • i think user278618 is right, I couldn't access jQuery that way either, and I'm not sure if it has anything to do with stored variables, correct me if i'm wrong. – Quang Van Mar 21 '11 at 06:09
  • So don't use jQuery, you can choose any other normal selector to put a value into "bar" – powtac Mar 21 '11 at 08:56
  • good tip on the Selenium "Stored Variables" plugin... didn't realize there were a bunch of other useful addons to Selenium. Thanks. :) – Quang Van Mar 26 '11 at 00:13
0

Have you bundled jQuery in Selenium jar? If not, you can't use that syntax.

Rajasankar
  • 928
  • 1
  • 19
  • 41
  • If you're using Selenium RC, check Jian blog post http://johnjianfang.blogspot.com/2009/04/how-to-use-jquery-to-create-custom.html. For IDE you can add jquery as a user extension. Refer the selenium docs. – Rajasankar Dec 01 '10 at 01:16
0

Try this instructions from German Rumm's blog

First, download Sizzle, which is a selector engine for jQuery and unpack sizzle.js to a convenient location.
Second, create empty user-extensions.js file. Name can be whatever you want by the way.
Add this to user-extensions.js

PageBot.prototype.locateElementBySizzle = function(locator, inDocument) {
  var results = [];
  window.Sizzle(locator, inDocument, results);
  return results.length > 0 ? results[0] : null;
}

Third, go to Selenium IDE, Options -> Options… and add sizzle.js and user-extensions.js to “Selenium Core extensions”.
Restart Selenium IDE (just close all instances of it and open it again), and now you can use sizzle=(locator) everywhere, where “locator” is needed

Your selector will looklike:

#result img[alt="NameOfPhoto"]:eq(0)

JQuery selectors mostly comes from CSS selectors, so you can use them also in selenium

css=cssSelector

Community
  • 1
  • 1
cetnar
  • 9,357
  • 2
  • 38
  • 45