Skip to content

Commit f8537c8

Browse files
authored
updated grabbers (#2381)
* updated grabbers * fixed Playwright tests
1 parent fcef0ee commit f8537c8

2 files changed

Lines changed: 64 additions & 36 deletions

File tree

lib/helper/Playwright.js

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,51 +1477,87 @@ class Playwright extends Helper {
14771477
*
14781478
*/
14791479
async grabTextFrom(locator) {
1480+
const texts = await this.grabTextFromAll(locator);
1481+
assertElementExists(texts, locator);
1482+
this.debugSection('Text', texts[0]);
1483+
return texts[0];
1484+
}
1485+
1486+
1487+
/**
1488+
* {{> grabTextFromAll }}
1489+
*
1490+
*/
1491+
async grabTextFromAll(locator) {
14801492
const els = await this._locate(locator);
1481-
assertElementExists(els, locator);
14821493
const texts = [];
14831494
for (const el of els) {
14841495
texts.push(await (await el.getProperty('innerText')).jsonValue());
14851496
}
1486-
if (texts.length === 1) return texts[0];
1497+
this.debug(`Matched ${els.length} elements`);
14871498
return texts;
14881499
}
14891500

14901501
/**
14911502
* {{> grabValueFrom }}
14921503
*/
14931504
async grabValueFrom(locator) {
1505+
const values = await this.grabValueFromAll(locator);
1506+
assertElementExists(values, locator);
1507+
this.debugSection('Value', values[0]);
1508+
return values[0];
1509+
}
1510+
1511+
/**
1512+
* {{> grabValueFromAll }}
1513+
*/
1514+
async grabValueFromAll(locator) {
14941515
const els = await findFields.call(this, locator);
1495-
assertElementExists(els, locator);
1496-
return els[0].getProperty('value').then(t => t.jsonValue());
1516+
this.debug(`Matched ${els.length} elements`);
1517+
return Promise.all(els.map(el => el.getProperty('value').then(t => t.jsonValue())));
14971518
}
14981519

14991520
/**
15001521
* {{> grabHTMLFrom }}
15011522
*/
15021523
async grabHTMLFrom(locator) {
1524+
const html = await this.grabHTMLFromAll(locator);
1525+
assertElementExists(html, locator);
1526+
this.debugSection('HTML', html[0]);
1527+
return html[0];
1528+
}
1529+
1530+
/**
1531+
* {{> grabHTMLFromAll }}
1532+
*/
1533+
async grabHTMLFromAll(locator) {
15031534
const els = await this._locate(locator);
1504-
assertElementExists(els, locator);
1505-
const values = await Promise.all(els.map(el => el.$eval('xpath=.', element => element.innerHTML, el)));
1506-
if (Array.isArray(values) && values.length === 1) {
1507-
return values[0];
1508-
}
1509-
return values;
1535+
this.debug(`Matched ${els.length} elements`);
1536+
return Promise.all(els.map(el => el.$eval('xpath=.', element => element.innerHTML, el)));
15101537
}
15111538

15121539
/**
15131540
* {{> grabCssPropertyFrom }}
15141541
*
15151542
*/
15161543
async grabCssPropertyFrom(locator, cssProperty) {
1544+
const cssValues = await this.grabCssPropertyFromAll(locator, cssProperty);
1545+
assertElementExists(cssValues, locator);
1546+
this.debugSection('CSS', cssValues[0]);
1547+
return cssValues[0];
1548+
}
1549+
1550+
/**
1551+
* {{> grabCssPropertyFromAll }}
1552+
*
1553+
*/
1554+
async grabCssPropertyFromAll(locator, cssProperty) {
15171555
const els = await this._locate(locator);
1556+
this.debug(`Matched ${els.length} elements`);
15181557
const res = await Promise.all(els.map(el => el.$eval('xpath=.', el => JSON.parse(JSON.stringify(getComputedStyle(el))), el)));
15191558
const cssValues = res.map(props => props[toCamelCase(cssProperty)]);
15201559

1521-
if (res.length > 0) {
1522-
return cssValues;
1523-
}
1524-
return cssValues[0];
1560+
return cssValues;
15251561
}
15261562

15271563
/**
@@ -1618,16 +1654,28 @@ class Playwright extends Helper {
16181654
*
16191655
*/
16201656
async grabAttributeFrom(locator, attr) {
1657+
const attrs = await this.grabAttributeFromAll(locator, attr);
1658+
assertElementExists(attrs, locator);
1659+
this.debugSection('Attribute', attrs[0]);
1660+
return attrs[0];
1661+
}
1662+
1663+
1664+
/**
1665+
* {{> grabAttributeFromAll }}
1666+
*
1667+
*/
1668+
async grabAttributeFromAll(locator, attr) {
16211669
const els = await this._locate(locator);
1622-
assertElementExists(els, locator);
1670+
this.debug(`Matched ${els.length} elements`);
16231671
const array = [];
16241672

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

1630-
return array.length === 1 ? array[0] : array;
1678+
return array;
16311679
}
16321680

16331681
/**

test/helper/Playwright_test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,26 +540,6 @@ describe('Playwright', function () {
540540
.then(() => I.seeInField('#text2', 'London')));
541541
});
542542

543-
544-
describe('#grabHTMLFrom', () => {
545-
it('should grab inner html from an element using xpath query', () => I.amOnPage('/')
546-
.then(() => I.grabHTMLFrom('//title'))
547-
.then(html => assert.equal(html, 'TestEd Beta 2.0')));
548-
549-
it('should grab inner html from an element using id query', () => I.amOnPage('/')
550-
.then(() => I.grabHTMLFrom('#area1'))
551-
.then(html => assert.equal(html.trim(), '<a href="/form/file" qa-id="test" qa-link="test"> Test Link </a>')));
552-
553-
it('should grab inner html from multiple elements', () => I.amOnPage('/')
554-
.then(() => I.grabHTMLFrom('//a'))
555-
.then(html => assert.equal(html.length, 5)));
556-
557-
it('should grab inner html from within an iframe', () => I.amOnPage('/iframe')
558-
.then(() => I.switchTo({ frame: 'iframe' }))
559-
.then(() => I.grabHTMLFrom('#new-tab'))
560-
.then(html => assert.equal(html.trim(), '<a href="/login" target="_blank">New tab</a>')));
561-
});
562-
563543
describe('#grabBrowserLogs', () => {
564544
it('should grab browser logs', () => I.amOnPage('/')
565545
.then(() => I.executeScript(() => {

0 commit comments

Comments
 (0)