Get element with jquery and selenium IDE 1.0.8

守給你的承諾、 提交于 2019-11-30 09:10:44

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.

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/

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.

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

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/

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!