Skip to content

fixed the case when there are more than one matched element #1453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"no-cond-assign": 0,
"import/no-unresolved": 0,
"no-await-in-loop": 0,
"arrow-body-style": 0
"arrow-body-style": 0,
"no-loop-func": 0
}
}
1 change: 1 addition & 0 deletions docs/helpers/Nightmare.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ I.fillField({css: 'form#login input[name=username]'}, 'John');
### grabAttributeFrom

Retrieves an attribute from an element located by CSS or XPath and returns it to test.
An array as a result will be returned if there are more than one matched element.
Resumes test execution, so **should be used inside async with `await`** operator.

```js
Expand Down
1 change: 1 addition & 0 deletions docs/helpers/Protractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ I.fillField({css: 'form#login input[name=username]'}, 'John');
### grabAttributeFrom

Retrieves an attribute from an element located by CSS or XPath and returns it to test.
An array as a result will be returned if there are more than one matched element.
Resumes test execution, so **should be used inside async with `await`** operator.

```js
Expand Down
1 change: 1 addition & 0 deletions docs/helpers/Puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ I.fillField({css: 'form#login input[name=username]'}, 'John');
### grabAttributeFrom

Retrieves an attribute from an element located by CSS or XPath and returns it to test.
An array as a result will be returned if there are more than one matched element.
Resumes test execution, so **should be used inside async with `await`** operator.

```js
Expand Down
1 change: 1 addition & 0 deletions docs/helpers/WebDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ I.fillField({css: 'form#login input[name=username]'}, 'John');
### grabAttributeFrom

Retrieves an attribute from an element located by CSS or XPath and returns it to test.
An array as a result will be returned if there are more than one matched element.
Resumes test execution, so **should be used inside async with `await`** operator.

```js
Expand Down
1 change: 1 addition & 0 deletions docs/helpers/WebDriverIO.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ I.fillField({css: 'form#login input[name=username]'}, 'John');
### grabAttributeFrom

Retrieves an attribute from an element located by CSS or XPath and returns it to test.
An array as a result will be returned if there are more than one matched element.
Resumes test execution, so **should be used inside async with `await`** operator.

```js
Expand Down
1 change: 1 addition & 0 deletions docs/webapi/grabAttributeFrom.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Retrieves an attribute from an element located by CSS or XPath and returns it to test.
An array as a result will be returned if there are more than one matched element.
Resumes test execution, so **should be used inside async with `await`** operator.

```js
Expand Down
13 changes: 10 additions & 3 deletions lib/helper/Nightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,16 @@ class Nightmare extends Helper {
*/
async grabAttributeFrom(locator, attr) {
locator = new Locator(locator, 'css');
const el = await this.browser.findElement(locator.toStrict());
assertElementExists(el, locator);
return this.browser.evaluate((el, attr) => window.codeceptjs.fetchElement(el).getAttribute(attr), el, attr);
const els = await this.browser.findElements(locator.toStrict());
const array = [];

for (let index = 0; index < els.length; index++) {
const el = els[index];
assertElementExists(el, locator);
array.push(await this.browser.evaluate((el, attr) => window.codeceptjs.fetchElement(el).getAttribute(attr), el, attr));
}

return array.length === 1 ? array[0] : array;
}


Expand Down
9 changes: 7 additions & 2 deletions lib/helper/Protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,14 @@ class Protractor extends Helper {
async grabAttributeFrom(locator, attr) {
const els = await this._locate(locator);
assertElementExists(els);
return els[0].getAttribute(attr);
}
const array = [];

for (let index = 0; index < els.length; index++) {
const el = els[index];
array.push(await el.getAttribute(attr));
}
return array.length === 1 ? array[0] : array;
}
/**
* {{> ../webapi/seeInTitle }}
*/
Expand Down
10 changes: 8 additions & 2 deletions lib/helper/Puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1363,8 +1363,14 @@ class Puppeteer extends Helper {
async grabAttributeFrom(locator, attr) {
const els = await this._locate(locator);
assertElementExists(els, locator);
return this._evaluateHandeInContext((el, attr) => el[attr] || el.getAttribute(attr), els[0], attr)
.then(t => t.jsonValue());
const array = [];

for (let index = 0; index < els.length; index++) {
const a = await this._evaluateHandeInContext((el, attr) => el[attr] || el.getAttribute(attr), els[index], attr);
array.push(await a.jsonValue());
}

return array.length === 1 ? array[0] : array;
}

/**
Expand Down