Created
August 16, 2012 12:26
-
-
Save nicegraham/3369800 to your computer and use it in GitHub Desktop.
Dynamically load jquery for TouchActions
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 ***/ | |
var JQUERY_URL = 'http://code.jquery.com/jquery-latest.min.js'; | |
if (typeof jQuery == 'undefined') { | |
var script=document.createElement('script'); | |
script.src = JQUERY_URL; | |
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); | |
} | |
}); | |
head.appendChild(script); | |
} |
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
package com.paddypower.selenium.framework; | |
import com.paddypower.reporter.ReporterFactory; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.WebDriver; | |
import java.io.IOException; | |
import java.io.InputStream; | |
public class TouchAction { | |
//Javascripts to trigger the touch events. This will be appended to the javascript string | |
private static final String JQUERY_LOAD_SCRIPT = "jQueryLoad.js"; | |
private static String DOUBLETAP = ".trigger('touchend').trigger('touchend');"; | |
private static String SINGLETAP = ".trigger('touchstart').trigger('touchend');"; | |
private static String SWIPERIGHT = ".trigger('swiperight');"; | |
private static String SWIPELEFT = ".trigger('swipeleft');"; | |
private WebDriver driver; | |
public TouchAction(WebDriver driver) { | |
this.driver = driver; | |
} | |
private int getElementXPosition(AutObject element) { | |
return element.getLocation().getX(); | |
} | |
public static void main(String args[]) { | |
new TouchAction(null).loadJquery(); | |
} | |
private void loadJquery() { | |
try { | |
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(JQUERY_LOAD_SCRIPT); | |
StringBuilder stringBuilder = new StringBuilder(); | |
byte[] b = new byte[4096]; | |
for (int n; (n = inputStream.read(b)) != -1; ) { | |
stringBuilder.append(new String(b, 0, n)); | |
} | |
executeScript(stringBuilder.toString()); | |
} catch (IOException e) { | |
ReporterFactory.getReporter().logException(e); | |
} | |
} | |
private boolean isJqueryAvailable() { | |
String js = "return (typeof jQuery == 'undefined');"; | |
boolean available; | |
Object response = ((JavascriptExecutor) driver).executeScript(js); | |
available = Boolean.valueOf(response.toString()); | |
return available; | |
} | |
private int getElementYPosition(AutObject element) { | |
return element.getLocation().getY(); | |
} | |
private String addCommonScript(AutObject elementToTap) { | |
return "window.jQuery(document.elementFromPoint(" | |
+ getElementXPosition(elementToTap) + "," | |
+ getElementYPosition(elementToTap) + "))"; | |
} | |
public void doubleTap(AutObject elementToTap) { | |
String javascriptToExecute = addCommonScript(elementToTap) + DOUBLETAP; | |
executeScript(javascriptToExecute); | |
} | |
public void singleTap(AutObject elementToTap) { | |
if(!isJqueryAvailable()) loadJquery(); | |
String javascriptToExecute = "return window.jQuery(//document.elementFromPoint(" | |
+ getElementXPosition(elementToTap) + "," | |
+ getElementYPosition(elementToTap) + "))" + SINGLETAP; | |
executeScript(javascriptToExecute); | |
} | |
public void swipeLeft(AutObject elementToSwipe) { | |
String javascriptToExecute = "window.jQuery(document.elementFromPoint(" | |
+ getElementXPosition(elementToSwipe) + "," | |
+ getElementYPosition(elementToSwipe) + "))" + SWIPELEFT; | |
executeScript(javascriptToExecute); | |
} | |
public void swipeRight(AutObject elementToSwipe) { | |
String javascriptToExecute = "window.jQuery(document.elementFromPoint(" | |
+ getElementXPosition(elementToSwipe) + "," | |
+ getElementYPosition(elementToSwipe) + "))" + SWIPERIGHT; | |
executeScript(javascriptToExecute); | |
} | |
private void executeScript(String javascriptToExecute) { | |
try { | |
Object obj = ((JavascriptExecutor) driver).executeScript(javascriptToExecute); | |
if (obj != null) { | |
ReporterFactory.getReporter().log(""); | |
} | |
} catch (Exception e) { | |
ReporterFactory.getReporter().logException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment