Skip to content

Commit 587e475

Browse files
author
DavertMik
committed
improved output
1 parent 252af38 commit 587e475

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

docs/webapi/click.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ If a fuzzy locator is given, the page will be searched for a button, link, or im
33
For buttons, the "value" attribute, "name" attribute, and inner text are searched. For links, the link text is searched.
44
For images, the "alt" attribute and inner text of any parent links are searched.
55

6+
If no locator is provided, defaults to clicking the body element (`'//body'`).
7+
68
The second parameter is a context (CSS or XPath locator) to narrow the search.
79

810
```js
11+
// click body element (default)
12+
I.click();
913
// simple link
1014
I.click('Logout');
1115
// button of form
@@ -20,6 +24,6 @@ I.click('Logout', '#nav');
2024
I.click({css: 'nav a.login'});
2125
```
2226

23-
@param {CodeceptJS.LocatorOrString} locator clickable link or button located by text, or any element located by CSS|XPath|strict locator.
27+
@param {CodeceptJS.LocatorOrString} [locator='//body'] (optional, `'//body'` by default) clickable link or button located by text, or any element located by CSS|XPath|strict locator.
2428
@param {?CodeceptJS.LocatorOrString | null} [context=null] (optional, `null` by default) element to search in CSS|XPath|Strict locator.
2529
@returns {void} automatically synchronized promise through #recorder

lib/command/init.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const packages = []
3535
let isTypeScript = false
3636
let extension = 'js'
3737

38-
const requireCodeceptConfigure = "const { setHeadlessWhen, setCommonPlugins } = require('@codeceptjs/configure');"
3938
const importCodeceptConfigure = "import { setHeadlessWhen, setCommonPlugins } from '@codeceptjs/configure';"
4039

4140
const configHeader = `
@@ -232,9 +231,9 @@ export default async function (initPath) {
232231
fs.writeFileSync(typeScriptconfigFile, configSource, 'utf-8')
233232
print(`Config created at ${typeScriptconfigFile}`)
234233
} else {
235-
configSource = beautify(`/** @type {CodeceptJS.MainConfig} */\nexports.config = ${inspect(config, false, 4, false)}`)
234+
configSource = beautify(`/** @type {CodeceptJS.MainConfig} */\nexport const config = ${inspect(config, false, 4, false)}`)
236235

237-
if (hasConfigure) configSource = requireCodeceptConfigure + configHeader + configSource
236+
if (hasConfigure) configSource = importCodeceptConfigure + configHeader + configSource
238237

239238
fs.writeFileSync(configFile, configSource, 'utf-8')
240239
print(`Config created at ${configFile}`)

lib/helper/Playwright.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ class Playwright extends Helper {
19111911
* ```
19121912
*
19131913
*/
1914-
async click(locator, context = null, options = {}) {
1914+
async click(locator = '//body', context = null, options = {}) {
19151915
return proceedClick.call(this, locator, context, options)
19161916
}
19171917

@@ -2640,6 +2640,33 @@ class Playwright extends Helper {
26402640
return array
26412641
}
26422642

2643+
/**
2644+
* Retrieves the ARIA snapshot for an element using Playwright's [`locator.ariaSnapshot`](https://playwright.dev/docs/api/class-locator#locator-aria-snapshot).
2645+
* This method returns a YAML representation of the accessibility tree that can be used for assertions.
2646+
* If no locator is provided, it captures the snapshot of the entire page body.
2647+
*
2648+
* ```js
2649+
* const snapshot = await I.grabAriaSnapshot();
2650+
* expect(snapshot).toContain('heading "Sign up"');
2651+
*
2652+
* const formSnapshot = await I.grabAriaSnapshot('#login-form');
2653+
* expect(formSnapshot).toContain('textbox "Email"');
2654+
* ```
2655+
*
2656+
* [Learn more about ARIA snapshots](https://playwright.dev/docs/aria-snapshots)
2657+
*
2658+
* @param {string|object} [locator='//body'] element located by CSS|XPath|strict locator. Defaults to body element.
2659+
* @return {Promise<string>} YAML representation of the accessibility tree
2660+
*/
2661+
async grabAriaSnapshot(locator = '//body') {
2662+
const matchedLocator = new Locator(locator)
2663+
const els = await this._locate(matchedLocator)
2664+
assertElementExists(els, locator)
2665+
const snapshot = await els[0].ariaSnapshot()
2666+
this.debugSection('Aria Snapshot', snapshot)
2667+
return snapshot
2668+
}
2669+
26432670
/**
26442671
* {{> saveElementScreenshot }}
26452672
*

lib/helper/Puppeteer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ class Puppeteer extends Helper {
11461146
*
11471147
* {{ react }}
11481148
*/
1149-
async click(locator, context = null) {
1149+
async click(locator = '//body', context = null) {
11501150
return proceedClick.call(this, locator, context)
11511151
}
11521152

lib/helper/WebDriver.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ class WebDriver extends Helper {
10481048
*
10491049
* {{ react }}
10501050
*/
1051-
async click(locator, context = null) {
1051+
async click(locator = '//body', context = null) {
10521052
const clickMethod = this.browser.isMobile && !this.browser.isW3C ? 'touchClick' : 'elementClick'
10531053
const locateFn = prepareLocateFn.call(this, context)
10541054

@@ -1173,7 +1173,17 @@ class WebDriver extends Helper {
11731173
assertElementExists(res, field, 'Field')
11741174
const elem = usingFirstElement(res)
11751175
highlightActiveElement.call(this, elem)
1176-
await elem.clearValue()
1176+
try {
1177+
await elem.clearValue()
1178+
} catch (err) {
1179+
if (err.message && err.message.includes('invalid element state')) {
1180+
await this.executeScript(el => {
1181+
el.value = ''
1182+
}, elem)
1183+
} else {
1184+
throw err
1185+
}
1186+
}
11771187
await elem.setValue(value.toString())
11781188
}
11791189

@@ -2912,14 +2922,6 @@ async function findFields(locator) {
29122922
let els = await this._locate(Locator.field.labelEquals(literal))
29132923
if (els.length) return els
29142924

2915-
// Try ARIA selector for accessible name
2916-
try {
2917-
els = await this._locate(`aria/${locator.value}`)
2918-
if (els.length) return els
2919-
} catch (e) {
2920-
// ARIA selector not supported or failed
2921-
}
2922-
29232925
els = await this._locate(Locator.field.labelContains(literal))
29242926
if (els.length) return els
29252927

test/helper/Playwright_test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,41 @@ describe('Playwright', function () {
800800
.then(html => assert.equal(html.trim(), '<a href="/login" target="_blank">New tab</a>')))
801801
})
802802

803+
describe('#grabAriaSnapshot', () => {
804+
it('should grab aria snapshot of entire page when no locator is provided', () =>
805+
I.amOnPage('/')
806+
.then(() => I.grabAriaSnapshot())
807+
.then(snapshot => {
808+
assert.ok(snapshot)
809+
assert.ok(typeof snapshot === 'string')
810+
}))
811+
812+
it('should grab aria snapshot of entire page using default body locator', () =>
813+
I.amOnPage('/')
814+
.then(() => I.grabAriaSnapshot('//body'))
815+
.then(snapshot => {
816+
assert.ok(snapshot)
817+
assert.ok(typeof snapshot === 'string')
818+
}))
819+
820+
it('should grab aria snapshot of a specific element', () =>
821+
I.amOnPage('/')
822+
.then(() => I.grabAriaSnapshot('#area1'))
823+
.then(snapshot => {
824+
assert.ok(snapshot)
825+
assert.ok(typeof snapshot === 'string')
826+
}))
827+
828+
it('should grab aria snapshot from within an iframe', () =>
829+
I.amOnPage('/iframe')
830+
.then(() => I.switchTo({ frame: 'iframe' }))
831+
.then(() => I.grabAriaSnapshot())
832+
.then(snapshot => {
833+
assert.ok(snapshot)
834+
assert.ok(typeof snapshot === 'string')
835+
}))
836+
})
837+
803838
describe('#grabBrowserLogs', () => {
804839
it('should grab browser logs', () =>
805840
I.amOnPage('/')

0 commit comments

Comments
 (0)