Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
Add Page#uploadFile (#541)
Browse files Browse the repository at this point in the history
* Adds Page#uploadFile
  • Loading branch information
jtillmann authored and amir20 committed Aug 31, 2016
1 parent 0c884c9 commit 7553c7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ page.invokeMethod('evaluate', function(selector) {
});
```

### `page#uploadFile`

A file can be inserted into file input fields using the `#uploadFile(selector, file)` method.

```js
page.uploadFile('#selector', '/path/to/file').then(function() {

});
```


## Tests

Expand Down
3 changes: 2 additions & 1 deletion src/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ const methods = [
'stop',
'switchToFrame',
'switchToMainFrame',
'goBack'
'goBack',
'uploadFile'
];

asyncMethods.forEach(method => {
Expand Down
13 changes: 13 additions & 0 deletions src/spec/page_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('Page', () => {
response.end('window.fooBar = 2;');
} else if (request.url === '/test.html') {
response.end('<html><head><title>Page Title</title></head><body>Test</body></html>');
} else if (request.url === '/upload.html') {
response.end('<html><head><title>Page Title</title></head><body><input type="file" id="upload" /></body></html>');
} else {
response.end('hi, ' + request.url);
}
Expand Down Expand Up @@ -631,4 +633,15 @@ describe('Page', () => {
return page.goBack();
});
});

it('#uploadFile() inserts file into file input field', function*() {
let page = yield phantom.createPage();
yield page.open('http://localhost:8888/upload.html');
yield page.uploadFile('#upload', process.env.PWD + '/package.json');
let response = yield page.evaluate(function () {
return document.querySelector("#upload").files[0].name;
});
expect(response).toEqual('package.json');
});

});

0 comments on commit 7553c7e

Please sign in to comment.