Skip to content

Commit c194f64

Browse files
committed
add some new helper methods
1 parent fb01be6 commit c194f64

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

runtime/helpers.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,80 @@ module.exports = {
217217
}, timeout, timeoutMessage);
218218
},
219219

220+
/**
221+
* Waits until an css element exists and returns it
222+
* @param {string} elementSelector - HTML element CSS selector
223+
* @param {integer} waitInMilliseconds - (optional) number of milliseconds to wait for the element
224+
* @returns {Promise} a promisse that will resolve if the element is found within timeout
225+
* @example
226+
* helpers.waitForCssXpathElement('#login-button', 5000);
227+
*/
228+
waitForCssXpathElement: function (elementSelector, waitInMilliseconds){
229+
// use either passed in timeout or global default
230+
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
231+
232+
// if the locator starts with '//' assume xpath, otherwise css
233+
var selector = (localizador.indexOf('//') === 0) ? "xpath" : "css";
234+
235+
// readable error message
236+
var timeoutMessage = attributeName + ' still exists after ' + waitInMilliseconds + ' milliseconds';
237+
238+
// wait until the element exists
239+
return driver.wait(selenium.until.elementLocated({ [selector]: elementSelector }), timeout, timeoutMessage);
240+
},
241+
242+
/**
243+
* Scroll until element is visible
244+
* @param {WebElement} elemento - selenium web element
245+
* @returns {Promise} a promise that will resolve to the scripts return value.
246+
* @example
247+
* helpers.scrollToElement(webElement);
248+
*/
249+
scrollToElement: function (element) {
250+
return driver.executeScript('return arguments[0].scrollIntoView(false);', element);
251+
},
252+
253+
/**
254+
* Select a value inside a dropdown list by its text
255+
* @param {string} elementSelector - css or xpath selector
256+
* @param {string} optionName - name of the option to be chosen
257+
* @param {Promise} a promise that will resolve when the click command has completed
258+
* @example
259+
* helpers.selectByVisibleText('#country', 'Brazil');
260+
*/
261+
selectDropdownValueByVisibleText: async function (elementSelector, optionName) {
262+
var select = await helpers.waitForCssXpathElement(elementSelector);
263+
var selectElements = await select.findElements({ css: 'option' });
264+
var options = [];
265+
266+
for (var option of selectElements) {
267+
options.push((await option.getText()).toUpperCase());
268+
}
269+
optionName = optionName.toUpperCase();
270+
271+
return selectElements[options.indexOf(optionName)].click();
272+
},
273+
274+
/**
275+
* Awaits and returns an array of all windows opened
276+
* @param {integer} waitInMilliseconds - (optional) number of milliseconds to wait for the result
277+
* @returns {Promise} a promise that will resolve with an array of window handles.
278+
* @example
279+
* helpers.waitForNewWindows();
280+
*/
281+
waitForNewWindows: async function (waitInMilliseconds) {
282+
// use either passed in timeout or global default
283+
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
284+
285+
var windows = [];
286+
for (var i = 0; i < timeout; i += 1000) {
287+
windows = await driver.getAllWindowHandles(); // procura por todas as windows abertas
288+
if (windows.length > 1) return windows;
289+
290+
await driver.sleep(1000);
291+
}
292+
},
293+
220294
/**
221295
* Get the content value of a :before pseudo element
222296
* @param {string} cssSelector - css selector of element to inspect

0 commit comments

Comments
 (0)