Skip to content

Commit 7e09b90

Browse files
authored
save file step (#132)
1 parent 7e3a61b commit 7e09b90

File tree

10 files changed

+74
-30
lines changed

10 files changed

+74
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ node_modules/
22
lib/
33
coverage/
44
test-e2e/report.*
5+
test-e2e/downloads/
56
.idea/
67
traces/
78
video/

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1010
:pencil: - chore
1111
:microscope: - experimental
1212

13+
## [0.54.0]
14+
- :rocket: added _I save file to {string} by clicking {string}_ step
15+
1316
## [0.53.0]
1417
- :rocket: added _I grant {string} permission_ step
1518
- :rocket: added _I revoke browser permissions_ step

package-lock.json

Lines changed: 35 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-playwright",
3-
"version": "0.53.0",
3+
"version": "0.54.0",
44
"description": "steps to interact with playwright",
55
"main": "./index.js",
66
"scripts": {
@@ -26,11 +26,11 @@
2626
},
2727
"homepage": "https://github.com/qavajs/steps-playwright#readme",
2828
"devDependencies": {
29-
"@cucumber/cucumber": "^11.0.0",
29+
"@cucumber/cucumber": "^11.0.1",
3030
"@qavajs/cli": "^0.40.0",
31-
"@qavajs/console-formatter": "^0.7.2",
31+
"@qavajs/console-formatter": "^0.8.0",
3232
"@qavajs/html-formatter": "^0.18.1",
33-
"@qavajs/memory": "^1.8.0",
33+
"@qavajs/memory": "^1.9.0",
3434
"@qavajs/webstorm-adapter": "^8.0.0",
3535
"@types/chai": "^4.3.17",
3636
"@types/express": "^4.17.21",
@@ -46,6 +46,6 @@
4646
},
4747
"dependencies": {
4848
"@playwright/test": "^1.47.0",
49-
"@qavajs/po-playwright": "^0.16.0"
49+
"@qavajs/po-playwright": "^0.16.1"
5050
}
5151
}

src/actions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ When('I scroll in {string} until {string} to be visible', async function (scroll
325325
}
326326
});
327327

328+
/**
329+
* Save a file to relative path
330+
* @param {string} pathAlias - file path
331+
* @param {string} initiatorAlias - alias of an element triggering downloading process
332+
* @example I save file to './folder/file.pdf' by clicking 'Download Button'
333+
*/
334+
When('I save file to {string} by clicking {string}', async function (pathAlias: string, initiatorAlias: string) {
335+
const downloadPromise = page.waitForEvent('download');
336+
const element = await getElement(await getValue(initiatorAlias));
337+
const path = await getValue(pathAlias);
338+
await element.click();
339+
const download = await downloadPromise;
340+
await download.saveAs(path);
341+
});
342+
328343
/**
329344
* Provide file url to upload input
330345
* @param {string} alias - element to upload file

test-e2e/apps/actions.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<div id="mouseEvent" style="height: 200px; width: 200px; background-color: #0f9e9e"></div>
7878
<input id="keyboardEvent" style="height: 200px; width: 200px; background-color: #0f9e9e"></input>
7979
<button id="location" onclick="getLocation()">No location</button>
80+
<a id="download" href="data:text/plain;charset=utf-8,Hello%20World!" download="hello.txt">Click to download</a>
8081
<script>
8182
const locationButton = document.getElementById("location");
8283
function getLocation() {

test-e2e/features/actions.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,7 @@ Feature: actions
257257
When I refresh page
258258
When I click 'Location Button'
259259
When I expect text of 'Location Button' to equal 'No location'
260+
261+
Scenario: save file
262+
When I save file to '$downloadPath' by clicking 'Download Button'
263+
When I expect file '$downloadPath' to exist

test-e2e/memory/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ export default class Memory {
4141
location = 'geolocation';
4242

4343
canada = { latitude: 62.39, longitude: -96.81};
44+
45+
downloadPath = './test-e2e/downloads/text.txt';
4446
}

test-e2e/page_object/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default class App {
6161
FetchButton = $('#fetchButton');
6262
FetchResult = $('#fetchResult');
6363
LocationButton = $('#location');
64+
DownloadButton = $('#download');
6465

6566
// Electron
6667
OpenNewWindowElectronButton = $('#electronButton');

test-e2e/step-definitions/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Then } from '@cucumber/cucumber';
22
import memory from '@qavajs/memory';
33
import { Page, expect } from '@playwright/test';
4-
import {getElement} from "../../src/transformers";
5-
import {getValidation} from "@qavajs/validation";
4+
import {getValue} from "../../src/transformers";
5+
import * as fs from "fs";
66

77
declare global {
88
var page: Page;
@@ -41,3 +41,8 @@ Then('I set {int} ms delayed mock for {string} request', async function (delay:
4141
}), delay);
4242
});
4343
})
44+
45+
Then('I expect file {string} to exist', async function (path: string){
46+
const filePresence = fs.existsSync(await getValue(path));
47+
expect(filePresence).toBeTruthy();
48+
});

0 commit comments

Comments
 (0)