シミュレートされた 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>