Last active
May 19, 2016 08:35
My ideas for a method to inject jQuery.js into a page for Selenium testing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample 1 | |
$.jGrowl("Hello world!"); | |
// Sample 2 | |
$.jGrowl("Stick this!", { sticky: true }); | |
// Sample 3 | |
$.jGrowl("A message with a header", { header: 'Important' }); | |
// Sample 4 | |
$.jGrowl("A message that will live a little longer.", { life: 10000 }); | |
// Sample 5 | |
$.jGrowl("A message with a beforeOpen callback and a different opening animation.", { | |
beforeClose: function(e,m) { | |
alert('About to close this notification!'); | |
}, | |
animateOpen: { | |
height: 'show' | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function(url) { | |
var s = document.createElement('script'); | |
s.src = url; | |
(document.getElementsByTagName('head')[0] || | |
document.getElementsByTagName('body')[0]).appendChild(s); | |
})('http://code.jquery.com/jquery-2.0.3.js') | |
javascript:(function(url) { | |
var s = document.createElement('script'); | |
s.src = url; | |
(document.getElementsByTagName('head')[0] || | |
document.getElementsByTagName('body')[0]).appendChild(s); | |
})('https://raw.github.com/stanlemon/jGrowl/master/jquery.jgrowl.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment