To upload a file, you'll first need to create a /data directory, and add the file you'll want to upload.
tests/nightwatch/data/IM-0001-1001.dcm
Your form will need an input with type of file. (Some people don't like the styling options this input provides; and a common pattern is to make this input hidden; and to have another button on the page click it on behalf of the user.)
<form id="myform">
<input type="file" id="fileUpload">
<input type="text" name="first_name">
<input type="text" name="last_name">
<input type="date" name="dob_month">
<input type="date" name="dob_day">
<input type="date" name="dob_year">
<input type="radio" name="gender" value="M">
<input type="radio" name="gender" value="F">
<input type="radio" name="gender" value="O">
<input type="select" name="hs_graduation_year">
<input type="text" name="city">
<input type="select" name="state">
<input type="submit" name="submit" value="Submit">
</form>
Your tests will then need to use setValue() and resolve the path to the local file asset.
module.exports = {
"Upload Study" : function (client) {
console.log(require('path').resolve(__dirname + '/../data' ));
var stringArray = "Chicago";
client
.url(client.globals.url)
.verify.elementPresent("form#myform")
// input[type="file"]
.verify.elementPresent("input#fileUpload")
.setValue('input#fileUpload', require('path').resolve(__dirname + '/../data/IM-0001-1001.dcm'))
// input[type="text"]
.setValue('input[name="first_name"]', 'First')
.setValue('input[name="last_name"]', 'Last')
// input[type="date"]
.click('select[name="dob_month"] option[value="3"]')
.click('select[name="dob_day"] option[value="18"]')
.click('select[name="dob_year"] option[value="1987"]')
// input[type="radio"]
.click('input[name="gender"][value="M"]')
// input[type="number"]
.click('select[name="hs_graduation_year"] option[value="2002"]')
// input[type="text"]
// sometimes Nightwatch will send text faster than the browser can handle
// which will cause skipping of letters. In such cases, we need to slow
// Nightwatch down; which we do by splitting our input into an array
// and adding short 50ms pauses between each letter
for(var i=0; i < userIdArray.length; i++) {
client.setValue('input[name="city"]', stringArray[i]).pause(50)
}
// input[type="select"]
// after an array input above, we need to resume our method chain...
client.click('select[name="state"] option[value="CA"]')
// input[type="number"]
.setValue('input[name="zip"]', '01234')
//input [ type="submit" ]
.click('button[type="submit"]')
.end();
}
};
Credit to Daniel Rinehart for inpsiring this example.