Skip to content

Instantly share code, notes, and snippets.

@jiangecho
Created April 29, 2016 03:42
Show Gist options
  • Save jiangecho/d01504c9e2c623ed3ec8f9cec1cfb511 to your computer and use it in GitHub Desktop.
Save jiangecho/d01504c9e2c623ed3ec8f9cec1cfb511 to your computer and use it in GitHub Desktop.
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

the commented lines can work also.

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