概要

Appiumは、iOSモバイル、Androidモバイル、Windowsデスクトッププラットフォーム上のネイティブ、モバイルWeb、ハイブリッドアプリケーションを自動化するためのオープンソースツールです。このガイドでは、主にモバイルデバイスでNightwatchテストを実行することに焦点を当てています。

動作原理

Appiumは、ベンダー(UIAutomator2/Espresso、Apple XCUITest/UIAutomation)の自動化ライブラリをラップするWebDriverプロトコルに準拠したREST APIを公開するNode JSベースのサーバーです。

エンドツーエンドのシナリオでは、NightwatchはAppiumサーバーにリクエストを行い、サーバーは異なるプラットフォームドライバを使用してネイティブフレームワークと通信してコマンドを実行し、最後にHTTPレスポンスをNightwatchに返します。

appium-nightwatch

Appiumのインストール

最初のステップは、Appiumをダウンロードしてセットアップすることです。

構成

Appiumサーバーに対してローカルで実行されているモバイルデバイスでテストを実行するようにNightwatchに構成を追加できます

nightwatch.conf.js
appium_ios: {
  selenium: {
    host: 'localhost',
    port: 4723
  },
  disable_error_log: true,
  webdriver: {
    timeout_options: {
      timeout: 150000,
      retry_attempts: 3
    },
    keep_alive: false,
    start_process: false
  },
  desiredCapabilities: {
    browserName: 'Safari', //not required incase using app
    javascriptEnabled: true,
    platformName: 'iOS', //android or iOS
    platformVersion: '15.0',
    deviceName: 'iPhone 13'
    // "app": APP_PATH + "ios/PieDrive.app", // path for the ios app you want to test
  }
}

ケイパビリティの詳細については、Appiumドキュメントを参照してください

基本的なテストを書く

これは、ライクス美術館のウェブサイトで「Night Watch」という用語を検索するデモテストです。

tests/sampleTest.js
describe('Nightwatch Website tests', function() {
    
it('Searching the Rijksmuseum ', async function(){ browser.navigateTo('https://www.rijksmuseum.nl/en'); const cookieDialogVisible = await browser.isVisible({ selector: '.cookie-consent-bar-wrap', suppressNotFoundErrors: true });
if (cookieDialogVisible) { browser.click('.cookie-consent-bar-wrap button.link'); } browser.pause(1000).click('a[aria-label="Search"]');
return browser.setValue('input.search-bar-input[type=text]', ['night watch']) .click('button.button.search-bar-button') .pause(1000) .assert.containsText('.search-results', 'The Night Watch, Rembrandt van Rijn, 1642'); }); });

テストを実行するには、コマンドappiumを使用してローカルでAppiumサーバーを実行し、appium_ios環境に対してテストを実行します。

ジェスチャーの使用

ジェスチャーは、モバイルデバイスと対話する際に広く使用されています。モバイルデバイスでジェスチャーを生成するには2つの方法があります。

1)Appiumの非標準APIを使用する

これらのAPIはプラットフォーム固有です。これについてはAppiumドキュメントで詳しく参照できます。iOSデバイスでスワイプジェスチャーを生成するコマンドは次のようになります。

browser.execute('mobile: swipe', args);
2)Actions APIを使用する

Actions APIは非常に一般的で、プラットフォームに依存しません。入力ソース(キー、ポインター、ホイール)の概念に依存しています。次のコードは、Actions APIを使用してスワイプとピンチズームジェスチャーを生成します。

tests/sampleTest.js
describe('W3C Actions API', function() {
  it('swipe down and zoom in the page - w3c actions api ', async function(){
    //Scroll down the page
    await  browser.perform(function(){
      const actions = this.actions();
      
return actions.move({x: 100, y: 100}).press().move({origin: 'pointer', y: -300, duration: 50}).release(); });
await browser.pause(2000);
//Pinch zoom await browser.perform(function(){ const actions= this.actions(); const pointer1 = new Device('finger-1', 'touch'); const pointer2 = new Device('finger-2', 'touch'); actions.insert(pointer1, pointer1.move({duration: 0, x: 100, y: 70}), pointer1.press(), {type: 'pause', duration: 500}, pointer1.move({duration: 1000, origin: 'pointer', x: 0, y: -20}), pointer1.release()); actions.insert(pointer2, pointer2.move({duration: 0, x: 100, y: 100}), pointer2.press(), {type: 'pause', duration: 500}, pointer2.move({duration: 1000, origin: 'pointer', x: 0, y: 20}), pointer2.release());
return actions; }); }); });