Created
April 29, 2016 03:42
-
-
Save jiangecho/d01504c9e2c623ed3ec8f9cec1cfb511 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
the commented lines can work also.