2

シミュレートされた Android デバイスと Appium があります。私のテストでは、特定のテキスト フィールドに適切なアクティビティとタイプが正常に起動されます。しかし、テキストを確認するために同じテキスト フィールドを見つけようとする"An element could not be located on the page using the given search parameters."と、2 回目の検索ではなく要素を再利用しようとしても、同じメッセージで失敗します。どうすればいいですか?2 番目のコンテキストfindElement()が間違っている可能性があります。テキスト フィールドの横にあるボタンも見つかりません。

アプリとテスト プロジェクトを含む git リポジトリを次に示します。失敗した JUnit テストは、問題を示しています: https://github.com/achengs/an-appium-question

以下の詳細 (コードと Appium ログのインターリーブ)

最初に成功した findElement を次に示します。アクティビティのレイアウト 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

失敗した 2 番目の 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 アプリをテストしています。テスト中の現在のアクティビティのレイアウト 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

4 に答える 4

6

使用する ID は"edit_message"ではなく、むしろ である必要があります"com.company.app:id/edit_message"

基本的に、layout.xml ファイルで UI 要素の ID が指定されている場合android:id="@+id/foo"(アプリが com.company.app の場合)、テストで"com.company.app:id/foo""foo".

just"foo"を使用すると、UI 要素を見つけることができる場合がありますが、それを 1 回操作した後は、UI で何も見つけることができなくなります。これがなぜなのかはわかりませんが、問題を混乱させます。

于 2013-12-13T16:26:53.543 に答える
2

SDK に付属の Android インスペクターを試してみてください。ツリー内の正確な要素の ID だけでなく、他の多くの詳細を提供できます。xPath がないため、目的に応じて appium インスペクターを引き続き使用する必要がある場合があります。で探してください

./path-to-sdk/android-sdk-macosx/tools/uiautomatorviewer
于 2013-12-05T21:11:58.703 に答える
0

スレッドスリープを入れます。これは、アプリのアクティビティが起動するまでに時間がかかるために発生します。その前に、UI オートメーションは要素を見つけることができません。キーを送信する前にスレッドをスリープ状態にすることができます

于 2014-07-29T12:28:28.710 に答える
0

テキストを入力した後に要素を検査し、入力後に ID が変更されていないことを確認します。

于 2013-12-11T07:22:14.620 に答える