Skip to content

Commit aee11a8

Browse files
more selector improvements
1 parent 8a0b7b0 commit aee11a8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/stringifyExtension.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,31 @@ export class StringifyExtension extends PuppeteerStringifyExtension {
278278
return formatAsJSLiteral(`=${selectors[0][0].slice(ARIA_PREFIX.length)}`)
279279
}
280280

281+
/**
282+
* check if selector is an element with text, e.g.
283+
* ```
284+
* "selectors": [
285+
* [
286+
* "aria/Flat White $18.00"
287+
* ],
288+
* [
289+
* "#app > div:nth-child(4) > ul > li:nth-child(5) > h4"
290+
* ]
291+
* ],
292+
* ```
293+
* then use element with text selector: h4=Flat White $18.00
294+
*/
295+
if (
296+
Array.isArray(selectors[0]) && Array.isArray(selectors[1]) &&
297+
selectors[0][0].startsWith(ARIA_PREFIX) &&
298+
selectors[1][0].includes(' > ')
299+
) {
300+
const tagName = selectors[1][0].split('>').pop()!.trim()
301+
// replace "button:nth-child(1)" with "button"
302+
.split(':')[0]
303+
return formatAsJSLiteral(`${tagName}=${selectors[0][0].slice(ARIA_PREFIX.length)}`)
304+
}
305+
281306
// Remove Aria selectors
282307
const nonAriaSelectors = this.filterArrayByString(selectors, ARIA_PREFIX)
283308

test/stringifyExtension.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,42 @@ describe('StringifyExtension', () => {
8989
expect(writer.toString()).toBe('await browser.$("=Guides").setValue("webdriverio")\n')
9090
})
9191

92+
it('should fetch by text', async () => {
93+
const ext = new StringifyExtension()
94+
const step = {
95+
type: 'change' as const,
96+
value: 'webdriverio',
97+
selectors: [[
98+
'aria/Flat White $18.00'
99+
], [
100+
'#app > div:nth-child(4) > ul > li:nth-child(5) > h4'
101+
]],
102+
target: 'main',
103+
}
104+
const flow = { title: 'change step', steps: [step] }
105+
const writer = new InMemoryLineWriter(' ')
106+
await ext.stringifyStep(writer, step, flow)
107+
expect(writer.toString()).toBe('await browser.$("h4=Flat White $18.00").setValue("webdriverio")\n')
108+
})
109+
110+
it('should fetch by text with pseudo selector', async () => {
111+
const ext = new StringifyExtension()
112+
const step = {
113+
type: 'change' as const,
114+
value: 'webdriverio',
115+
selectors: [[
116+
'aria/Yes'
117+
], [
118+
'[data-cy=add-to-cart-modal] > form > button:nth-child(1)'
119+
]],
120+
target: 'main',
121+
}
122+
const flow = { title: 'change step', steps: [step] }
123+
const writer = new InMemoryLineWriter(' ')
124+
await ext.stringifyStep(writer, step, flow)
125+
expect(writer.toString()).toBe('await browser.$("button=Yes").setValue("webdriverio")\n')
126+
})
127+
92128
it('should correctly exports keyDown step', async () => {
93129
const ext = new StringifyExtension()
94130
const step = {

0 commit comments

Comments
 (0)