0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Appium②】コード書いて動かす編

Posted at

【Appium①】環境構築編 の続き

はじめに

前回で環境構築は終わったので今回は実際にコードを書いて動かすところまでやります。

環境

詳しくは前回の記事参照、新しく使うものだけを記載します。

  • IntelliJ IDEA 2022.3.3 (Community Edition)

手順

※Appiumとエミュ(もしくは実機接続)を起動しておく

  1. 依存関係を書く
  2. コード書く
  3. 実行する

依存関係を書く

  1. Java の Maven で新しいプロジェクトを作成する
    inteli.png

  2. pom.xml に依存関係を書く
    3つ依存関係を追記します。
    io.appium
    JUnit Jupiter API
    JUnit Jupiter Engine
    最新バージョンを選んでおけば無難だと思います(しらんけど)。
    のところを pom.xml にコピペします。
    こうなればOKです。
    バージョンは最新バージョンで置き換えてください。
    ison.png
    version が赤くなってエラーになる場合は、左ペインのツリーの pom.xml 右クリック > Maven > プロジェクトの再ロード で改善すると思います。

  3. Javaに必要なものをimportする
    Appium Inspector を起動して Session Information タブをクリック、Start this Kind of Session with Code の内容をJavaファイルにコピペします(右のプルダウンをJava - JUnitにする)。
    結構長いので忘れずに下まで。
    appi.png

  4. 諸々編集する
    コピペしたものは依存関係に追加したものと異なるのでそのままだと使えません。
    なので以下のように直します。
    ・Before > BeforeEach
    ・After > AfterEach
    importも変えます。
    ・import io.appium.java_client.MobileElement; > import org.openqa.selenium.WebElement;
    ・import org.junit.After; > import org.junit.jupiter.api.AfterEach;
    ・import org.junit.Before; > import org.junit.jupiter.api.BeforeEach;
    ・import org.junit.Test; > import org.junit.jupiter.api.Test;
    以上で準備は整いました。

  5. コードを書く
    試しにホーム画面の電話アプリがちゃんと起動するかどうかをテストします。

@Test
    public void sampleTest() {
        WebElement phone = (WebElement) driver.findElement(By.xpath("//android.widget.TextView[@content-desc=\"電話\"]"));
        phone.click();
        String expectedText = "連絡先や場所を検索";
        String actualText = driver.findElement(By.id("com.google.android.dialer:id/open_search_bar_text_view")).getText();
        assertEquals(expectedText, actualText);
    }

電話アイコンをタップして「連絡先や場所を検索」という文言があれば成功とします。
assertEqualsは赤くなると思いますが該当箇所でAlt+Enterを押せば勝手にimport文を追加して解決してくれます。
これを実行して成功すれば終わりです。
お疲れ様でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?