切换WebView后无法使用findElement

时间:2018-09-10 12:50:42

标签: android android-espresso

我有一个适用于Android的简单Espresso测试,在“活动”切换之前,该测试正常进行。该测试通过登录运行,按类从Web视图中选择元素,然后在成功登录后切换活动。一旦进入新的Activity,就无法通过类,css选择器或x_path在新的webview中找到任何内容。

我知道这是隐藏在html中的,我可以看到它并在chrome insepctor中搜索它:

<div _ngcontent-c9="" class="tile-title" title="Pay">Pay </div>

但是我无法通过此CSS_SELECTOR找到它:

onWebView(allOf<View>(isDisplayed(), isJavascriptEnabled()))
                    .withElement(findElement(Locator.CSS_SELECTOR, "div[title=Pay]"))

类似地,我也尝试过使用X_PATH字符串,甚至还尝试使用类名(不是唯一的),但是仍然失败。

我们确实创建了多个Web视图,但是只有一个可见。

我该怎么做才能更清楚地说明为什么我不能选择有问题的div?

更新

看来我在日志中错过了一些揭示我部分问题的东西。我在日志中看到一个AmbiguousViewMatcherException

看代码,我们有一个名为web_container的FrameLayout,并通过addView()调用将3个Webview附加到视图。因此,此组件最终有3个子代。

我将测试更改为使用withParent()isDisplayed()来匹配网络视图。该调用之前曾引发异常,但现在通过了。

onWebView(allOf(withParent(withId(R.id.web_container)), isDisplayed())).forceJavascriptEnabled()

鉴于此成功,我仍然无法在此Webview中找到任何东西,这是修改后的onWebVeiw语句:

onWebView(allOf(withParent(withId(R.id.web_container)), isDisplayed())).forceJavascriptEnabled()

 Log.d("FeatureTest", "passed forceJavaScriptEnabled()")    
onWebView(allOf(withParent(withId(R.id.web_container)), isDisplayed()))
                        .withElement(findElement(Locator.CSS_SELECTOR, "div[title=Pay]")).perform(webClick())

我在日志中看到的错误:

09-21 09:19:19.490  9481  9529 D FeatureTest: Key: Mobile, with visibility: 0
09-21 09:19:19.490  9481  9529 D FeatureTest: Key: RDBX, with visibility: 4
09-21 09:19:19.490  9481  9529 D FeatureTest: Key: DEFAULT, with visibility: 4
09-21 09:19:19.496  9481  9481 I ViewInteraction: Performing 'Forcibly enable javascript.' action on view (has parent matching: with id: com.package.android.debug:id/web_container and is displayed on the screen to the user)
09-21 09:19:19.496  9481  9529 D FeatureTest: passed forceJavaScriptEnabled()
09-21 09:19:19.499  9481  9481 I ViewInteraction: Performing 'Evaluate Atom: android.support.test.espresso.web.webdriver.DriverAtoms$FindElementTransformingAtom@7c99c3e in window: null with element: null' action on view (has parent matching: with id: com.package.android.debug:id/web_container and is displayed on the screen to the user)
0

09-21 09:19:29.505  9481  9481 I ViewInteraction: Performing 'Propagate: java.lang.RuntimeException: java.util.concurrent.TimeoutException: Waited 10 seconds for android.support.test.espresso.web.internal.deps.guava.util.concurrent.AbstractTransformFuture$TransformFuture@8c677ec[status=PENDING, info=[inputFuture=[android.support.test.espresso.web.internal.deps.guava.util.concurrent.SettableFuture@19327b5[status=PENDING]], function=[android.support.test.espresso.web.action.AtomAction$3@cbf3e4a]]]' action on view (has parent matching: with id: com.package.android.debug:id/web_container and is displayed on the screen to the user)
09-21 09:19:29.515  9481  9529 D FeatureTest: java.lang.RuntimeException@89926d8[cause=java.util.concurrent.TimeoutException: Waited 10 seconds for android.support.test.espresso.web.internal.deps.guava.util.concurrent.AbstractTransformFuture$TransformFuture@8c677ec[status=PENDING, info=[inputFuture=[android.support.test.espresso.web.internal.deps.guava.util.concurrent.SettableFuture@19327b5[status=PENDING]], function=[android.support.test.espresso.web.action.AtomAction$3@cbf3e4a]]],detailMessage=java.util.concurrent.TimeoutException: Waited 10 seconds for android.support.test.espresso.web.internal.deps.guava.util.concurrent.AbstractTransformFuture$TransformFuture@8c677ec[status=PENDING, info=[inputFuture=[android.support.test.espresso.web.internal.deps.guava.util.concurrent.SettableFuture@19327b5[status=PENDING]], function=[android.support.test.espresso.web.action.AtomAction$3@cbf3e4a]]],stackTrace={},suppressedExceptions=[]]
09-21 09:19:29.516  9481  9481 W e.android.debu: Accessing hidden field Landroid/app/Activity;->mResultCode:I (light greylist, reflection)
09-21 09:19:29.517  9481  9481 W e.android.debu: Accessing hidden field Landroid/app/Activity;->mResultData:Landroid/content/Intent; (light greylist, reflection)
09-21 09:19:29.517  9481  9529 I TestRunner: failed: testFeature(com.package.android.ui.FeatureTest)

1 个答案:

答案 0 :(得分:0)

您需要在第一次互动结束时致电Caused by: java.lang.IllegalArgumentException: JBAS011470: O unitName persistente não foi especificado e existem definições de unidade ...

否则,您将无法访问(或匹配)该引用。

.reset()

作为Espresso Web的documentation

  

onWebView(allOf<View>(isDisplayed(), isJavascriptEnabled())) .withElement(findElement(Locator.CSS_SELECTOR, "div[title=Pay]")) .perform( ... ) .reset(); 将WebView还原为其初始状态。当诸如单击之类的先前操作引入了导航更改从而使ElementReference和WindowReference对象不可访问时,这是必需的。

然后再次匹配,将提供一个新的引用,可以对其进行访问。