Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simulate user clicks on the content of webview
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);
@jiangecho
Copy link
Author

jiangecho commented Apr 29, 2016

the commented lines can work also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment