From 2532164166c974026fc602e156a504a4c89fb3e7 Mon Sep 17 00:00:00 2001 From: Richard Petersen Date: Wed, 27 Nov 2019 11:21:43 +0100 Subject: [PATCH] Streamlined return values from grab* --- lib/helper/WebDriver.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/helper/WebDriver.js b/lib/helper/WebDriver.js index e6f4cf56c..5481ca110 100644 --- a/lib/helper/WebDriver.js +++ b/lib/helper/WebDriver.js @@ -912,14 +912,9 @@ class WebDriver extends Helper { async grabTextFrom(locator) { const res = await this._locate(locator, true); assertElementExists(res, locator); - let val; - if (res.length > 1) { - val = await forEachAsync(res, async el => this.browser.getElementText(getElementId(el))); - } else { - val = await this.browser.getElementText(getElementId(res[0])); - } - this.debugSection('Grab', val); - return val; + const val = await forEachAsync(res, el => this.browser.getElementText(getElementId(el))); + this.debugSection('Grab', String(val)); + return val.length > 1 ? val : val[0]; } /** @@ -929,12 +924,9 @@ class WebDriver extends Helper { async grabHTMLFrom(locator) { const elems = await this._locate(locator, true); assertElementExists(elems, locator); - const values = await Promise.all(elems.map(elem => elem.getHTML(false))); - this.debugSection('Grab', values); - if (Array.isArray(values) && values.length === 1) { - return values[0]; - } - return values; + const val = await forEachAsync(elems, elem => elem.getHTML(false)); + this.debugSection('Grab', String(val)); + return val.length > 1 ? val : val[0]; } /** @@ -944,8 +936,9 @@ class WebDriver extends Helper { async grabValueFrom(locator) { const res = await this._locate(locator, true); assertElementExists(res, locator); - - return forEachAsync(res, async el => el.getValue()); + const val = await forEachAsync(res, el => el.getValue()); + this.debugSection('Grab', String(val)); + return val.length > 1 ? val : val[0]; } /** @@ -954,7 +947,9 @@ class WebDriver extends Helper { async grabCssPropertyFrom(locator, cssProperty) { const res = await this._locate(locator, true); assertElementExists(res, locator); - return forEachAsync(res, async el => this.browser.getElementCSSValue(getElementId(el), cssProperty)); + const val = await forEachAsync(res, async el => this.browser.getElementCSSValue(getElementId(el), cssProperty)); + this.debugSection('Grab', String(val)); + return val.length > 1 ? val : val[0]; } /** @@ -964,7 +959,9 @@ class WebDriver extends Helper { async grabAttributeFrom(locator, attr) { const res = await this._locate(locator, true); assertElementExists(res, locator); - return forEachAsync(res, async el => el.getAttribute(attr)); + const val = await forEachAsync(res, async el => el.getAttribute(attr)); + this.debugSection('Grab', String(val)); + return val.length > 1 ? val : val[0]; } /**