【Nightwatch.js】ファイル選択ボタンで選択させる

【Nightwatch.js】ファイル選択ボタンで選択させる

ファイル選択ボタンで選択させる

普段ブラウザでの操作においてはファイルアップロードボタンからファイルを選択する時に、
ボタンを押下するとエクスプローラーが開いてファイルを指定するように誘導されます。
nightwatchではエクスプローラーの操作はできないので、直接値を渡して(setValue)あげます。

テスト対象画面

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>test fileSelect</title>
    </head>
    <body>
        <table width="100%" cellspacing="1" cellpadding="2" border="1">
            <tbody>
                <tr>
                    <td colspan="1">File select test</td>
                </tr>
                <tr>
                    <td>
                        <input id="file_button" type="file" />
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

テストコード

const path = require('path');

var url = path.resolve(__dirname,"fileSelectTest.html");
var file = path.resolve(__dirname,"../file/hoge.txt");

module.exports = {

  '@disabled': false,

  'file selection' : function (client) {
    client
      .url(url)
      .waitForElementPresent('body', 6000)
      .pause(3000)
      .useXpath()
      .setValue('//*[@id="file_button"]', file)
      .pause(3000)
      .end();
  }
};

nightwatch.json

特別な指定とかはありません。普通です。

{
	"src_folders": [
		"tests"
	],
	"output_folder": "reports",
	"webdriver": {
		"start_process": true,
		"server_path": "./lib/chromedriver_2.45",
		"cli_args": [
			"--verbose"
		],
		"port": 9515
	},
	"test_settings": {
		"default": {
			"desiredCapabilities": {
				"browserName": "chrome"
			}
		}
	}
}

参考

File Upload Testing in Nightwatch.js

nightwatch.jsカテゴリの最新記事