Here is a script which loads jQuery through WebDriver. I've been using it successfully for quite some time.
Features:
-- will not overwrite any existing jQuery instance.
-- will not alter the DOM under test or pollute the global namespace
-- cleans up after itself to avoid memory leaks
-- waits for jQuery to be fully loaded before returning.
-- takes an optional parameter specifying a jQuery source (defaults to version 1.7.2 from Google's CDN).
-- cross-browser compatible
The Script:
You must pass this as a string to IJavaScriptExecutor.ExecuteAsyncScript
:
/** dynamically load jQuery */
(function(jqueryUrl, callback) {
if (typeof jqueryUrl != 'string') {
jqueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
}
if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
var head = document.getElementsByTagName('head')[0];
var done = false;
script.onload = script.onreadystatechange = (function() {
if (!done && (!this.readyState || this.readyState == 'loaded'
|| this.readyState == 'complete')) {
done = true;
script.onload = script.onreadystatechange = null;
head.removeChild(script);
callback();
}
});
script.src = jqueryUrl;
head.appendChild(script);
}
else {
callback();
}
})(arguments[0], arguments[arguments.length - 1]);
Example Usage (Java):
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* An example of loading jQuery dynamically using WebDriver.
*/
public class Test {
// its nice to keep JavaScript snippets in separate files.
private static final String JQUERY_LOAD_SCRIPT = "resources\\jQuerify.js";
// driver
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
String jQueryLoader = readFile(JQUERY_LOAD_SCRIPT);
// give jQuery time to load asynchronously
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeAsyncScript(jQueryLoader /*, http://localhost:8080/jquery-1.7.2.js */);
// ready to rock
js.executeScript(
"jQuery(function($) { " +
" $('input[name=\"q\"]').val('bada-bing').closest('form').submit(); " +
" }); "
);
}
// helper method
private static String readFile(String file) throws IOException {
Charset cs = Charset.forName("UTF-8");
FileInputStream stream = new FileInputStream(file);
try {
Reader reader = new BufferedReader(new InputStreamReader(stream, cs));
StringBuilder builder = new StringBuilder();
char[] buffer = new char[8192];
int read;
while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
builder.append(buffer, 0, read);
}
return builder.toString();
}
finally {
stream.close();
}
}
}
Credits:
I adapted this script from the excellent jQuerify bookmarklet by Karl Swedberg.