Implement Page.uploadFile (#61)

This patch implements `Page.uploadFile` method to support file upload inputs.
This commit is contained in:
JoelEinbinder
2017-07-10 11:21:46 -07:00
committed by Andrey Lushnikov
parent 739c1566a9
commit da0cde1b45
8 changed files with 75 additions and 8 deletions

View File

@@ -544,6 +544,15 @@ describe('Puppeteer', function() {
await page.click('button');
expect(await page.evaluate(() => result)).toBe('Clicked');
}));
it('should fail to click a missing button', SX(async function() {
await page.navigate(STATIC_PREFIX + '/input/button.html');
try {
await page.click('button.does-not-exist');
fail('Clicking the button did not throw.');
} catch (error) {
expect(error.message).toBe('No node found for selector: button.does-not-exist');
}
}));
it('should type into the textarea', SX(async function() {
await page.navigate(STATIC_PREFIX + '/input/textarea.html');
await page.focus('textarea');
@@ -557,6 +566,21 @@ describe('Puppeteer', function() {
await page.click('button');
expect(await page.evaluate(() => result)).toBe('Clicked');
}));
it('should upload the file', SX(async function(){
await page.navigate(STATIC_PREFIX + '/input/fileupload.html');
await page.uploadFile('input', __dirname + '/assets/file-to-upload.txt');
expect(await page.evaluate(() => {
let input = document.querySelector('input');
return input.files[0].name;
})).toBe('file-to-upload.txt');
expect(await page.evaluate(() => {
let input = document.querySelector('input');
let reader = new FileReader();
let promise = new Promise(fulfill => reader.onload = fulfill);
reader.readAsText(input.files[0]);
return promise.then(() => reader.result);
})).toBe('contents of the file');
}));
});
describe('Page.setUserAgent', function() {
it('should work', SX(async function() {