Skip to content

Commit 9d72231

Browse files
Mouse and keyboard actions (#55)
* added mouse and keyboard actions steps
1 parent 036f34f commit 9d72231

File tree

11 files changed

+196
-3
lines changed

11 files changed

+196
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to the "@qavajs/steps-playwright" will be documented in this
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7+
## [0.26.0]
8+
- :rocket: added mouse and keyboard actions steps
9+
710
## [0.25.0]
811
- :rocket: added enabled/disabled validation
912
- :beetle: fixed issue with default timeouts

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ require('./lib/localSessionStorage.js');
1111
require('./lib/mock.js');
1212
require('./lib/poDefine.js');
1313
require('./lib/multiBrowser.js');
14+
require('./lib/mouseActions.js');
15+
require('./lib/keyboardActions.js');

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.25.0",
3+
"version": "0.26.0",
44
"description": "steps to interact with playwright",
55
"main": "./index.js",
66
"scripts": {

src/keyboardActions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { When } from '@cucumber/cucumber';
2+
3+
/**
4+
* Press and hold keyboard key
5+
* @param {string} key - key to press
6+
* @example When I hold down 'Q' key
7+
*/
8+
When('I hold down {string} key', async function (key) {
9+
await page.keyboard.down(key);
10+
});
11+
12+
/**
13+
* Release keyboard key
14+
* @param {string} key - key to release
15+
* @example When I release 'A' key
16+
*/
17+
When('I release {string} key', async function (key) {
18+
await page.keyboard.up(key);
19+
});

src/mouseActions.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { When } from '@cucumber/cucumber';
2+
import { getValue } from './transformers';
3+
import { parseCoords } from './utils/utils';
4+
5+
/**
6+
* Press mouse key
7+
* @param {string} button - button to press (left, right, middle)
8+
* @example When I press left mouse button
9+
*/
10+
When('I press {playwrightMouseButton} mouse button', async function (button) {
11+
await page.mouse.down({ button });
12+
});
13+
14+
/**
15+
* Release mouse key
16+
* @param {string} button - button to release (left, right, middle)
17+
* @example When I release left mouse button
18+
*/
19+
When('I release {playwrightMouseButton} mouse button', async function (button) {
20+
await page.mouse.up({ button });
21+
});
22+
23+
/**
24+
* Move mouse to coordinates
25+
* @param {string} coords - x, y coordinates to move
26+
* @example When I move mouse to '10, 15'
27+
*/
28+
When('I move mouse to {string}', async function (coords){
29+
const [x, y] = parseCoords(await getValue(coords));
30+
await page.mouse.move(x, y);
31+
});
32+
33+
/**
34+
* Scroll mouse wheel by x, y offset
35+
* @param {string} coords - x, y offset to scroll
36+
* @example When I scroll mouse wheel by '0, 15'
37+
*/
38+
When('I scroll mouse wheel by {string}', async function (offset) {
39+
const [x, y] = parseCoords(await getValue(offset));
40+
await page.mouse.wheel(x, y);
41+
});

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ defineParameterType({
3434
transformer: p => p ? parseInt(p) : null,
3535
useForSnippets: false
3636
});
37+
38+
defineParameterType({
39+
name: 'playwrightMouseButton',
40+
regexp: /(left|right|middle)/,
41+
transformer: p => p,
42+
useForSnippets: false
43+
});

test-e2e/apps/actions.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<div>
5252
<pre id="keywordevent"></pre>
5353
</div>
54+
55+
<div id="mouseEvent" style="height: 200px; width: 200px; background-color: #0f9e9e"></div>
56+
<input id="keyboardEvent" style="height: 200px; width: 200px; background-color: #0f9e9e"></input>
57+
5458
<script>
5559
const action = document.querySelector('#action');
5660
const button = document.querySelector('#button');
@@ -64,6 +68,9 @@
6468
const confirm = document.querySelector('#confirm');
6569
const prompt = document.querySelector('#prompt');
6670
const keywordEvent = document.querySelector('#keywordevent');
71+
const mouseEvent = document.querySelector('#mouseEvent');
72+
const keyboardEvent = document.querySelector('#keyboardEvent');
73+
6774
let presses = 0;
6875

6976
button.addEventListener('click', function () {
@@ -128,6 +135,70 @@
128135
setTimeout(()=> action.innerText = window.prompt('Are you robot?'), 500);
129136
});
130137

138+
mouseEvent.addEventListener('mousedown', function (event) {
139+
mouseEvent.innerText = JSON.stringify({
140+
type: event.type,
141+
screenX: event.screenX,
142+
screenY: event.screenY,
143+
buttons: event.buttons
144+
});
145+
event.preventDefault();
146+
});
147+
148+
mouseEvent.addEventListener('mouseup', function (event) {
149+
mouseEvent.innerText = JSON.stringify({
150+
type: event.type,
151+
screenX: event.screenX,
152+
screenY: event.screenY,
153+
buttons: event.buttons
154+
});
155+
event.preventDefault();
156+
});
157+
158+
mouseEvent.addEventListener('contextmenu', function (event) {
159+
event.preventDefault();
160+
});
161+
162+
mouseEvent.addEventListener('wheel', function (event) {
163+
console.log(event)
164+
mouseEvent.innerText = JSON.stringify({
165+
type: event.type,
166+
screenX: event.screenX,
167+
screenY: event.screenY,
168+
buttons: event.buttons,
169+
deltaY: event.deltaY
170+
});
171+
event.preventDefault();
172+
});
173+
174+
mouseEvent.addEventListener('mousemove', function (event) {
175+
mouseEvent.innerText = JSON.stringify({
176+
type: event.type,
177+
screenX: event.screenX,
178+
screenY: event.screenY,
179+
buttons: event.buttons
180+
});
181+
event.preventDefault();
182+
});
183+
184+
keyboardEvent.addEventListener('keydown', function (event) {
185+
console.log(event)
186+
keyboardEvent.value = JSON.stringify({
187+
type: event.type,
188+
code: event.code
189+
});
190+
event.preventDefault();
191+
});
192+
193+
keyboardEvent.addEventListener('keyup', function (event) {
194+
console.log(event)
195+
keyboardEvent.value = JSON.stringify({
196+
type: event.type,
197+
code: event.code
198+
});
199+
event.preventDefault();
200+
});
201+
131202
</script>
132203
</body>
133204
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Feature: keyboard actions
2+
3+
Background:
4+
When I open '$actionsPage' url
5+
And I click 'Keyboard Event Handler'
6+
7+
Scenario: key press and release
8+
When I hold down 'Q' key
9+
Then I expect 'value' property of 'Keyboard Event Handler' to contain '"code":"KeyQ"'
10+
Then I expect 'value' property of 'Keyboard Event Handler' to contain '"type":"keydown"'
11+
When I release 'Q' key
12+
Then I expect 'value' property of 'Keyboard Event Handler' to contain '"code":"KeyQ"'
13+
Then I expect 'value' property of 'Keyboard Event Handler' to contain '"type":"keyup"'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Feature: mouse actions
2+
3+
Background:
4+
When I open '$actionsPage' url
5+
And I hover over 'Event Handler'
6+
7+
Scenario Outline: mouse press and release
8+
Then I press <button> mouse button
9+
And I expect text of 'Event Handler' to contain '"buttons":<buttonCode>'
10+
And I expect text of 'Event Handler' to contain '"type":"mousedown"'
11+
Then I release <button> mouse button
12+
And I expect text of 'Event Handler' to contain '"buttons":0'
13+
And I expect text of 'Event Handler' to contain '"type":"mouseup"'
14+
15+
Examples:
16+
| button | buttonCode |
17+
| left | 1 |
18+
| right | 2 |
19+
| middle | 4 |
20+
21+
Scenario: mouse move
22+
Then I move mouse to '10, 10'
23+
And I expect text of 'Event Handler' to contain '"type":"mousemove"'
24+
25+
Scenario Outline: mouse wheel
26+
Then I scroll mouse wheel by '<offset>'
27+
And I expect text of 'Event Handler' to contain '"type":"wheel"'
28+
And I expect text of 'Event Handler' to match '<deltaY>'
29+
30+
Examples:
31+
| offset | deltaY |
32+
| 0, 100 | .+"deltaY":\d+.+ |
33+
| 0, -100 | .+"deltaY":-\d+.+ |
34+

0 commit comments

Comments
 (0)