Skip to content

adding downloadFile action for puppeteer #1542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/helpers/Puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ I.doubleClick('.btn.edit');
- `locator` clickable link or button located by text, or any element located by CSS|XPath|strict locator.
- `context` (optional) element to search in CSS|XPath|Strict locator.

### downloadFile

Performs a download file on an element matched by link|button|CSS or XPath.
File is downloaded by default to output folder.
If no custom file name is provided, the default name will be used

```js
I.downloadFile('td[class="text-right file-link"] a', 'thisIsCustomName');
```

#### Parameters

- `locator` clickable link or button located by CSS|XPath locator.
- `string` custom file name.

### dragAndDrop

Drag an item to a destination element.
Expand Down
11 changes: 11 additions & 0 deletions docs/webapi/downloadFile.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Performs a download file on an element matched by link|button|CSS or XPath.
File is downloaded by default to output folder.
If no custom file name is provided, the default name will be used


```js
I.downloadFile('td[class="text-right file-link"] a', 'thisIsCustomName');
```

@param locator clickable link or button located by CSS|XPath locator.
@param string custom file name.
4 changes: 2 additions & 2 deletions lib/command/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ module.exports = function (genPath, options) {

function addAllMethodsInObject(supportObj, actions, methods, translations) {
for (const action of methodsOfObject(supportObj)) {
let fn = supportObj[action];
const fn = supportObj[action];
if (!fn.name) {
Object.defineProperty(fn, "name", {value: action});
Object.defineProperty(fn, 'name', { value: action });
}
const actionAlias = translations ? translations.actionAliasFor(action) : action;
if (!actions[actionAlias]) {
Expand Down
51 changes: 51 additions & 0 deletions lib/helper/Puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const ElementNotFound = require('./errors/ElementNotFound');
const RemoteBrowserConnectionRefused = require('./errors/RemoteBrowserConnectionRefused');
const Popup = require('./extras/Popup');
const Console = require('./extras/Console');
const axios = require('axios');
const fs = require('fs');


let puppeteer;
Expand Down Expand Up @@ -851,6 +853,55 @@ class Puppeteer extends Helper {
return proceedClick.call(this, locator, context, { waitForNavigation: true });
}

/**
* {{> ../webapi/downloadFile }}
*/
async downloadFile(locator, customName) {
let fileName;
await this.page.setRequestInterception(true);
this.click(locator);

const xRequest = await new Promise((resolve) => {
this.page.on('request', (request) => {
const grabbedFileName = request.url().split('/')[request.url().split('/').length - 1];
const fileExtension = request.url().split('/')[request.url().split('/').length - 1].split('.')[1];
customName ? fileName = `${customName}.${fileExtension}` : fileName = grabbedFileName;
request.abort();
resolve(request);
});
});

const options = {
encoding: null,
method: xRequest._method,
uri: xRequest._url,
body: xRequest._postData,
headers: xRequest._headers,
};

const cookies = await this.page.cookies();
options.headers.Cookie = cookies.map(ck => `${ck.name}=${ck.value}`).join(';');

const response = await axios({
method: options.method, url: options.uri, headers: options.headers, responseType: 'arraybuffer',
});

const outputFile = path.join(`${global.output_dir}/${fileName}`);

try {
return new Promise((resolve, reject) => {
const wstream = fs.createWriteStream(outputFile);
wstream.write(response.data);
wstream.end();
this.debug(`File is downloaded in ${outputFile}`);
wstream.on('finish', () => { resolve(fileName); });
wstream.on('error', reject);
});
} catch (error) {
throw new Error(`There is something wrong with downloaded file. ${error}`);
}
}

/**
* {{> ../webapi/doubleClick }}
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function getParams(fn) {
});
return params;
} catch (err) {
console.log('Error in ' + newFn.toString());
console.log(`Error in ${newFn.toString()}`);
console.error(err);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const isGenerator = module.exports.isGenerator = function (fn) {
};

const isFunction = module.exports.isFunction = function (fn) {
return typeof fn === 'function'
return typeof fn === 'function';
};

const isAsyncFunction = module.exports.isAsyncFunction = function (fn) {
Expand Down
47 changes: 47 additions & 0 deletions test/helper/Puppeteer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ const AssertionFailedError = require('../../lib/assert/error');
const formContents = require('../../lib/utils').test.submittedData(path.join(__dirname, '/../data/app/db'));
const expectError = require('../../lib/utils').test.expectError;
const webApiTests = require('./webapi');
const FileSystem = require('../../lib/helper/FileSystem');

let I;
let browser;
let page;
let FS;
const siteUrl = TestHelper.siteUrl();

describe('Puppeteer', function () {
Expand All @@ -22,6 +24,7 @@ describe('Puppeteer', function () {

before(() => {
global.codecept_dir = path.join(__dirname, '/../data');

I = new Puppeteer({
url: siteUrl,
windowSize: '500x700',
Expand Down Expand Up @@ -558,6 +561,50 @@ describe('Puppeteer', function () {
assert.notEqual(before, after);
});
});

describe('#downloadFile', () => {
before(() => {
// create download folder;
fs.mkdir(path.join(`${__dirname}/../data/download`), () => {

});
global.output_dir = path.join(`${__dirname}/../data/download`);

FS = new FileSystem();
FS._before();
FS.amInPath('download');
});

after(() => {
// Remove the test dir
fs.readdir(global.output_dir, (err, files) => {
if (err) throw err;

for (const file of files) {
if (file.includes('.mp4')) {
fs.unlink(path.join(global.output_dir, file), (err) => {
if (err) throw err;
});
}
}
});
});


it('should dowload file', async () => {
await I.amOnPage('http://file-examples.com/index.php/sample-video-files/sample-mp4-files/');
const fileName = await I.downloadFile('td[class="text-right file-link"] a');

FS.seeFile(fileName);
});

it('should dowload file with custom name', async () => {
await I.amOnPage('http://file-examples.com/index.php/sample-video-files/sample-mp4-files/');
const fileName = await I.downloadFile('td[class="text-right file-link"] a', 'thisisacustomname');

FS.seeFile(fileName);
});
});
});

let remoteBrowser;
Expand Down