Appium +的Andr​​oid +的webdriver findElement():找不到的SendKeys后元素()? [英] Appium + Android + WebDriver findElement() : cannot find element after sendKeys()?

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

问题描述

我有一个模拟的Andr​​oid设备和Appium。我测试的成功发射在一个特定的文本字段右边的活动和类型。但是,当我试图找到以检查它的文本相同的文本字段,我得到的元素找不到使用给定的搜索参数在页面上。即使我尝试重新使用,而不是寻找它第二次的元素,它仍然失败,同样的信息。我应该怎么做不同?也许对于上下文中的第二个 findElement()是错的 - 我不能找到旁边的文本字段的按钮或者

下面是一个混帐回购协议包含应用程序和测试项目。失败的JUnit测试演示了该问题: https://github.com/achengs/an-appium-question

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

下面是它成功的第一个findElement。该活动的布局xml文件中有我要找的文本字段此属性:机器人:ID =@ + ID / edit_message

 公共静态最后弦乐MESSAGE_TO_SEND =edit_message;
...
WebElement E = driver.findElement(By.id(MESSAGE_TO_SEND));

首先findElement成功:

 调试:在/ WD /集线器/会话/ 0ec259be-87e0-47f6-9279-da577fe29a07 /元素Appium请求启动
调试:使用参数收到的请求:{用:ID,值:edit_message}
信息:推命令appium工作队列:查找,{战略:ID,选择:edit_message,背景:,多:假}]
信息:[引导] [信息]得到客户端的数据: {\"cmd\":\"action\",\"action\":\"find\",\"params\":{\"strategy\":\"id\",\"selector\":\"edit_message\",\"context\":\"\",\"multiple\":false}}
信息:[引导] [资讯]知道类型的动作命令
信息:[引导] [调试]得到命令操作:找到
信息:[引导] [调试]查找使用ID与关联标识符edit_message:
信息:[引导] [资讯]返回结果:{价值:{元素:1},状态:0}
信息:应对成功客户端:{身份:0,价值:{元素:1},的sessionId:0ec259be-87e0-47f6-9279-da577fe29a07}
POST / WD /集线器/会话/ 0ec259be-87e0-47f6-9279-da577fe29a07 /元件200 5656ms - 109B

打个招呼吧!

 字符串文本=你好!
e.sendKeys(文本);

这是成功的:

 调试:在/ WD /集线器/会话/ 0ec259be-87e0-47f6-9279-da577fe29a07 /元/ 1 /值Appium请求启动
调试:使用参数收到的请求:{ID:1,值:你好!]}
信息:推命令appium工作队列:元素:的setText,{elementId:1,文:你好!}]
信息:[引导] [信息]得到客户数据:{CMD:行动,行动:元素:的setText,PARAMS:{elementId:1,文:你好!}}
信息:[引导] [资讯]知道类型的动作命令
信息:[引导] [调试]得到命令操作:的setText
信息:[引导] [资讯]返回结果:{值:真的,状态:0}
信息:应对成功客户端:{身份:0,值:真实的的sessionId:0ec259be-87e0-47f6-9279-da577fe29a07}
POST / WD /集线器/会话/ 0ec259be-87e0-47f6-9279-da577fe29a07 /元/ 1 /值200 4215ms - 89B

下面是它失败的第二findElement。 (如果我跳过此findElement并重新使用原来的 - 或者,如果我试图找到发送按钮旁边的文本字段,而不是,我仍然得到了类似的故障)

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

下面是该日志失败:

 调试:在/ WD /集线器/会话/ 0ec259be-87e0-47f6-9279-da577fe29a07 /元素Appium请求启动
调试:使用参数收到的请求:{用:ID,值:edit_message}
信息:推命令appium工作队列:查找,{战略:ID,选择:edit_message,背景:,多:假}]
信息:[引导] [信息]得到客户端的数据: {\"cmd\":\"action\",\"action\":\"find\",\"params\":{\"strategy\":\"id\",\"selector\":\"edit_message\",\"context\":\"\",\"multiple\":false}}
信息:[引导] [资讯]知道类型的动作命令
信息:[引导] [调试]得到命令操作:找到
信息:[引导] [调试]查找使用ID与关联标识符edit_message:
信息:[引导] [资讯]返回结果:{值:没有找到元素,状态:7}
信息:响应客户端的错误:{身份:7,值:{消息:一个元素不能在页面上使用给定的搜索参数位于,origValue:未找到元素},的sessionId:0ec259be-87e0-47f6-9279-da577fe29a07}
POST / WD /集线器/会话/ 0ec259be-87e0-47f6-9279-da577fe29a07 / 500元874ms - 223B

有是在HTML的请求。我测试原生Android应用程序。下面是测试当前Activity的布局XML。如果有别的我应该包括在这里,请不要让我知道。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
< EditText上机器人:ID =@ + ID / edit_message
    机器人:layout_weight =1
    机器人:layout_width =0dp
    机器人:layout_height =WRAP_CONTENT
    机器人:提示=@字符串/ edit_message/>
<按钮
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:文字=@字符串/ button_send
   安卓的onClick =的sendMessage机器人:ID =@ + ID /发送/>
< / LinearLayout中>


解决方案

用于不应该是edit_message而是融为一体的ID。 company.app:id/edit_message

基本上,如果你layout.xml文件使用为您的UI元素的ID机器人:ID =@ + ID /富(和你的应用程序是com.company 。应用程序),然后在您的测试,你应该使用com.company.app:id/foo而不仅仅是foo的

仅仅使用foo的可允许UI元素被发现,但它一个互动之后,你将无法找到在UI东西。不知道这是为什么,但是它混淆的问题。

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.

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

Details below (code and Appium log interleaved)

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));

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

Saying hello!

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

That succeeds:

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

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));

Here's the log for the failure:

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

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>

解决方案

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

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".

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 +的Andr​​oid +的webdriver findElement():找不到的SendKeys后元素()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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