Skip to content

Commit 7f754b9

Browse files
committed
add alert steps
1 parent 3fc68b7 commit 7f754b9

File tree

10 files changed

+132
-5
lines changed

10 files changed

+132
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules/
22
lib/
33
coverage/
4-
test-e2e/report.xml
4+
test-e2e/report.*
5+
.idea/
6+
customDir/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.13
2+
- :rocket: added JS alert steps
3+
- :rocket: added _I press button given number of times_ step
4+
15
## 0.0.12
26
- :rocket: added _I upload file_ step
37

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-playwright",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "steps to interact with playwright",
55
"main": "./index.js",
66
"scripts": {

src/actions.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ When('I press {string} key', async function (key: string) {
121121
await page.press('body', key);
122122
});
123123

124+
/**
125+
* Press button given number of times
126+
* @param {string} key - key to press
127+
* @param {number} num - number of times
128+
* @example I press 'Enter' key 5 times
129+
*/
130+
When('I press {string} key {int} time(s)', async function (key: string, num: number) {
131+
for (let i: number = 0; i < num; i++) {
132+
await page.keyboard.press(key);
133+
}
134+
});
135+
124136
/**
125137
* Hover over element
126138
* @param {string} alias - element to hover over
@@ -197,7 +209,6 @@ When('I scroll by {string} in {string}', async function (offset: string, alias:
197209
}, coords);
198210
});
199211

200-
201212
/**
202213
* Provide file url to upload input
203214
* @param {string} alias - element to upload file
@@ -209,3 +220,37 @@ When('I upload {string} file to {string}', async function (value: string, alias:
209220
const filePath = await getValue(value);
210221
await element.setInputFiles(filePath);
211222
});
223+
224+
/**
225+
* Accept alert
226+
* @example I accept alert
227+
*/
228+
When('I accept alert', async function () {
229+
await new Promise<void>((resolve)=> page.once('dialog', async (dialog) => {
230+
await dialog.accept();
231+
resolve();
232+
}))
233+
});
234+
235+
/**
236+
* Dismiss alert
237+
* Playwright automatically dismisses all dialogs. This step is just to make it implicitly.
238+
* @example I dismiss alert
239+
*/
240+
When('I dismiss alert', async function () {
241+
await new Promise<void>((resolve)=> page.once('dialog', async (dialog) => {
242+
await dialog.dismiss();
243+
resolve();
244+
}));
245+
});
246+
247+
/**
248+
* I type {string} to alert
249+
* @example I type 'coffee' to alert
250+
*/
251+
When('I type {string} to alert', async function (value: string) {
252+
await new Promise<void>((resolve)=> page.once('dialog', async (dialog) => {
253+
await dialog.accept(value);
254+
resolve();
255+
}))
256+
});

src/validations.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,17 @@ Then(
215215
}
216216
);
217217

218+
/**
219+
* Verify that text of an alert meets expectation
220+
* @param {string} validationType - validation
221+
* @param {string} value - expected text value
222+
* @example I expect text of alert does not contain 'coffee'
223+
*/
224+
Then('I expect text of alert {playwrightValidation} {string}', async function (validationType: string, expectedValue: string) {
225+
const alertText = await new Promise<string>(resolve => page.once('dialog', async (dialog) => {
226+
resolve(dialog.message());
227+
}));
228+
const validation = getValidation(validationType);
229+
validation(alertText, expectedValue);
230+
}
231+
);

src/waits.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { When } from '@cucumber/cucumber';
22
import { getValue, getElement, getValueWait, getConditionWait } from './transformers';
3+
import { getValidation } from '@qavajs/validation';
34

45
/**
56
* Wait for element condition
@@ -142,5 +143,21 @@ When(
142143
}
143144
);
144145

146+
/**
147+
* Verify that text of an alert meets expectation
148+
* @param {string} validationType - validation
149+
* @param {string} value - expected text value
150+
* @example I expect text of alert does not contain 'coffee'
151+
*/
152+
When('I wait until text of alert {playwrightValidation} {string}', async function (validationType: string, expectedValue: string) {
153+
let alertText;
154+
page.once('dialog', async (dialog) => {
155+
alertText = dialog.message();
156+
});
157+
const validation = getValidation(validationType);
158+
validation(alertText, expectedValue);
159+
}
160+
);
161+
145162

146163

test-e2e/apps/actions.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<button class="button">Button1</button>
1414
<button class="button">Button2</button>
1515
<button class="button">Button3</button>
16+
<button id="confirm">Show alert in 3 sec</button>
17+
<button id="prompt">Show prompt</button>
1618

1719
<iframe src="frame.html" style="border: 1px solid black"></iframe>
1820
<a id="newTabLink" href="frame.html" target="_blank">New tab</a>
@@ -56,6 +58,8 @@
5658
const body = document.body;
5759
const select = document.querySelector('#select');
5860
const pressCounter = document.querySelector('#pressCounter');
61+
const confirm = document.querySelector('#confirm');
62+
const prompt = document.querySelector('#prompt');
5963
let presses = 0;
6064

6165
button.addEventListener('click', function () {
@@ -101,6 +105,15 @@
101105
action.innerText = select.value;
102106
});
103107

108+
confirm.addEventListener('click', function () {
109+
setTimeout(()=> action.innerText = window.confirm('Are you robot?'), 3000);
110+
});
111+
112+
prompt.addEventListener('click', () => {
113+
setTimeout(()=> action.innerText = window.prompt('Are you robot?'), 500);
114+
});
115+
116+
104117
</script>
105118
</body>
106119
</html>

test-e2e/features/actions.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ Feature: actions
5555
When I press 'w' key
5656
Then I expect text of 'Action' to be equal 'keypress'
5757

58+
Scenario Outline: press <Key> key multiple times
59+
When I press '<Key>' key <Times> time<Postfix>
60+
Then I expect text of 'Press Counter' to be equal '<Result>'
61+
62+
Examples:
63+
| Key | Times | Postfix | Result |
64+
| Enter | 1 | | pressed Enter 1 times |
65+
| Space | 5 | s | pressed Space 5 times |
66+
5867
Scenario: hover
5968
When I hover over 'Button Hover'
6069
Then I expect text of 'Action' to be equal 'hover'
@@ -93,3 +102,23 @@ Feature: actions
93102
When I upload '$uploadFile' file to 'File Input'
94103
Then I expect text of 'Action' to be equal 'file:C:\fakepath\actions.html'
95104

105+
Scenario: accept alert
106+
When I click "Alert Button"
107+
And I accept alert
108+
Then I expect text of 'Action' to be equal 'true'
109+
110+
Scenario: dismiss alert
111+
When I click "Alert Button"
112+
And I dismiss alert
113+
Then I expect text of 'Action' to be equal 'false'
114+
115+
@debug
116+
Scenario: type text to alert
117+
When I expect text of 'Action' to be equal 'Nothing'
118+
And I click "Prompt Button"
119+
And I type 'I am not a robot' to alert
120+
Then I expect text of 'Action' to be equal 'I am not a robot'
121+
122+
Scenario: expect text of alert
123+
When I click "Prompt Button"
124+
Then I expect text of alert to be equal 'Are you robot?'

test-e2e/page_object/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export default class App {
55
SimpleTextInput = $('#textInput');
66
FileInput = $('#fileInput');
77
Action = $('#action');
8+
AlertButton = $('#confirm');
9+
PromptButton = $('#prompt');
810
Button = $('#button');
911
ButtonHover = $('#buttonHover');
1012
Input = $('#input');
@@ -23,6 +25,7 @@ export default class App {
2325
Loading = $('#loading');
2426
LoadingInput = $('#loadingInput');
2527
WaitCollection = $$('#waitCollection > div');
28+
PressCounter = $('#pressCounter');
2629

2730
Users = $$('#users > li');
2831
OverflowContainer = $('#overflowContainer');

0 commit comments

Comments
 (0)