Espresso-Web

Espresso-Web は、Android WebView UI コンポーネントを操作するためのエントリ ポイントです。 広く利用されている WebDriver API から Atom を再利用し、WebView の動作の確認や制御が行えます。

Espresso-Web を使用するケース

Espresso-Web を使用して、ハイブリッド アプリ(特に アプリのネイティブ UI コンポーネントを WebView に置き換える UI コンポーネントEspresso-Web API は、他の Google Cloud サービスと WebView オブジェクト内のウェブ要素を完全に操作するための Espresso API。

WebView 自体だけをテストし、 アプリの WebView とネイティブ コンポーネントの間のインタラクションについては、 WebDriver などのフレームワークを使用して一般的なウェブテストを作成する。ウェブテスト フレームワークを使用する場合、 テストを実行するために、Android デバイスまたは Java 仮想マシン より高速かつ確実に実行できますただし、Espresso-Web を使用すると カスタムの WebDriver atom を使用できるため、 スタンドアロンのウェブアプリと ウェブアプリの両方で実行できるテストを Android UI を含むアプリ。

仕組み

Espresso の onData() と同様 メソッドの場合、WebView インタラクションは複数の Atom で構成されます。 WebView インタラクションでは、Java プログラミング言語と 使用します。新しい P-MAX キャンペーンを JavaScript 環境からのデータ公開によって競合状態を Espresso から見た Java ベースのサイドは分離されたコピーで、 Web.WebInteraction 完全にサポートされているため、インスタンスから返されたすべてのデータ、 できます。

WebDriver Atom とは

WebDriver フレームワークは Atoms を使用してウェブ要素を検出、操作 できます。WebDriver でのブラウザ操作は Atom を通じて実現しており、「 概念的には、Atom は ViewAction: 自己完結型の UI でアクションを実行するユニットです。Atoms を公開するには、 findElement()getElement() などの定義済みのメソッドを使用して、 おすすめします。ただし、WebDriver を使用して 直接やり取りするには、Atoms を適切にオーケストレートする必要があり、 非常に冗長です。

Espresso 内のクラス Web および Web.WebInteraction このボイラープレートをラップして、WebView を Espresso のように操作できるようにする 説明します。そのため、WebView のコンテキストでは、Atom は 従来の Espresso ViewMatchers および ViewActions に代わるものです。

API の表記は、次のように非常にシンプルです。

KotlinJava
onWebView()
   
.withElement(Atom)
   
.perform(Atom)
   
.check(WebAssertion)
onWebView()
   
.withElement(Atom)
   
.perform(Atom)
   
.check(WebAssertion);

詳細については、Selenium の Atom に関するドキュメントをご覧ください。

WebView の実装

以下の各セクションのガイダンスに沿って操作してください。 アプリのテスト内の WebView

パッケージ

Espresso-Web をプロジェクトに含めるには、次の手順を行います。

  1. アプリの build.gradle ファイルを開きます。これは通常 トップレベルの build.gradle ファイルと app/build.gradle
  2. 依存関係内に次の行を追加します。

    GroovyKotlin
        androidTestImplementation 'androidx.test.espresso:espresso-web:3.6.1'
       
        androidTestImplementation('androidx.test.espresso:espresso-web:3.6.1')
       
  3. Espresso-Web は Espresso 2.2 以降にのみ対応しており、 バージョン 0.3 以降のテスト用ライブラリを 行も確認できます。

    GroovyKotlin
        androidTestImplementation 'androidx.test:runner:1.6.1'
        androidTestImplementation
    'androidx.test:rules:1.6.1'
        androidTestImplementation
    'androidx.test.espresso:espresso-core:3.6.1'
       
        androidTestImplementation('androidx.test:runner:1.6.1')
        androidTestImplementation
    ('androidx.test:rules:1.6.1')
        androidTestImplementation
    ('androidx.test.espresso:espresso-core:3.6.1')
       

API の一般的な使用方法

onWebView() メソッドは、Android で WebView を使用する場合の主なエントリ ポイントです。 Espressoこのメソッドは、次のような Espresso-Web テストを実行するために使用します。 次のとおりです。

KotlinJava
onWebView()
   
.withElement(findElement(Locator.ID, "link_2")) // similar to onView(withId(...))
   
.perform(webClick()) // Similar to perform(click())

   
// Similar to check(matches(...))
   
.check(webMatches(getCurrentUrl(), containsString("navigation_2.html")))
onWebView()
   
.withElement(findElement(Locator.ID, "link_2")) // similar to onView(withId(...))
   
.perform(webClick()) // Similar to perform(click())

   
// Similar to check(matches(...))
   
.check(webMatches(getCurrentUrl(), containsString("navigation_2.html")));

この例では、Espresso-Web が ID が "link_2" の DOM 要素を探し、 それをクリックします。次に、WebView が GET リクエストを送信することを確認します。 "navigation_2.html" という文字列が含まれます。

JavaScript のサポート

テストを実行すると、システムはすべての WebView インタラクションを JavaScriptそのため、JavaScript の評価をサポートするために、テスト対象の WebView は JavaScript が有効になっている必要があります。

JavaScript を強制的に有効にするには、 forceJavascriptEnabled() として テストします。 追加します。

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
   
@get:Rule val activityScenarioRule =
        activityScenarioRule
<MyWebViewActivity>()

   
@Test fun testWebViewInteraction() {
       
onWebView().forceJavascriptEnabled()
   
}
}

一般的なウェブ操作

Web.WebInteraction オブジェクトを使用する一般的な操作には、次のようなものがあります。

  • withElement() WebView 内の DOM 要素を参照します。

    例:

    KotlinJava
    onWebView().withElement(findElement(Locator.ID, "teacher"))
    onWebView().withElement(findElement(Locator.ID, "teacher"));
  • <ph type="x-smartling-placeholder"></ph> withContextualElement() はスコープが設定された DOM 要素を参照しています 別の DOM 要素に対して相対的に決定されます。 まず withElement() で参照を確立する Web.WebInteraction オブジェクト(DOM 要素)。

    例:

    KotlinJava
    .withElement(findElement(Locator.ID, "teacher"))
       
    .withContextualElement(findElement(Locator.ID, "person_name"))
    .withElement(findElement(Locator.ID, "teacher"))
       
    .withContextualElement(findElement(Locator.ID, "person_name"));
  • <ph type="x-smartling-placeholder"></ph> check() は条件を評価し、解決することを確認します 宛先: true

    例:

    KotlinJava
    onWebView()
       
    .withElement(findElement(Locator.ID, "teacher"))
       
    .withContextualElement(findElement(Locator.ID, "person_name"))
       
    .check(webMatches(getText(), containsString("Socrates")))
    onWebView()
       
    .withElement(findElement(Locator.ID, "teacher"))
       
    .withContextualElement(findElement(Locator.ID, "person_name"))
       
    .check(webMatches(getText(), containsString("Socrates")));
  • <ph type="x-smartling-placeholder"></ph> perform() は、WebView 内で次のようなアクションを実行します。 要素をクリックするだけです

    例:

    KotlinJava
    onWebView()
       
    .withElement(findElement(Locator.ID, "teacher"))
       
    .perform(webClick())
    onWebView()
       
    .withElement(findElement(Locator.ID, "teacher"))
       
    .perform(webClick());
  • reset() WebView を初期状態に戻します。これは事前トレーニング済みの クリックなどのアクションによってナビゲーションが変化し、 ElementReference オブジェクトと WindowReference オブジェクトにアクセスできません。

    注: reset() を使用すると、 複数ページのワークフローに対するアサーションの作成 通常、テストの範囲を限定し、1 つのページに焦点を絞るべきです。

    例:

    KotlinJava
    onWebView()
       
    .withElement(...)
       
    .perform(...)
       
    .reset()
    onWebView()
       
    .withElement(...)
       
    .perform(...)
       
    .reset();

次の例では、WebView にテキストを入力した後、 [Submit] ボタンを選択すると、同じテキストが 使用します。

KotlinJava
const val MACCHIATO = "Macchiato"

@RunWith(AndroidJUnit4::class)
class MyEspressoWebTestSuite {

   
@Test fun typeTextInInput_clickButton_SubmitsForm() {
       
// Create an intent that displays a web form.
       
val webFormIntent = Intent()
       
// ...

       
// Lazily launch the Activity with a custom start Intent per test.
       
ActivityScenario.launchActivity(webFormIntent)

       
// Selects the WebView in your layout. If you have multiple WebView
       
// objects, you can also use a matcher to select a given WebView,
       
// onWebView(withId(R.id.web_view)).
        onWebView
()
           
// Find the input element by ID.
           
.withElement(findElement(Locator.ID, "text_input"))

           
// Clear previous input and enter new text into the input element.
           
.perform(clearElement())
           
.perform(DriverAtoms.webKeys(MACCHIATO))

           
// Find the "Submit" button and simulate a click using JavaScript.
           
.withElement(findElement(Locator.ID, "submitBtn"))
           
.perform(webClick())

           
// Find the response element by ID, and verify that it contains the
           
// entered text.
           
.withElement(findElement(Locator.ID, "response"))
           
.check(webMatches(getText(), containsString(MACCHIATO)))
   
}
}
public static final String MACCHIATO = "Macchiato";

@Test
public void typeTextInInput_clickButton_SubmitsForm() {
   
// Create an intent that displays a web form.
   
Intent webFormIntent = new Intent();
   
// ...

   
// Lazily launch the Activity with a custom start Intent per test.
   
ActivityScenario.launchActivity(webFormIntent);

   
// Selects the WebView in your layout. If you have multiple WebView objects,
   
// you can also use a matcher to select a given WebView,
   
// onWebView(withId(R.id.web_view)).
    onWebView
()
       
// Find the input element by ID.
       
.withElement(findElement(Locator.ID, "text_input"))

       
// Clear previous input and enter new text into the input element.
       
.perform(clearElement())
       
.perform(DriverAtoms.webKeys(MACCHIATO))

       
// Find the "Submit" button and simulate a click using JavaScript.
       
.withElement(findElement(Locator.ID, "submitBtn"))
       
.perform(webClick())

       
// Find the response element by ID, and verify that it contains the
       
// entered text.
       
.withElement(findElement(Locator.ID, "response"))
       
.check(webMatches(getText(), containsString(MACCHIATO)));
}

参考情報

Android テストで Espresso-Web を使用する方法について詳しくは、 ご覧ください

サンプル

  • WebBasicSample Espresso-Web を使用して WebView オブジェクトを操作します。