Skip to content

Commit 541f3a3

Browse files
Merge pull request #3 from qavajs/implemented-memory
added memory steps
2 parents b29001c + 9cd5d57 commit 541f3a3

File tree

9 files changed

+2905
-892
lines changed

9 files changed

+2905
-892
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ require('./lib/hooks.js');
33
require('./lib/actions.js');
44
require('./lib/waits.js');
55
require('./lib/validations.js');
6+
require('./lib/memory.js');

package-lock.json

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

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-playwright",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "steps to interact with playwright",
55
"main": "./index.js",
66
"scripts": {
@@ -21,14 +21,13 @@
2121
},
2222
"homepage": "https://github.com/qavajs/steps-playwright#readme",
2323
"devDependencies": {
24-
"@cucumber/cucumber": "^8.2.2",
24+
"@cucumber/cucumber": "^8.7.0",
2525
"@qavajs/cli": "^0.0.9",
26-
"@qavajs/po": "^1.0.2",
27-
"@qavajs/xunit-formatter": "^0.0.1",
26+
"@qavajs/xunit-formatter": "^0.0.2",
2827
"@types/chai": "^4.3.1",
29-
"@types/jest": "^28.1.4",
30-
"jest": "^28.1.2",
31-
"ts-jest": "^28.0.5",
28+
"@types/jest": "^29.2.0",
29+
"jest": "^29.2.1",
30+
"ts-jest": "^29.0.3",
3231
"ts-node": "^10.9.1"
3332
},
3433
"dependencies": {

src/hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Before(async function () {
3131
BeforeStep(async function () {
3232
if (config.screenshot === ScreenshotEvent.BEFORE_STEP) {
3333
try {
34-
this.attach(await page.screenshot(), 'base64:image/png');
34+
this.attach(await page.screenshot(), 'image/png');
3535
} catch (err) {
3636
console.warn(err)
3737
}
@@ -44,7 +44,7 @@ AfterStep(async function (step) {
4444
(config.screenshot === ScreenshotEvent.ON_FAIL && step.result.status === Status.FAILED) ||
4545
config.screenshot === ScreenshotEvent.AFTER_STEP
4646
) {
47-
this.attach(await page.screenshot(), 'base64:image/png');
47+
this.attach(await page.screenshot(), 'image/png');
4848
}
4949
} catch (err) {
5050
console.warn(err)

src/memory.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import memory from '@qavajs/memory';
2+
import { When } from '@cucumber/cucumber';
3+
import { getElement, getValue } from './transformers';
4+
5+
/**
6+
* Save text of element to memory
7+
* @param {string} alias - element to get value
8+
* @param {string} key - key to store value
9+
* @example I save text of '#1 of Search Results' as 'firstSearchResult'
10+
*/
11+
When('I save text of {string} as {string}', async function (alias, key) {
12+
const element = await getElement(alias);
13+
const value = await element.innerText();
14+
memory.setValue(key, value);
15+
});
16+
17+
/**
18+
* Save property of element to memory
19+
* @param {string} property - property to store
20+
* @param {string} alias - element to get value
21+
* @param {string} key - key to store value
22+
* @example I save 'checked' property of 'Checkbox' as 'checked'
23+
* @example I save '$prop' property of 'Checkbox' as 'checked'
24+
*/
25+
When('I save {string} property of {string} as {string}', async function (property, alias, key) {
26+
const element = await getElement(alias);
27+
const propertyName = await getValue(property);
28+
const value = await element.evaluate((node: any, propertyName: string) => node[propertyName], propertyName);
29+
memory.setValue(key, value);
30+
});
31+
32+
/**
33+
* Save attribute of element to memory
34+
* @param {string} attribute - attribute to store
35+
* @param {string} alias - element to get value
36+
* @param {string} key - key to store value
37+
* @example I save 'href' attribute of 'Link' as 'linkHref'
38+
* @example I save '$prop' attribute of 'Link' as 'linkHref'
39+
*/
40+
When('I save {string} attribute of {string} as {string}', async function (attribute, alias, key) {
41+
const element = await getElement(alias);
42+
const attributeName = await getValue(attribute);
43+
const value = await element.getAttribute(attributeName);
44+
memory.setValue(key, value);
45+
});
46+
47+
/**
48+
* Save number of elements in collection to memory
49+
* @param {string} alias - collection to get value
50+
* @param {string} key - key to store value
51+
* @example I save number of elements in 'Search Results' as 'numberOfSearchResults'
52+
*/
53+
When('I save number of elements in {string} collection as {string}', async function (alias, key) {
54+
const collection = await getElement(alias);
55+
const value = await collection.count();
56+
memory.setValue(key, value);
57+
});
58+
59+
/**
60+
* Save array of texts of collection to memory
61+
* @param {string} alias - collection to get values
62+
* @param {string} key - key to store value
63+
* @example I save text of every element of 'Search Results' collection as 'searchResults'
64+
*/
65+
When(
66+
'I save text of every element of {string} collection as {string}',
67+
async function (alias: string, key: string) {
68+
const collection = await getElement(alias);
69+
const values = await collection.evaluateAll(
70+
(collection: Array<any>) => collection.map(e => e.innerText)
71+
);
72+
memory.setValue(key, values);
73+
}
74+
);
75+
76+
/**
77+
* Save array of attributes of collection to memory
78+
* @param {string} alias - collection to get values
79+
* @param {string} key - key to store value
80+
* @example I save 'checked' attribute of every element of 'Search > Checkboxes' collection as 'checkboxes'
81+
*/
82+
When(
83+
'I save {string} attribute of every element of {string} collection as {string}',
84+
async function (attribute: string, alias: string, key: string) {
85+
const collection = await getElement(alias);
86+
const values = await collection.evaluateAll(
87+
(collection: Array<any>, attr: string) => collection.map(e => e.attributes[attr].value),
88+
attribute
89+
);
90+
memory.setValue(key, values);
91+
}
92+
);
93+
94+
/**
95+
* Save array of property of collection to memory
96+
* @param {string} alias - collection to get values
97+
* @param {string} key - key to store value
98+
* @example I save 'href' property of every element of 'Search > Links' collection as 'hrefs'
99+
*/
100+
When(
101+
'I save {string} property of every element of {string} collection as {string}',
102+
async function (property: string, alias: string, key: string) {
103+
const collection = await getElement(alias);
104+
const values = await collection.evaluateAll(
105+
(collection: Array<any>, prop: string) => collection.map(e => e[prop]),
106+
property
107+
);
108+
memory.setValue(key, values);
109+
}
110+
);
111+
112+
/**
113+
* Save current url to memory
114+
* @param {string} key - key to store value
115+
* @example I save current url as 'currentUrl'
116+
*/
117+
When('I save current url as {string}', async function (key: string) {
118+
memory.setValue(key, page.url());
119+
});
120+
121+
/**
122+
* Save current page title to memory
123+
* @param {string} key - key to store value
124+
* @example I save page title as 'currentTitle'
125+
*/
126+
When('I save page title as {string}', async function (key: string) {
127+
const title = await page.title();
128+
memory.setValue(key, title);
129+
});
130+
131+
/**
132+
* Save page screenshot into memory
133+
* @param {string} key - key to store value
134+
* @example I save screenshot as 'screenshot'
135+
*/
136+
When('I save screenshot as {string}', async function(key: string) {
137+
const screenshot = await page.screenshot();
138+
memory.setValue(key, screenshot);
139+
});

test-e2e/features/memory.feature

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Feature: Memory
2+
3+
Background:
4+
When I open '$valuesPage' url
5+
6+
Scenario: element text
7+
When I save text of 'Simple Text Element' as 'memory'
8+
Then I expect '$memory' memory value to be equal 'text value'
9+
10+
Scenario: collection number of elements
11+
When I save number of elements in 'Simple Text List Items' collection as 'memory'
12+
Then I expect '$memory' memory value to be equal '$number(3)'
13+
14+
Scenario: element property
15+
When I save 'value' property of 'Simple Text Input' as 'memory'
16+
Then I expect '$memory' memory value to be equal '123'
17+
18+
Scenario: element attribute
19+
When I save 'name' attribute of 'Simple Text Input' as 'memory'
20+
Then I expect '$memory' memory value to be equal 'textInputName'
21+
22+
Scenario: current url
23+
When I save current url as '$valuesPage'
24+
Then I expect '$memory' memory value to be equal 'textInputName'
25+
26+
Scenario: page title
27+
Then I save page title as 'memory'
28+
Then I expect '$memory' memory value to be equal '@qavajs'
29+
30+
Scenario: collection text of elements
31+
Then I save text of every element of 'Simple Text List Items' collection as 'memory'
32+
Then I expect '$memory' memory value to be equal '$array("first value", "second value", "third value")'
33+
34+
Scenario: collection attribute of elements
35+
Then I save 'id' attribute of every element of 'Simple Text List Items' collection as 'memory'
36+
Then I expect '$memory' memory value to be equal '$array("firstValue", "secondValue", "thirdValue")'
37+
38+
Scenario: collection property of elements
39+
Then I save 'nodeName' property of every element of 'Simple Text List Items' collection as 'memory'
40+
Then I expect '$memory' memory value to be equal '$array("LI", "LI", "LI")'

test-e2e/memory/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ export default class Memory {
55
actionsPage = file(resolve('./test-e2e/apps/actions.html'));
66
framePage = file(resolve('./test-e2e/apps/frame.html'));
77
waitsPage = file(resolve('./test-e2e/apps/waits.html'));
8+
9+
array = (...args: Array<any>) => args
810
}
911

test-e2e/page_object/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { $, $$, Component } from '@qavajs/po';
1+
import { $, $$, Component } from '@qavajs/po-playwright';
22
export default class App {
33
SimpleTextElement = $('#textValue');
44
SimpleTextListItems = $$('#textValueList li');

test-e2e/step-definitions/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Then, When } from '@cucumber/cucumber';
22
import memory from '@qavajs/memory';
3-
import {Browser, BrowserContext, Page} from 'playwright';
3+
import { Browser, BrowserContext, Page } from 'playwright';
4+
import { expect } from 'chai';
45

56
declare global {
67
var browser: Browser;
@@ -9,3 +10,9 @@ declare global {
910
var page: Page;
1011
var config: any;
1112
}
13+
14+
Then('I expect {string} memory value to be equal {string}', async function(actual, expected) {
15+
const actualValue = memory.getValue(actual);
16+
const expectedValue = memory.getValue(expected);
17+
expect(expectedValue).to.eql(actualValue);
18+
});

0 commit comments

Comments
 (0)