selenium-helpers
v0.1.6
Published
Set of helper functions for Selenium webdriver
Downloads
42
Maintainers
Readme
selenium-helpers
Set of helper functions for Selenium webdriver
If you have different needs regarding the functionality, please add a feature request.
Installation
npm install --save selenium-helpersUsage
const oneSecond = 1000;
const oneMinute = 60 * oneSecond;
const TIMEOUT = 2 * oneMinute;
const seleniumHelpers = require('selenium-helpers')({ timeout: TIMEOUT });
const url = 'http://some-login-page/';
/* return */ seleniumHelpers.buildChrome()
.then((webDriver) => seleniumHelpers.openPage(url))
.then(() => seleniumHelpers.debug('* Login page is loading...'))
.then((el) => seleniumHelpers.scrollAndType('#login', username) )
.then((el) => seleniumHelpers.scrollAndType('#password', password) )
.then((el) => seleniumHelpers.scrollAndClick('#') )
.then(() => seleniumHelpers.debug('* Username/password entered, waiting for home page...'))
.then(() => seleniumHelpers.waitForOneOfElements([
By.css('#page-layout-1'),
By.css('#page-layout-2'),
By.css('#page-layout-3')
], TIMEOUT))
.then(() => seleniumHelpers.debug('* Home page loaded.'))
.catch((reason) => {
console.log(reason);
return Promise.reject(reason);
})
;Methods
buildChrome({ proxyConfig, chromeOptions })
Build the driver for Chrome using new clean Chrome profile.
proxyConfig- optional:{ host, port, username, password [, tempDir ] }chromeOptions- optional; if set, chromeOptions will be used when build the Chrome's driver.
returns {Promise}
buildFirefox({ profilePath })
Build the driver for Firefox using new clean Firefox profile.
profilePath-string- optional, path to Firefox profile
returns {Promise}
buildFirefoxWithProfile(firefoxProfilePath)
Build the driver for Firefox using existing Firefox profile defined by path firefoxProfilePath.
returns {Promise}
sleep(timeout)
Delay for timeout milliseconds
scrollToBottom()
Scroll to the bottom of the page.
scrollIntoView(webElementOrCssString)
Scroll to element defined as webElement or string css selector and click on it.
scrollAndClick(webElementOrCssString)
Scroll to element defined as webElement or string css selector and then click on it.
scrollAndType(webElementOrCssString)
Scroll to element defined as webElement or string css selector and then set its value property (as if value was typed in it).
waitForOneOfElements(bySelectors, timeout, string)
Wait until one of several elements will appear on the page.
Possible elements are defined by array bySelectors of webdriver.By selectors.
bySelectors- array ofwebdriver.Byselectorstimeout- wait timeoutstring- optional string to output if fails
Example:
waitForOneOfElements([
By.css('.element-class-1'),
By.css('#element-id-1'),
By.id('element-id-2'),
By.css('[name=some-name]')
], TIMEOUT);Full set of By selectors cn be found here: http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_By.html
waitAndAct(actions)
Simple scripting-like function which allows to define:
- Wait rules to wait for several conditions simultaneously:
- wait, - CSS selector to wait;
- waitText - Text to wait (anywhere on the whole page);
- waitJs - Javascript expression to wait until evaluates to true.
- Actions to execute when some element defined by one of wait rules is found. Several actions may be combined. Actions in order of execution:
- sleep - pause (value in milliseconds);
- scroll - scroll to element;
- click - click on the element;
- reload - reload the page;
- cancel - stop to wait and continue with the program execution.
Example:
const ACTIONS = [
{ wait: '.class1', click: '.button--to-click' }, // if this element found, click the button
{ wait: '.class2', click: '.another-button' }, // if other element found, click another button
{ wait: '.class2', cancel: true }, // finish to wait
];Action click is done in two phases: (1) scroll to element (2) click
clickAllDetails(cssToClick, xpathToParent, subCssToWait)
For each of elements defined by cssToClick:
remember path to element's parent defined by
xpathToParent(example:'./../..');click the element
wait util some element with
subCssToWaitwill appear under the element's parent
openPage(url, actions)
Open page (using driver.navigate().to(url)), then execute actions (see waitAndAct for more info on actions)
savePageSource(pathname)
Save value of driver.getPageSource() into file pathname
saveOuterHTML(pathname)
Save value of document.documentElement.outerHTML into file pathname
Debugging
This module uses standard debug with namespace selenium-helpers.
In order to get output to console you must set environment variable DEBUG to the value of namespace.
Example for Linux:
DEBUG=selenium-helpers <command-to-start>Example for Windows:
SET DEBUG=selenium-helpers
<command-to-start>Where <command-to-start> is the command starting the app i.e.: node app.js or npm start etc.
Credits
Links to package pages:
github.com npmjs.com travis-ci.org coveralls.io inch-ci.org
License
MIT
