1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nightwatch.js検証

Posted at

Nightwatch.jsを試す機会があったため、ドキュメントではわからない、実際に使ってみて気づいた点をまとめている。

開発環境構築

  • Getting Started - Installationに従えば、インストールは問題なくできる。

  • Windows環境ではChrome Driverを使ってテスト実行すると以下のエラーが出力された。

    An error occurred while trying to start ChromeDriver: cannot resolve path: "node_modules/.bin/chromedriver".
    
  • これはChrome Driverのパス設定の問題なのでnightwatch.jsonを変更すればよい。(参考

    "server_path": "node_modules/chromedriver/lib/chromedriver/chromedriver.exe"
    

テスト実装

  • テストランナーにはMocha、アサーションにはchaiが使えるため、取っつきやすい。

  • テストコードの構造化には、Page Objectsパターンや、事前・事後処理を定義するGlobal Hooksが使える。また、テストファイルのグループ化(Test Groups)もできる。

  • TypeScriptを使うこともできるが、公式にサポートされてはいないので使いにくいところがある。

    • Nightwatch.jsはテストファイルの拡張子がjs前提になっているため、ts-nodeでは実行できず、tscでトランスパイルしたものを読み込む形にする必要がある。
    • 型定義(@types/nightwatch)の最新版は0.9.12と少し古い。(Nightwatch.jsの最新安定版は1.0.19)
    • Page Objectはオブジェクトとして定義するため、型(EnhancedPageObject)に合わせられない。
    • NightwatchBrowserオブジェクトから参照できるPage Objectの型もEnhancedPageObject型になるため自分で定義したメソッドを使うためには、キャストが必要になる。
  • async/awaitはv1.1(beta)以降で使えるとのことだが、v1.1.8時点ではPromiseを返すメソッドと返さないメソッドが混じっている。また、Page Objectはasync/await未対応。

    browser.url('http://www.google.com')) // Promise
    browser.click('input[name=btnK]') // 同期的
    browser.pause(500) // Promise
    browser.waitForElementVisible('input[name=btnK]') // 同期的
    
  • Visual Studio Codeでのデバッグ実行はnodeコマンドにinspectオプションを指定すれば簡単にできる。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?