Skip to content

Commit 9331fc9

Browse files
Merge pull request #27 from qavajs/default-resolver
Added capability to provide defaultResolver to define default logic to identify element
2 parents 1e5d9e5 + 7acb85e commit 9331fc9

File tree

7 files changed

+67
-37
lines changed

7 files changed

+67
-37
lines changed

CHANGELOG.md

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

13+
## [3.2.0]
14+
- :rocket: Added capability to provide _defaultResolver_ to define default logic to identify element
15+
```typescript
16+
class App {
17+
defaultResolver({ alias }: { alias: string }) {
18+
return ({ parent }: { parent: Locator }) => parent.getByText(alias);
19+
}
20+
}
21+
```
22+
1323
## [3.1.0]
1424
- :rocket: updated playwright to 1.53.0
1525
- :rocket: added support of `locator.describe()` to log alias and corresponding locator

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ qavajs implementation for playwright test runner
33

44
## Installation
55

6-
```
7-
npm install @qavajs/playwright`
6+
```bash
7+
npm install @qavajs/playwright
88
```
99

1010
## Configuration
@@ -38,13 +38,21 @@ export default defineConfig({
3838

3939
## Development and testing
4040
Install dependencies
41-
`npm install`
41+
```bash
42+
npm install
43+
```
4244

4345
Install playwright browsers
44-
`install:browsers`
46+
```bash
47+
install:browsers
48+
```
4549

4650
Build lib
47-
`npm run build`
51+
```bash
52+
npm run build
53+
```
4854

4955
Execute e2e browser tests
50-
`npm run test:e2e`
56+
```bash
57+
npm run test:e2e
58+
```

package-lock.json

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

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/playwright",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "steps to interact with playwright",
55
"main": "./index.js",
66
"scripts": {
@@ -26,14 +26,14 @@
2626
"homepage": "https://github.com/qavajs/playwright#readme",
2727
"devDependencies": {
2828
"@types/express": "^5.0.3",
29-
"@types/node": "^24.0.1",
30-
"electron": "^36.4.0",
29+
"@types/node": "^24.1.0",
30+
"electron": "^37.2.4",
3131
"express": "^5.1.0",
3232
"ts-node": "^10.9.2",
3333
"typescript": "^5.8.3"
3434
},
3535
"dependencies": {
36-
"@playwright/test": "^1.53.0",
36+
"@playwright/test": "^1.54.1",
3737
"@qavajs/playwright-runner-adapter": "^1.4.3",
3838
"@qavajs/memory": "^1.10.2"
3939
}

src/pageObject.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +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-
if (!currentComponent) {
102-
throw new Error(`Alias '${currentAlias}' is not a component`);
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';
103109
}
104-
const currentElement = currentComponent[alias];
105-
if (!currentElement) throw new Error(`Alias '${alias}' has not been found in '${currentAlias}'`);
106110
currentAlias = groups.alias;
107111
currentComponent = currentElement.component ? new currentElement.component() : null;
108112

test-e2e/features/pageObject.feature

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

2828
Scenario: top level component
29-
Then I expect text of 'Top Level Component > Text Element' to be equal 'text value'
29+
Then I expect text of 'Top Level Component > Text Element' to be equal 'text value'
30+
31+
Scenario: default resolver
32+
Then I expect text of 'third value' to be equal 'third value'

test-e2e/page_object/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { locator } from '../../src/pageObject';
2+
import { Locator } from '@playwright/test';
23

34
export default class App {
45
SimpleTextElement = locator('#textValue');
@@ -69,6 +70,10 @@ export default class App {
6970
BodyComponentNative = locator.native(({ page }) => page.locator('body')).as(BodyComponent);
7071

7172
TopLevelComponent = locator.as(BodyComponent);
73+
74+
defaultResolver({ alias }: { alias: string }) {
75+
return ({ parent }: { parent: Locator }) => parent.getByText(alias);
76+
}
7277
}
7378

7479
class BodyComponent {

0 commit comments

Comments
 (0)