getContexts
WebdriverIOのgetContexts
メソッドは、デフォルトのAppiumのcontexts
(および以前のWebdriverIOのgetContexts
)コマンドの改良版です。モバイルアプリセッションで利用可能なコンテキストに関する詳細かつ実用的な情報を提供し、デフォルトのAppiumメソッドの制限に対処します。
Webviewがどのように動作し、このメソッドがどのように役立つか
詳細については、ハイブリッドアプリのドキュメントを参照してください。以下はgetContexts
コマンドによって対処される課題の概要です:
Androidの課題
- 単一のwebview(例:
WEBVIEW_{packageName}
)には、複数のページ(ブラウザのタブに類似)が含まれる場合があります。 - デフォルトのAppiumメソッドでは、
title
、url
、または可視性などのこれらのページに関する詳細が含まれておらず、 正しいページを識別することが難しく、潜在的な不安定性につながることがあります。
iOSの課題
- デフォルトのAppiumメソッドは、追加のメタデータなしに一般的なwebview ID(例:
WEBVIEW_{id}
)のみを返します。 - これにより、どのwebviewがターゲットアプリ画面に対応しているかを判断することが難しくなります。
強化されたgetContexts
メソッドは、詳細なコンテキストオブジェクトを返すことでこれらの問題を解決します:
- Android向け:
title
、url
、packageName
、webviewPageId
、およびレイアウトの詳細(screenX
、screenY
、width
、およびheight
)。 - iOS向け:
bundleId
、title
、およびurl
。
これらの強化により、ハイブリッドアプリのデバッグと操作がより信頼性の高いものになります。
このメソッドを使用する理由
デフォルトでは、Appiumのcontexts
メソッドは利用可能なコンテキストを表す文字列の配列のみを返します:
- Android向け:
['NATIVE_APP', 'WEBVIEW_com.wdiodemoapp', ...]
- iOS向け:
['NATIVE_APP', 'WEBVIEW_84392.1', ...]
単純なシナリオでは十分ですが、これらのデフォルトレスポンスにはハイブリッドアプリテストに不可欠なメタデータが不足しています:
- Android向け: ページ固有のメタデータがないと、正しいwebviewとのインタラクションが困難になります。
- iOS向け: 一般的なwebview IDには、それらが表すコンテンツやアプリ画面に関する洞察がありません。
強化されたgetContexts
メソッドが提供するもの:
- AndroidとiOS両方の詳細なメタデータ。
- より良いターゲティングとインタラクションのた めに、返されるコンテキストをフィルタリングしカスタマイズするオプション。
注意と制限
- 強化された
getContexts
メソッドは、AndroidとiOSの両方のプラットフォームで動作します。ただし、返されるデータはプラットフォームとテスト対象アプリによって異なる場合があります。 returnDetailedContexts
オプションを指定しない場合、このメソッドはデフォルトのAppiumcontexts
メソッドのように動作し、シンプルなコンテキスト配列を返します。- 「デフォルト」のAppium
contexts
メソッドを使用するには、driver.getAppiumContexts()
を使用してください。詳細については、Appium Contextsドキュメントを参照してください。
Android Webviews:
androidWebviewData
などのメタデータは、returnAndroidDescriptionData
がtrue
の場合にのみ利用可能です。- Chromeブラウザで
getContexts
メソッドを使用すると、ブラウザ/Webview/ChromeDriverのバージョンが一致しないため、時々不完全なデータが返されること があります。そのような場合、デフォルト値または不正確なwebviewPageId
(例:0
)が返される可能性があります。
パラメータ
名前 | タイプ | 詳細 |
---|---|---|
options optional | GetContextsOptions | getContexts オプション(オプション) |
options.returnDetailedContexts optional | boolean | デフォルトでは、デフォルトのAppium contexts APIに基づいてコンテキスト名のみを返します。すべてのデータを取得したい場合は、これをtrue に設定できます。デフォルトはfalse です(オプション)。 |
options.androidWebviewConnectionRetryTime optional | number | webviewへの接続を再試行する間に待機するミリ秒単位の時間。デフォルトは500 ミリ秒です(オプション)。ANDROIDのみ |
options.androidWebviewConnectTimeout optional | number | ウェブビューページが検出されるのを待つ最大時間(ミリ秒)。デフォルトは5000 ミリ秒です(オプション)。ANDROIDのみ |
options.filterByCurrentAndroidApp optional | boolean | デフォルトではすべてのwebviewを返します。開いている現在のAndroidアプリでwebviewをフィルタリングしたい場合は、これをtrue に設定できます。デフォルトはfalse です(オプション)。注意:この「制限」に基づいてWebviewが見つからない可能性があることに注意してください。 ANDROIDのみ |
options.isAndroidWebviewVisible optional | boolean | デフォルトでは、接続されて表示されているwebviewのみを返します。すべてのwebviewを取得したい場合は、これをfalse に設定できます(オプション)。デフォルトはtrue です。ANDROIDのみ |
options.returnAndroidDescriptionData optional | boolean | デフォルトではAndroid Webview(Chrome)の説明データはありません。すべてのデータを取得したい場合は、これをtrue に設定できます。デフォルトはfalse です(オプション)。このオプションを有効にすると、レスポンスに追加データが含まれます。詳細については description.data.test.js を参照してください。ANDROIDのみ |
例
example.test.js
it('should return all contexts in the current session with the default Appium `contexts`-method.', async () => {
// For Android
await driver.getContexts()
// Returns ['NATIVE_APP', 'WEBVIEW_com.wdiodemoapp', ...]
//
// For iOS, the context will be 'WEBVIEW_{number}'
await driver.getContexts()
// Returns [ 'NATIVE_APP', 'WEBVIEW_84392.1', ... ]
})
detailed.test.js
it('should return all contexts in the current session with detailed info.', async () => {
// For Android
await driver.getContexts({returnDetailedContexts: true})
// Returns [
// { id: 'NATIVE_APP' },
// {
// id: 'WEBVIEW_com.wdiodemoapp',
// title: 'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO',
// url: 'https://webdriver.io/',
// packageName: 'com.wdiodemoapp',
// webviewPageId: '58B0AA2DBBBBBE9008C35AE42385BB0D'
// },
// {
// id: 'WEBVIEW_chrome',
// title: 'Android | Get more done with Google on Android-phones and devices',
// url: 'https://www.android.com/',
// packageName: 'com.android.chrome',
// webviewPageId: '0'
// }
// ]
//
// For iOS, the context will be 'WEBVIEW_{number}'
await driver.getContexts({returnDetailedContexts: true})
// Returns: [
// { id: 'NATIVE_APP' },
// {
// id: 'WEBVIEW_86150.1',
// title: 'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO',
// url: 'https://webdriver.io/',
// bundleId: 'org.reactjs.native.example.wdiodemoapp'
// },
// {
// id: 'WEBVIEW_86152.1',
// title: 'Apple',
// url: 'https://www.apple.com/',
// bundleId: 'com.apple.mobilesafari'
// }
// ]
})
description.data.test.js
it('should return Android description data for the webview', async () => {
// For Android
await driver.getContexts({returnDetailedContexts: true, returnAndroidDescriptionData: true})
// Returns [
// { id: 'NATIVE_APP' },
// {
// androidWebviewData: {
// // Indicates whether the web page is currently attached to a web view.
// // `true` means the page is attached and likely active, `false` indicates it is not.
// attached: true,
// // Indicates whether the web page is empty or not. An empty page typically means that
// // there is no significant content loaded in it. `true` indicates the page is empty,
// // `false` indicates it has content.
// empty: false,
// // Indicates whether the page has never been attached to a web view. If `true`, the
// // page has never been attached, which could indicate a new or unused page. If `false`,
// // the page has been attached at some point.
// neverAttached: false,
// // Indicates whether the web page is visible on the screen. `true` means the page is
// // visible to the user, `false` means it is not.
// visible: true,
// // This data can be super useful to determine where on the screen the webview is located
// // and can come in handy when you want to interact with elements on the screen based on
// // coordinates based on the top-left corner of the screen
// screenX: 0,
// screenY: 151,
// height: 2589,
// width: 1344
// },
// id: 'WEBVIEW_com.wdiodemoapp',
// title: 'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO',
// url: 'https://webdriver.io/',
// packageName: 'com.wdiodemoapp',
// webviewPageId: '58B0AA2DBBBBBE9008C35AE42385BB0D'
// }
// ]
})