High-level abstraction for selenium-webdriver
Features:
- Built with Selenium v3 and W3C WebDriver
- Supports DOM API to explore page structure (via jsdom)
- No setup: built in Chrome support (via chromedriver)
- Flexibility: integrates with any test framework, node script, and selenium cloud provider.
Smoke test for http://example.com using mocha and chai.
import { expect } from 'chai'
import { Browser } from 'sweb'
describe('smoke-tests', () => {
let browser
before(() => { browser = new Browser() })
after(() => { browser.quit() })
it('opens example.com', async () => {
const page = await browser.open('example.com')
expect(page.url).equal('http://example.com/')
expect(page.title).equal('Example Domain')
expect(page.document.links).length(1)
expect(page.document.querySelector('a').href).equal('http://www.iana.org/domains/example')
await page.click('a')
expect(page.url).equal('http://www.iana.org/domains/reserved')
})
})
Requires node >= 6.9 (because of selenium-webdriver)
$ yarn add -D sweb
$ npm i -D sweb
Creates new Browser
instance.
Available options:
screenhostOnError
(default: false) - make automatic screenshot whensweb
throws a custom error, for example on failed waitFor or click.workDir
(default:${cwd}/.sweb
) - specify directory for sweb's logs and screenshots. If folder does not exist, it will be created automatically.driver
- custom build of WebDriver, useful for running different browsers or for Browserstack/Saucelabs integration. Check custom drivers section for more details.
Load url
in browser and return Page
instance.
Page
instance has useful properties:
url
- page urldocument
- similar towindow.document
, it's a jsdom instance with support of all DOM API methods.title
- page titlesource
- page html sourcedriver
- WebDriver instance
Quit real browser.
Perform click event to selector
.
Send user type input (combination of keydown, keypress, keyup for each character) to selector
with text
.
To emulate special keys like ENTER or F12 use Key
.
import { Browser, Key } from 'sweb'
const browser = new Browser()
const page = await browser.open('https://www.google.com')
await page.type('[name="q"]', `selenium webdriver npm${Key.ENTER}`)
Wait for selector
appears in html.
Make screenshot of the page and store to ${browser.workDir}/${name}.png
.
driver
option allows to pass custom selenium-webdriver
configuration.
import 'geckodriver'
import { Browser, Builder } from 'sweb'
const driver = new Builder().forBrowser('firefox').build()
const browser = new Browser({ driver })
const page = await browser.open('http://example.com')
import { Browser, Builder } from 'sweb'
const driver = new Builder()
.usingServer('http://hub-cloud.browserstack.com/wd/hub')
.withCapabilities({
'browserName': 'chrome',
'browserstack.user': browserStackUser,
'browserstack.key': browserStackKey,
'browserstack.debug': 'true',
'build': 'First build'
})
.build()
const browser = new Browser({ driver })
const page = await browser.open('https://www.google.com')
import { Browser, Builder } from 'sweb'
const driver = new Builder()
.usingServer('http://' + username + ':' + accessKey + '@ondemand.saucelabs.com:80/wd/hub')
.withCapabilities({
'browserName': 'chrome',
'platform': 'Windows XP',
'version': '43.0',
username,
accessKey
})
.build()
const browser = new Browser({ driver })
const page = await browser.open('http://example.com')