Skip to content

Commit 474b5e1

Browse files
authored
feat: other locators from playwright (#4090)
1 parent 78309b1 commit 474b5e1

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

docs/react.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ To find React element names and props in a tree use [React DevTools](https://chr
6666

6767
> Turn off minification for application builds otherwise component names will be uglified as well
6868
69-
React locators work via [resq](https://github.com/baruchvlz/resq) library, which handles React 16 and above.
69+
- With WebDriver and Puppeteer, React locators work via [resq](https://github.com/baruchvlz/resq) library, which handles React 16 and above.
70+
- With Playwright, React locators work via [Playwright React Locator](https://playwright.dev/docs/other-locators#react-locator).

docs/vue.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ tests
104104

105105
If you agreed to create a demo component, you will also see `TestMe` component in `src/components` folder.
106106

107+
## Locators
108+
109+
For Vue apps a special `vue` locator is available. It allows to select an element by its component name, and props.
110+
111+
```js
112+
{ vue: 'MyComponent' }
113+
{ vue: 'Button', props: { title: 'Click Me' }}
114+
```
115+
116+
With Playwright, you can use Vue locators in any method where locator is required:
117+
118+
```js
119+
I.click({ vue: 'Tab', props: { title: 'Click Me!' }});
120+
I.seeElement({ vue: 't', props: { title: 'Clicked' }});
121+
```
122+
123+
To find Vue element names and props in a tree use [Vue DevTools](https://chromewebstore.google.com/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd) extension.
124+
125+
> Turn off minification for application builds otherwise component names will be uglified as well
126+
127+
Vue locators work via [Playwright Vue Locator](https://playwright.dev/docs/other-locators#vue-locator).
128+
107129
## How to write tests?
108130

109131
* Open `tests/e2e/app_js` and see the demo test

lib/helper/Playwright.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const ElementNotFound = require('./errors/ElementNotFound');
3333
const RemoteBrowserConnectionRefused = require('./errors/RemoteBrowserConnectionRefused');
3434
const Popup = require('./extras/Popup');
3535
const Console = require('./extras/Console');
36-
const findReact = require('./extras/React');
36+
const { findReact, findVue } = require('./extras/PlaywrightReactVueLocator');
3737

3838
let playwright;
3939
let perfTiming;
@@ -3444,6 +3444,7 @@ function buildLocatorString(locator) {
34443444

34453445
async function findElements(matcher, locator) {
34463446
if (locator.react) return findReact(matcher, locator);
3447+
if (locator.vue) return findVue(matcher, locator);
34473448
locator = new Locator(locator, 'css');
34483449

34493450
return matcher.locator(buildLocatorString(locator)).all();
@@ -3505,6 +3506,7 @@ async function proceedClick(locator, context = null, options = {}) {
35053506

35063507
async function findClickable(matcher, locator) {
35073508
if (locator.react) return findReact(matcher, locator);
3509+
if (locator.vue) return findVue(matcher, locator);
35083510

35093511
locator = new Locator(locator);
35103512
if (!locator.isFuzzy()) return findElements.call(this, matcher, locator);

lib/helper/extras/PlaywrightReact.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
async function findReact(matcher, locator) {
2+
let _locator = `_react=${locator.react}`;
3+
let props = '';
4+
5+
if (locator.props) {
6+
props += propBuilder(locator.props);
7+
_locator += props;
8+
}
9+
return matcher.locator(_locator).all();
10+
}
11+
12+
async function findVue(matcher, locator) {
13+
let _locator = `_vue=${locator.vue}`;
14+
let props = '';
15+
16+
if (locator.props) {
17+
props += propBuilder(locator.props);
18+
_locator += props;
19+
}
20+
return matcher.locator(_locator).all();
21+
}
22+
23+
function propBuilder(props) {
24+
let _props = '';
25+
26+
for (const [key, value] of Object.entries(props)) {
27+
if (typeof value === 'object') {
28+
for (const [k, v] of Object.entries(value)) {
29+
_props += `[${key}.${k} = "${v}"]`;
30+
}
31+
} else {
32+
_props += `[${key} = "${value}"]`;
33+
}
34+
}
35+
return _props;
36+
}
37+
38+
module.exports = { findReact, findVue };

typings/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ declare namespace CodeceptJS {
431431
| string
432432
| ILocator
433433
| Locator
434-
| CustomLocators[keyof CustomLocators];
434+
| CustomLocators;
435435

436436
type StringOrSecret = string | CodeceptJS.Secret;
437437

0 commit comments

Comments
 (0)