node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

sweb

sweb

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.

Example

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')
  })
})

Installation

Requires node >= 6.9 (because of selenium-webdriver)

$ yarn add -D sweb
$ npm i -D sweb

API

new Browser(opts = {})

Creates new Browser instance.

Available options:

  • screenhostOnError (default: false) - make automatic screenshot when sweb 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.

await browser.open(url)

Load url in browser and return Page instance.

Page instance has useful properties:

  • url - page url
  • document - similar to window.document, it's a jsdom instance with support of all DOM API methods.
  • title - page title
  • source - page html source
  • driver - WebDriver instance

await browser.quit()

Quit real browser.

await page.click(selector)

Perform click event to selector.

await page.type(selector, text)

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}`)

await page.waitFor(selector, delay = 2000)

Wait for selector appears in html.

await page.screenshot(name)

Make screenshot of the page and store to ${browser.workDir}/${name}.png.

Custom drivers

driver option allows to pass custom selenium-webdriver configuration.

Firefox with geckodriver

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')

Integration with Browserstack

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')  

Integration with Saucelabs

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')

LICENSE

MIT