Skip to content

Commit 154f2f7

Browse files
authored
feat: expose WebElement (#4043)
1 parent 7be700b commit 154f2f7

File tree

10 files changed

+139
-2
lines changed

10 files changed

+139
-2
lines changed

docs/helpers/Playwright.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,36 @@ let inputs = await I.grabValueFromAll('//form/input');
13891389

13901390
Returns **[Promise][22]<[Array][10]<[string][9]>>** attribute value
13911391

1392+
### grabWebElement
1393+
1394+
Grab WebElement for given locator
1395+
Resumes test execution, so **should be used inside an async function with `await`** operator.
1396+
1397+
```js
1398+
const webElement = await I.grabWebElement('#button');
1399+
```
1400+
1401+
#### Parameters
1402+
1403+
- `locator` **([string][9] | [object][6])** element located by CSS|XPath|strict locator.
1404+
1405+
Returns **[Promise][22]<any>** WebElement of being used Web helper
1406+
1407+
### grabWebElements
1408+
1409+
Grab WebElements for given locator
1410+
Resumes test execution, so **should be used inside an async function with `await`** operator.
1411+
1412+
```js
1413+
const webElements = await I.grabWebElements('#button');
1414+
```
1415+
1416+
#### Parameters
1417+
1418+
- `locator` **([string][9] | [object][6])** element located by CSS|XPath|strict locator.
1419+
1420+
Returns **[Promise][22]<any>** WebElement of being used Web helper
1421+
13921422
### grabWebSocketMessages
13931423

13941424
Grab the recording WS messages

docs/helpers/Puppeteer.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,21 @@ let inputs = await I.grabValueFromAll('//form/input');
12161216
12171217
Returns **[Promise][13]<[Array][15]<[string][6]>>** attribute value
12181218
1219+
### grabWebElements
1220+
1221+
Grab WebElements for given locator
1222+
Resumes test execution, so **should be used inside an async function with `await`** operator.
1223+
1224+
```js
1225+
const webElements = await I.grabWebElements('#button');
1226+
```
1227+
1228+
#### Parameters
1229+
1230+
- `locator` **([string][6] | [object][4])** element located by CSS|XPath|strict locator.
1231+
1232+
Returns **[Promise][13]<any>** WebElement of being used Web helper
1233+
12191234
### handleDownloads
12201235
12211236
Sets a directory to where save files. Allows to test file downloads.

docs/helpers/WebDriver.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,21 @@ let inputs = await I.grabValueFromAll('//form/input');
13671367
13681368
Returns **[Promise][25]<[Array][28]<[string][17]>>** attribute value
13691369
1370+
### grabWebElements
1371+
1372+
Grab WebElements for given locator
1373+
Resumes test execution, so **should be used inside an async function with `await`** operator.
1374+
1375+
```js
1376+
const webElements = await I.grabWebElements('#button');
1377+
```
1378+
1379+
#### Parameters
1380+
1381+
- `locator` **([string][17] | [object][16])** element located by CSS|XPath|strict locator.
1382+
1383+
Returns **[Promise][25]<any>** WebElement of being used Web helper
1384+
13701385
### moveCursorTo
13711386
13721387
Moves cursor to element matched by locator.

docs/webapi/grabWebElement.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Grab WebElement for given locator
2+
Resumes test execution, so **should be used inside an async function with `await`** operator.
3+
4+
```js
5+
const webElement = await I.grabWebElement('#button');
6+
```
7+
8+
@param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
9+
@returns {Promise<*>} WebElement of being used Web helper

docs/webapi/grabWebElements.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Grab WebElements for given locator
2+
Resumes test execution, so **should be used inside an async function with `await`** operator.
3+
4+
```js
5+
const webElements = await I.grabWebElements('#button');
6+
```
7+
8+
@param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
9+
@returns {Promise<*>} WebElement of being used Web helper

lib/helper/Playwright.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,22 @@ class Playwright extends Helper {
13041304
return findFields.call(this, locator);
13051305
}
13061306

1307+
/**
1308+
* {{> grabWebElements }}
1309+
*
1310+
*/
1311+
async grabWebElements(locator) {
1312+
return this._locate(locator);
1313+
}
1314+
1315+
/**
1316+
* {{> grabWebElement }}
1317+
*
1318+
*/
1319+
async grabWebElement(locator) {
1320+
return this._locateElement(locator);
1321+
}
1322+
13071323
/**
13081324
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
13091325
*

lib/helper/Puppeteer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,14 @@ class Puppeteer extends Helper {
917917
return findFields.call(this, locator);
918918
}
919919

920+
/**
921+
* {{> grabWebElements }}
922+
*
923+
*/
924+
async grabWebElements(locator) {
925+
return this._locate(locator);
926+
}
927+
920928
/**
921929
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
922930
*

lib/helper/WebDriver.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,14 @@ class WebDriver extends Helper {
869869
return findFields.call(this, locator).then(res => res);
870870
}
871871

872+
/**
873+
* {{> grabWebElements }}
874+
*
875+
*/
876+
async grabWebElements(locator) {
877+
return this._locate(locator);
878+
}
879+
872880
/**
873881
* Set [WebDriver timeouts](https://webdriver.io/docs/timeouts.html) in realtime.
874882
*

test/helper/Playwright_test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const assert = require('assert');
1+
const { assert } = require('chai');
22
const expect = require('chai').expect;
33
const path = require('path');
44
const fs = require('fs');
@@ -1638,4 +1638,21 @@ describe('Playwright - HAR', () => {
16381638
await I.amOnPage('https://demo.playwright.dev/api-mocking');
16391639
await I.see('CodeceptJS');
16401640
});
1641+
1642+
describe('#grabWebElements, #grabWebElement', () => {
1643+
it('should return an array of WebElement', async () => {
1644+
await I.amOnPage('/form/focus_blur_elements');
1645+
1646+
const webElements = await I.grabWebElements('#button');
1647+
assert.equal(webElements[0], 'locator(\'#button\').first()');
1648+
assert.isAbove(webElements.length, 0);
1649+
});
1650+
1651+
it('should return a WebElement', async () => {
1652+
await I.amOnPage('/form/focus_blur_elements');
1653+
1654+
const webElement = await I.grabWebElement('#button');
1655+
assert.equal(webElement, 'locator(\'#button\').first()');
1656+
});
1657+
});
16411658
});

test/helper/Puppeteer_test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const assert = require('assert');
1+
const { assert } = require('chai');
22
const expect = require('chai').expect;
33
const path = require('path');
44

@@ -1083,4 +1083,14 @@ describe('Puppeteer - Trace', () => {
10831083
assert.ok(fs.existsSync(test.artifacts.trace));
10841084
expect(test.artifacts.trace).to.include(path.join(global.output_dir, 'trace'));
10851085
});
1086+
1087+
describe('#grabWebElements', () => {
1088+
it('should return an array of WebElement', async () => {
1089+
await I.amOnPage('/form/focus_blur_elements');
1090+
1091+
const webElements = await I.grabWebElements('#button');
1092+
assert.include(webElements[0].constructor.name, 'CDPElementHandle');
1093+
assert.isAbove(webElements.length, 0);
1094+
});
1095+
});
10861096
});

0 commit comments

Comments
 (0)