Skip to content

Commit dbf2c54

Browse files
Merge pull request #5 from dilpreetj/dilpreetj/4/prefer-aria-over-xpath
4 | Prefer aria selector over xpath when both are available
2 parents e602014 + f5e2a33 commit dbf2c54

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/stringifyExtension.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,7 @@ export class StringifyExtension extends PuppeteerStringifyExtension {
270270
if (idSelector) return idSelector
271271

272272
/**
273-
* use xPath selector if available
274-
*/
275-
const xPathSelector = findByCondition(
276-
selectors,
277-
(s) => s.startsWith(XPATH_PREFIX)
278-
)
279-
if (xPathSelector) return `"${xPathSelector.slice(XPATH_PREFIX.length + 1)}`
280-
281-
/**
282-
* use WebdriverIOs aria selector
273+
* Use WebdriverIOs aria selector as second option
283274
* https://webdriver.io/docs/selectors#accessibility-name-selector
284275
*/
285276
const ariaSelector = findByCondition(
@@ -291,6 +282,15 @@ export class StringifyExtension extends PuppeteerStringifyExtension {
291282
// Remove Aria selectors
292283
const nonAriaSelectors = this.filterArrayByString(selectors, ARIA_PREFIX)
293284

285+
/**
286+
* use xPath selector if aria selector is not available
287+
*/
288+
const xPathSelector = findByCondition(
289+
selectors,
290+
(s) => s.startsWith(XPATH_PREFIX)
291+
)
292+
if (xPathSelector) return `"${xPathSelector.slice(XPATH_PREFIX.length + 1)}`
293+
294294
let preferredSelector
295295

296296
// Give preference to user selector

test/stringifyExtension.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('StringifyExtension', () => {
8787
expect(writer.toString()).toBe('await browser.$("#heading").setValue("webdriverio")\n')
8888
})
8989

90-
it('should prefer xPath selector', async () => {
90+
it('should prefer aria selector over xpath', async () => {
9191
const ext = new StringifyExtension()
9292
const step = {
9393
type: StepType.Change as const,
@@ -98,7 +98,7 @@ describe('StringifyExtension', () => {
9898
const flow = { title: 'change step', steps: [step] }
9999
const writer = new InMemoryLineWriter(' ')
100100
await ext.stringifyStep(writer, step, flow)
101-
expect(writer.toString()).toBe('await browser.$("//*[@data-test=\\"heading\\"]").setValue("webdriverio")\n')
101+
expect(writer.toString()).toBe('await browser.$("aria/Search").setValue("webdriverio")\n')
102102
})
103103

104104
it('should prefer link text selectors', async () => {

0 commit comments

Comments
 (0)