How to press/click a button in WebView using Espresso?

4,110 views
Skip to first unread message

keta...@gmail.com

unread,
Jul 11, 2016, 3:18:08 AM7/11/16
to Android Testing Support Library

I have an android SDK and upon pressing a button opens a Webview where i can see a button whose html view is as below:

<button type="button" class="button js-analytics-submit" id="submit-try-sample" data-analytics-category="Sample" data-analytics-label="Intro">
  <svg xmlns="http://www.w3.org/2000/svg" width="58" height="58" viewBox="0 0 58 58" id="button-logo
" class="button__logo">
  <path fill="currentColor" fill-rule="evenodd" d="M37.32 21.933l2.877-2.876-.626-.625c-5.04-5.043-13.247-5.043-18.29 0-4.832 4.833-5.03 12.568-.6 17.643l-2.876 2.875.625.625c5.042 5.043 13.247 5.043 18.29 0 4.832-4.832 5.03-12.567.6-17.642zm-14.79-2.25c4.144-4.144 10.76-4.344 15.142-.6l-1.64 1.64c-3.488-2.86-8.644-2.664-11.9.593-3.256 3.256-3.454 8.412-.594 11.9l-1.608 1.608c-3.743-4.382-3.542-11 .6-15.142zm12.94 18.642c-4.143 4.144-10.76 4.344-15.142.6l1.64-1.64c3.488 2.86 8.644 2.664 11.9-.593 3.256-3.256 3.454-8.412.594-11.9l1.608-1.608c3.743 4.382 3.543 10.998-.6 15.14z"></path>
</svg>
  <span class="button__wrapper" id="button-text">
    <span class="button__text">
      Let’s do this!
    </span>
  </span>
</button>

Now i'm trying to press/click a button using the following but nothing works :(

onWebView().perform(script("return document.getElementById(\"submit-try-sample\").click()"));

onWebView().withElement(findElement(Locator.ID, "submit-try-sample")).perform(webClick());

onWebView().withElement(findElement(Locator.XPATH, "//button//span[contains(text(),'do this')]")).perform(webClick());

The above code doesn't throw any error and executes successfully but it doesn't click the button.

Would appreciate if any espresso Webview expert can help me with clicking the button please.

Thanks

Nick Korostelev

unread,
Jul 11, 2016, 2:34:22 PM7/11/16
to Android Testing Support Library, keta...@gmail.com
A bit hard to know whats going on? Can you provide a logcat or better yet a small small which we can use to reproduce the issue? 

do you have more than 1 webview in your activity? maybe you need onWebView(withId...)?

Ketan Soni

unread,
Jul 12, 2016, 12:59:39 PM7/12/16
to Luis Ramirez, nkor...@google.com, android-testing...@googlegroups.com
Hi Nick / Luis,

Thanks for replying!

I've tried following statement as well but it fails with an exception mentioned the logcat details
onWebView(withId(R.id.__sample_web_view)).forceJavascriptEnabled().withElement(findElement(Locator.ID, "submit-try-sample")).perform(webClick());
Logcat as follows:
07-11 02:05:48.867 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Forcibly enable javascript.' action on view WebView with JS enabled
07-11 02:06:30.375 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Forcibly enable javascript.' action on view with id: com.sample.android.sdk.sample:id/__sample_web_view
07-11 02:06:30.379 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.TransformingAtom@11d44f1 in window: null with element: null' action on view with id: com.sample.android.sdk.sample:id/__sample_web_view
07-11 02:06:30.400 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$1@2077304 in window: null with element: {"ELEMENT":":wdc:1468217190399"}' action on view with id: com.sample.android.sdk.sample:id/__sample_web_view
07-11 02:06:30.437 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.sampleeAtom@b4f60ed in window: null with element: null' action on view WebView with JS enabled
07-11 02:06:30.455 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.sampleeAtom@4a2ab22 in window: null with element: null' action on view WebView with JS enabled
07-11 02:06:30.460 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.TransformingAtom@7b369b3 in window: null with element: null' action on view WebView with JS enabled
07-11 02:06:30.473 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performancerming 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$1@50abb70 in window: null with element: {"ELEMENT":":wdc:1468217190400"}' action on view WebView with JS enabled
07-11 02:06:30.511 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.TransformingAtom@34e1ee9 in window: null with element: null' action on view WebView with JS enabled
07-11 02:06:30.527 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$1@6e4576e in window: null with element: {"ELEMENT":":wdc:1468217190399"}' action on view WebView with JS enabled
07-11 02:06:30.557 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.sampleeAtom@3b81e9c in window: null with element: null' action on view WebView with JS enabled
07-11 02:06:30.602 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.TransformingAtom@c768888 in window: null with element: null' action on view WebView with JS enabled
07-11 02:06:30.611 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$3@4abe646 in window: null with element: {"ELEMENT":":wdc:1468217190401"}' action on view WebView with JS enabled
07-11 02:06:30.629 5470-5470/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Propagate: java.lang.RuntimeException: java.lang.RuntimeException: Error in evaluationEvaluation: status: 12 value: {message=Element is not currently interactable and may not be manipulated} hasMessage: true message: Element is not currently interactable and may not be manipulated' action on view WebView with JS enabled
07-11 02:06:30.631 5470-5519/com.sample.android.sdk.sample I/art: Starting a blocking GC Explicit
07-11 02:06:30.631 5470-5470/com.sample.android.sdk.sample D/LifecycleMonitor: Lifecycle status change: com.sample.android.sdk.sample.SampleActivity@a586f9c in: DESTROYED
07-11 02:06:30.637 5470-5519/com.sample.android.sdk.sample I/art: Explicit concurrent mark sweep GC freed 3499(268KB) AllocSpace objects, 20(1568KB) LOS objects, 39% free, 4MB/6MB, paused 184us total 5.881ms
07-11 02:06:30.637 5470-5519/com.sample.android.sdk.sample I/art: Starting a blocking GC Explicit
07-11 02:06:30.641 5470-5519/com.sample.android.sdk.sample I/art: Explicit concurrent mark sweep GC freed 236(31KB) AllocSpace objects, 1(24KB) LOS objects, 39% free, 4MB/6MB, paused 157us total 4.191ms
07-11 02:06:30.642 5470-5519/com.sample.android.sdk.sample I/TestRunner: failed: testShouldVerifyReminderAndActivitiesFlowForALead(com.sample.android.sdk.sample.Tests.FirstTransactionTest)
07-11 02:06:30.642 5470-5519/com.sample.android.sdk.sample I/TestRunner: ----- begin exception -----
07-11 02:06:30.643 5470-5519/com.sample.android.sdk.sample I/TestRunner: java.lang.RuntimeException: java.lang.RuntimeException: Error in evaluationEvaluation: status: 12 value: {message=Element is not currently interactable and may not be manipulated} hasMessage: true message: Element is not currently interactable and may not be manipulated
                                                                            at android.support.test.espresso.web.sugar.Web$WebInteraction$ExceptionPropagator.<init>(Web.java:323)
                                                                            at android.support.test.espresso.web.sugar.Web$WebInteraction.doEval(Web.java:292)
                                                                            at android.support.test.espresso.web.sugar.Web$WebInteraction.perform(Web.java:259)
                                                                            at com.sample.android.sdk.sample.screen.verifyOTPScreen.tapOnVerifyOTPButton(verifyOTPScreen.java:34)
                                                                            at com.sample.android.sdk.sample.action.CheckoutAction.verifyUserIsAbleToMakeSuccessfulTransaction(CheckoutAction.java:31)
                                                                            at com.sample.android.sdk.sample.Tests.FirstTransactionTest.testShouldVerifyReminderAndActivitiesFlowForALead(FirstTransactionTest.java:21)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
                                                                            at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
                                                                            at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
                                                                            at junit.framework.TestCase.runBare(TestCase.java:134)
                                                                            at junit.framework.TestResult$1.protect(TestResult.java:115)
                                                                            at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
                                                                            at junit.framework.TestResult.run(TestResult.java:118)
                                                                            at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
                                                                            at junit.framework.TestCase.run(TestCase.java:124)
                                                                            at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
                                                                            at junit.framework.TestSuite.runTest(TestSuite.java:243)
                                                                            at junit.framework.TestSuite.run(TestSuite.java:238)
                                                                            at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
                                                                            at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69)
                                                                            at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:103)
                                                                            at org.junit.runners.Suite.runChild(Suite.java:128)
                                                                            at org.junit.runners.Suite.runChild(Suite.java:27)
                                                                            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                            at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                            at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                                                                            at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                                            at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
                                                                            at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
                                                                            at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
                                                                         Caused by: java.lang.RuntimeException: Error in evaluationEvaluation: status: 12 value: {message=Element is not currently interactable and may not be manipulated} hasMessage: true message: Element is not currently interactable and may not be manipulated
                                                                            at android.support.test.espresso.web.model.sampleeAtom.handleBadEvaluation(sampleeAtom.java:142)
                                                                            at android.support.test.espresso.web.model.sampleeAtom.transform(sampleeAtom.java:76)
                                                                            at android.support.test.espresso.web.model.sampleeAtom.transform(sampleeAtom.java:30)
                                                                            at android.support.test.espresso.web.action.AtomAction$2.apply(AtomAction.java:143)
                                                                            at android.support.test.espresso.web.action.AtomAction$2.apply(AtomAction.java:140)
                                                                            at android.support.test.espresso.web.deps.guava.util.concurrent.Futures$2.apply(Futures.java:760)
                                                                            at android.support.test.espresso.web.deps.guava.util.concurrent.Futures$ChainingListenableFuture.run(Futures.java:906)
                                                                        	at android.support.test.espresso.web.deps.guava.util.concurrent.MoreExecutors$DirectExecutor.execute(Mo
07-11 02:06:30.643 5470-5519/com.sample.android.sdk.sample I/TestRunner: ----- end exception -----
07-11 02:06:30.644 5470-5519/com.sample.android.sdk.sample I/TestRunner: finished: testShouldVerifyTransaction(com.sample.android.sdk.sample.Tests.FirstTransactionTest)
07-11 02:06:30.646 5470-5470/com.sample.android.sdk.sample I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 2
07-11 02:06:30.646 5470-5470/com.sample.android.sdk.sample I/MonitoringInstrumentation: Finishing activity: com.sample.android.sdk.view.activity.BasesampleScreen@c6f57c9
07-11 02:06:30.648 5470-5470/com.sample.android.sdk.sample I/MonitoringInstrumentation: Finishing activity: com.sample.android.sdk.sample.CheckoutActivity@444507d
07-11 02:06:30.648 5470-5470/com.sample.android.sdk.sample D/LifecycleMonitor: Lifecycle status change: com.sample.android.sdk.view.activity.BasesampleScreen@c6f57c9 in: PAUSED
07-11 02:06:30.649 5470-5519/com.sample.android.sdk.sample I/TestRunner: run finished: 1 tests, 1 failed, 0 ignored
07-11 02:06:30.653 5470-5470/com.sample.android.sdk.sample D/LifecycleMonitor: Lifecycle status change: com.sample.android.sdk.sample.CheckoutActivity@444507d in: DESTROYED
07-11 02:06:30.903 5470-5470/com.sample.android.sdk.sample D/LifecycleMonitor: Lifecycle status change: com.sample.android.sdk.view.activity.BasesampleScreen@c6f57c9 in: STOPPED

On Tue, Jul 12, 2016 at 12:04 AM, Nick Korostelev <nko


On Tue, Jul 12, 2016 at 9:57 PM, Luis Ramirez <luis...@gmail.com> wrote:

On Mon, Jul 11, 2016 at 2:18 AM, <keta...@gmail.com> wrote:
The above code doesn't throw any error and executes successfully but it doesn't click the button.

I would try specifying the web view ID and forcing javascript: onWebView(withId(R.id.web_view)).forceJavascript().withElement(...)

Luis Ramirez

unread,
Jul 13, 2016, 5:55:29 PM7/13/16
to Ketan Soni, nkor...@google.com, android-testing...@googlegroups.com

On Tue, Jul 12, 2016 at 11:59 AM, Ketan Soni <keta...@gmail.com> wrote:
Hi Nick / Luis,

Thanks for replying!

I've tried following statement as well but it fails with an exception mentioned the logcat details
onWebView(withId(R.id.__sample_web_view)).forceJavascriptEnabled().withElement(findElement(Locator.ID, "submit-try-sample")).perform(webClick());
I would try to boil your sample webview to the most minimal thing and build it back up to see what works. I've only done clicks on links so maybe try that first? Like
<a href="someplace.com" id="some-id">Click Me!</a>

It's also possible this is not working simply because the instrumentation is buggy. We experienced a case where matching the text of a given <style> block was failing because the instrumentation thought there was no text in the block even though it definitely was there.

Ketan Soni

unread,
Jul 16, 2016, 9:07:42 AM7/16/16
to Luis Ramirez, Nick Korostelev, android-testing...@googlegroups.com
Hi Luis,

I did not quite understand the first part. However i'm observing the same error as mentioned at https://github.com/googlesamples/android-testing/issues/61

Any help would be really appreciated


Ketan Soni

unread,
Jul 17, 2016, 8:56:30 AM7/17/16
to Luis Ramirez, Nick Korostelev, android-testing...@googlegroups.com
Hi Nick/Luis,

t executes the following line and proceed further but it doesn't click the button in real. I tried debugging and i'm attaching the screenshot of it. Please let me know if you can figure out anything from it. It just says Status : 0 value :null.
 

Logcat info for the same as follows:

07-17 07:06:29.395 7090-7090/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.model.TransformingAtom@132cfdd in window: null with element: null' action on view WebView with JS enabled
07-17 07:06:29.445 7090-7090/com.sample.android.sdk.sample I/ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$1@6984a23 in window: null with element: {"ELEMENT":":wdc:1468753140041"}' action on view WebView with JS enabled

It says window as null but it has a reference to element. 

I'm really stuck at this. Not sure whether its a bug. Can someone help?

Thanks

Luis Ramirez

unread,
Jul 18, 2016, 11:38:35 AM7/18/16
to Ketan Soni, Nick Korostelev, android-testing...@googlegroups.com

On Sat, Jul 16, 2016 at 8:07 AM, Ketan Soni <keta...@gmail.com> wrote:
Hi Luis,

I did not quite understand the first part. However i'm observing the same error as mentioned at https://github.com/googlesamples/android-testing/issues/61

Any help would be really appreciated

What I meant was to boil down your sample to something ultra-simple to see if it works, then gradually add back things until it breaks again to determine the cause of the problem.

So, for example, your example HTML:
<button type="button" class="button js-analytics-submit" id="submit-try-sample" data-analytics-category="Sample" data-analytics-label="Intro">
  <svg xmlns="http://www.w3.org/2000/svg" width="58" height="58" viewBox="0 0 58 58" id="button-logo
" class="button__logo">
  <path fill="currentColor" fill-rule="evenodd" d="M37.32 21.933l2.877-2.876-.626-.625c-5.04-5.043-13.247-5.043-18.29 0-4.832 4.833-5.03 12.568-.6 17.643l-2.876 2.875.625.625c5.042 5.043 13.247 5.043 18.29 0 4.832-4.832 5.03-12.567.6-17.642zm-14.79-2.25c4.144-4.144 10.76-4.344 15.142-.6l-1.64 1.64c-3.488-2.86-8.644-2.664-11.9.593-3.256 3.256-3.454 8.412-.594 11.9l-1.608 1.608c-3.743-4.382-3.542-11 .6-15.142zm12.94 18.642c-4.143 4.144-10.76 4.344-15.142.6l1.64-1.64c3.488 2.86 8.644 2.664 11.9-.593 3.256-3.256 3.454-8.412.594-11.9l1.608-1.608c3.743 4.382 3.543 10.998-.6 15.14z"></path>
</svg>
  <span class="button__wrapper" id="button-text">
    <span class="button__text">
      Let’s do this!
    </span>
  </span>
</button>
Replace all of that with:
<a href="google.com" id="anchor">Link!</a>
Can't get simpler than that. Then try clicking that link through Espresso. If that works, replace <a> with <button>. If that works, add the "class" attribute. If that works ... etc, etc. until you find the one change that makes it fail.

Ketan Soni

unread,
Jul 18, 2016, 12:24:32 PM7/18/16
to Luis Ramirez, Nick Korostelev, android-testing...@googlegroups.com
Thanks Luis for all you support.

Finally figured out the culprit. While debugging further i got to know 'touchstart' was the causing the issue. My submitEventType was set to 'touchstart' but when i set it to 'click', my statement started working. Took couple of days to figure out. Sigh!

Learned something new. Shall i raise this as a bug or new request? What do you guys think?

Thanks,
Ketan

jiangm...@gmail.com

unread,
Mar 15, 2018, 3:02:25 AM3/15/18
to Android Testing Support Library
Thanks Ketan for your post which saves my time.
I met the exact same webclick problem during testing. Is there any plan to support it?

-Mingxia

Nintex Mobile

unread,
Aug 25, 2023, 2:04:10 AM8/25/23
to Android Testing Support Library
Hi Ketan/Jiang,

Could you please share how you changed the submitEventType?  was it in yr Espresso script?

Regards,
Matt

Reply all
Reply to author
Forward
0 new messages