Appium + Android + WebDriver findElement():sendKeys() 后找不到元素? [英] Appium + Android + WebDriver findElement() : cannot find element after sendKeys()?

查看:28
本文介绍了Appium + Android + WebDriver findElement():sendKeys() 后找不到元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模拟的 Android 设备和 Appium.我的测试成功启动了正确的 Activity 并在特定文本字段中键入.但是,当我尝试查找相同的文本字段以检查其中的文本时,我得到 无法使用给定的搜索参数在页面上找到元素." 即使我尝试重新使用该元素而不是第二次搜索它,它仍然失败并显示相同的消息.我应该怎么做?也许第二个 findElement() 的上下文是错误的——我也找不到文本字段旁边的按钮.

I have a simulated Android device and Appium. My test successfully launches the right Activity and types in a particular text field. But when I try to find the same text field in order to check the text in it, I get "An element could not be located on the page using the given search parameters." Even if I try to re-use the element instead of searching for it a second time, it still fails with the same message. What should I do differently? Maybe the context for the second findElement() is wrong -- I can't find the button next to the text field either.

这是一个包含应用程序和测试项目的 git 存储库.失败的 JUnit 测试说明了这个问题:https://github.com/achengs/an-appium-question

Here's a git repo that contains an app and a test project. The failed JUnit test demonstrates the issue: https://github.com/achengs/an-appium-question

详情如下(代码和 Appium 日志交错)

Details below (code and Appium log interleaved)

这是第一个成功的 findElement.Activity 的布局 xml 文件具有我正在查找的文本字段的此属性:android:id="@+id/edit_message"

Here's the first findElement which succeeds. The layout xml file for the Activity has this attribute for the text field that I'm looking for: android:id="@+id/edit_message"

public static final String MESSAGE_TO_SEND = "edit_message";
...
WebElement e = driver.findElement(By.id(MESSAGE_TO_SEND));

第一个 findElement 成功:

First findElement succeeds:

debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element
debug: Request received with params: {"using":"id","value":"edit_message"}
info: Pushing command to appium work queue: ["find",{"strategy":"id","selector":"edit_message","context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"edit_message","context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding edit_message using ID with the contextId: 
info: [BOOTSTRAP] [info] Returning result: {"value":{"ELEMENT":"1"},"status":0}
info: Responding to client with success: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element 200 5656ms - 109b

问好!

String text = "hello!";
e.sendKeys(text);

成功了:

debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element/1/value
debug: Request received with params: {"id":"1","value":["hello!"]}
info: Pushing command to appium work queue: ["element:setText",{"elementId":"1","text":"hello!"}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"element:setText","params":{"elementId":"1","text":"hello!"}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: setText
info: [BOOTSTRAP] [info] Returning result: {"value":true,"status":0}
info: Responding to client with success: {"status":0,"value":true,"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element/1/value 200 4215ms - 89b

这是第二个失败的 findElement.(如果我跳过这个 findElement 并重新使用原始的 - 或者如果我尝试在文本字段旁边找到发送按钮,我仍然会遇到类似的失败)

Here's the second findElement which fails. (If I skip this findElement and re-use the original one -- or if I try to find the Send button next to the text field instead, I still get a similar failure)

WebElement f = driver.findElement(By.id(MESSAGE_TO_SEND));

这是失败的日志:

debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element
debug: Request received with params: {"using":"id","value":"edit_message"}
info: Pushing command to appium work queue: ["find",{"strategy":"id","selector":"edit_message","context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"edit_message","context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding edit_message using ID with the contextId: 
info: [BOOTSTRAP] [info] Returning result: {"value":"No element found","status":7}
info: Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element 500 874ms - 223b

有一个对 HTML 的请求.我正在测试本机 Android 应用程序.这是当前被测活动的布局 xml.如果还有什么我应该在这里包含的内容,请告诉我.

There was a request for the HTML. I'm testing a native Android app. Here's the layout xml for the current Activity under test. If there's something else I should include here, please do let me know.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/button_send"
   android:onClick="sendMessage" android:id="@+id/send"/>
</LinearLayout>

推荐答案

使用的 ID 不应是 "edit_message" 而是 "com.company.app:id/edit_message".

The ID used should not be "edit_message" but rather "com.company.app:id/edit_message".

基本上,如果您的 layout.xml 文件使用 android:id="@+id/foo"(并且您的应用程序是 com.company.app)为您的 UI 元素指定了 ID,那么在您的测试你应该使用 "com.company.app:id/foo" 而不是 "foo".

Basically if your layout.xml file specifies an ID for your UI element with android:id="@+id/foo" (and your app is com.company.app) then in your test you should use "com.company.app:id/foo" instead of just "foo".

仅使用 "foo" 可能允许找到 UI 元素,但在与它的单次交互后,您将无法在 UI 中找到任何内容.不知道为什么会这样,但它混淆了问题.

Using just "foo" may allow the UI element to be found but after a single interaction with it you will be unable to find anything in the UI. Not sure why this is, but it confuses matters.

这篇关于Appium + Android + WebDriver findElement():sendKeys() 后找不到元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆