Skip to content

Commit cd32844

Browse files
Added capability to provide _defaultResolver_ to define default logic to identify element (#151)
1 parent 15df6b2 commit cd32844

File tree

7 files changed

+546
-663
lines changed

7 files changed

+546
-663
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ Breaking change:
2424
When I click 'Alert Button'
2525
And I accept dialog
2626
```
27+
28+
- Added capability to provide _defaultResolver_ to define default logic to identify element
29+
```typescript
30+
class App {
31+
defaultResolver({ alias }: { alias: string }) {
32+
return ({ parent }: { parent: Locator }) => parent.getByText(alias);
33+
}
34+
}
35+
```
36+
2737
- :chore: updated playwright dependency
2838

2939
## [2.4.0]

package-lock.json

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

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/steps-playwright",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"description": "steps to interact with playwright",
55
"main": "./index.js",
66
"scripts": {
@@ -33,17 +33,17 @@
3333
"@qavajs/memory": "^1.10.2",
3434
"@qavajs/webstorm-adapter": "^8.0.0",
3535
"@types/chai": "^4.3.20",
36-
"@types/express": "^5.0.0",
37-
"@vitest/coverage-v8": "^3.0.8",
38-
"@vitest/ui": "^3.0.8",
39-
"electron": "^35.0.1",
40-
"express": "^5.0.0",
36+
"@types/express": "^5.0.1",
37+
"@vitest/coverage-v8": "^3.1.1",
38+
"@vitest/ui": "^3.1.1",
39+
"electron": "^35.1.5",
40+
"express": "^5.1.0",
4141
"ts-node": "^10.9.2",
42-
"typescript": "^5.8.2",
43-
"vitest": "^3.0.8",
44-
"@qavajs/validation": "^1.1.0"
42+
"typescript": "^5.8.3",
43+
"vitest": "^3.1.1",
44+
"@qavajs/validation": "^1.1.1"
4545
},
4646
"dependencies": {
47-
"@playwright/test": "^1.51.0"
47+
"@playwright/test": "^1.51.1"
4848
}
4949
}

src/pageObject.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,15 @@ export function query(root: any, path: string) {
9898
for (const element of elements) {
9999
const groups = element.match(/^(?<alias>.+?)(?:\((?<argument>.+)\))?$/)?.groups as { alias: string, argument: string };
100100
const alias = groups.alias.replace(/\s/g, '');
101-
const currentElement = currentComponent[alias];
102-
if (!currentElement) throw new Error(`Alias '${alias}' has not been found in '${currentAlias}'`);
101+
let currentElement = currentComponent[alias];
102+
if (!currentElement && (!currentComponent.defaultResolver || typeof currentComponent.defaultResolver !== 'function')) {
103+
throw new Error(`Alias '${alias}' has not been found in '${currentAlias}'`);
104+
}
105+
if (!currentElement && currentComponent.defaultResolver) {
106+
currentElement = {};
107+
currentElement.selector = currentComponent.defaultResolver({ alias: groups.alias, argument: groups.argument });
108+
currentElement.type = 'native';
109+
}
103110
currentAlias = groups.alias;
104111
currentComponent = currentElement.component ? new currentElement.component() : null;
105112

test-e2e/features/dialog.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@debug
21
Feature: dialog
32

43
Background:

test-e2e/features/pageObject.feature

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ Feature: page object
2323
Then I expect text of 'Simple Text Element Template ({$selector})' to be equal 'text value'
2424

2525
Scenario: top level component
26-
Then I expect text of 'Top Level Component > Text Element' to be equal 'text value'
26+
Then I expect text of 'Top Level Component > Text Element' to be equal 'text value'
27+
28+
Scenario: default resolver
29+
Then I expect text of 'third value' to be equal 'third value'

test-e2e/page_object/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { locator } from '../../po';
2+
import { Locator } from '@playwright/test';
3+
24
export default class App {
35
SimpleTextElement = locator('#textValue');
46
SimpleTextElementTemplate = locator.template(selector => selector);
@@ -79,6 +81,10 @@ export default class App {
7981
SimpleTextListItemsByJS = locator('js=document.querySelectorAll("#textValueList li")');
8082

8183
TopLevelComponent = locator.as(BodyComponent);
84+
85+
defaultResolver({ alias }: { alias: string }) {
86+
return ({ parent }: { parent: Locator }) => parent.getByText(alias);
87+
}
8288
}
8389

8490
class BodyComponent {

0 commit comments

Comments
 (0)