Appium + Android + WebDriver findElement():sendKeys()之后找不到元素?

时间:2013-12-04 15:46:50

标签: android selenium appium

我有一个模拟Android设备和Appium。我的测试成功启动了正确的Activity并在特定的文本字段中输入。但是当我尝试找到相同的文本字段以检查其中的文本时,我得到"An element could not be located on the page using the given search parameters."即使我尝试重新使用该元素而不是第二次搜索它,它仍然会失败同样的消息。我该怎么办?也许第二个findElement()的上下文是错误的 - 我找不到文本字段旁边的按钮。

这是一个包含应用程序和测试项目的git仓库。失败的JUnit测试演示了此问题:https://github.com/achengs/an-appium-question

下面的详细信息(代码和Appium日志交错)

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

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

第一个findElement成功:

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并重新使用原始的 - 或者如果我尝试在文本字段旁边找到“发送”按钮,我仍会遇到类似的失败)

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应用。这是当前正在测试的Activity的布局xml。如果我还应该包含其他内容,请告诉我。

<?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>

4 个答案:

答案 0 :(得分:6)

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

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

仅使用"foo"可能允许找到UI元素,但在与其进行单次交互后,您将无法在UI中找到任何内容。不知道为什么会这样,但它让人感到困惑。

答案 1 :(得分:2)

您应该尝试使用sdk附带的android检查器。它不仅可以为您提供树中确切元素的ID,还可以提供许多其他细节。它没有xPath,因此您可能仍需要使用appium检查器,具体取决于您要执行的操作。

寻找它
./path-to-sdk/android-sdk-macosx/tools/uiautomatorviewer

答案 2 :(得分:0)

输入文本后检查元素,并确保在输入

后ID未更改

答案 3 :(得分:0)

让一些线程睡觉。发生这种情况是因为您的应用活动需要一些时间才能启动。之前,UI自动机无法找到该元素。您可以在发送密钥之前设置线程休眠