-
-
Save libern/a5ea055fd72b8b8bb68278551f1a4523 to your computer and use it in GitHub Desktop.
simulate user clicks on the content of webview
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
int X_MAX = 412; | |
int Y_MAX = 50; | |
int x, y; | |
x = random.nextInt(X_MAX); | |
y = random.nextInt(Y_MAX); | |
// String htmlEventJs = "var ev = document.createEvent(\"HTMLEvents\"); " + | |
// "var el = document.elementFromPoint(%d,%d); " + | |
// "ev.initEvent('click',true,true);" + | |
// " el.dispatchEvent(ev);"; | |
// htmlEventJs = String.format(htmlEventJs, x, y); | |
// webView.loadUrl("javascript:" + htmlEventJs); | |
long downTime = SystemClock.uptimeMillis(); | |
long eventTime = SystemClock.uptimeMillis() + random.nextInt(100); | |
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() | |
int metaState = 0; | |
MotionEvent me = MotionEvent.obtain( | |
downTime, | |
eventTime, | |
MotionEvent.ACTION_DOWN, | |
x, | |
y, | |
metaState | |
); | |
webView.dispatchTouchEvent(me); | |
me = MotionEvent.obtain( | |
downTime, | |
eventTime, | |
MotionEvent.ACTION_UP, | |
x, | |
y, | |
metaState | |
); | |
webView.dispatchTouchEvent(me); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment