Skip to content

Latest commit

 

History

History
267 lines (176 loc) · 5.92 KB

COFFEE-DOC.md

File metadata and controls

267 lines (176 loc) · 5.92 KB

wd-sync with CofffeeScript

A synchronous version with a nice api of wd, the lightweight WebDriver / Selenium2 client for node.js, built using node-fibers.

Remote testing with Sauce Labs also works.

Note: headless zombie was removed in 1.1.0

status

Build Status Selenium Test Status

Selenium Test Status

install

npm install wd-sync

usage

All the methods from wd are available.

The browser functions must to be run within a sync block. This block holds the fiber environment. The sync block context is set to the browser, so that the browser methods may be accessed using @.

The executeAsync and safeExecuteAsync methods may still be run asynchronously.

# assumes that selenium server is running

wdSync = require 'wd-sync'

# 1/ simple Wd example

{browser, sync} = wdSync.remote()

sync ->
  console.log "server status:", @status()
  @init browserName:'firefox'
  console.log "session id:", @getSessionId()
  console.log "session capabilities:", @sessionCapabilities()

  @get "http://google.com"
  console.log @title()

  queryField = @elementByName 'q'
  @type queryField, "Hello World"
  @type queryField, "\n"

  @setWaitTimeout 3000
  @elementByCss '#ires' # waiting for new page to load
  console.log @title()

  console.log @elementByNameIfExists 'not_exists' # undefined

  @quit()

Sauce Labs example

Remote testing with Sauce Labs works.

# configure saucelabs username/access key here
username = process.env.SAUCE_USERNAME or '<USERNAME>'
accessKey = process.env.SAUCE_KEY or '<ACCESS KEY>'

wdSync = require 'wd-sync'

# 2/ wd saucelabs example

desired =
  platform: "LINUX"
  name: "wd-sync demo"
  browserName: "firefox"

{browser, sync} = wdSync.remote \
  "ondemand.saucelabs.com",
  80,
  username,
  accessKey

sync ->
  console.log "server status:", @status()
  @init(desired)
  console.log "session id:", @getSessionId()
  console.log "session capabilities:", @sessionCapabilities()

  @get "http://google.com"
  console.log @title()

  queryField = @elementByName 'q'
  @type queryField, "Hello World"
  @type queryField, "\n"

  @setWaitTimeout 3000
  @elementByCss '#ires' # waiting for new page to load
  console.log @title()

  @quit()

browser initialization

Please refer to wd doc.

wrap

wrap is a wrapper around sync within so it nicely integrates with test frameworks like Mocha. wrap manages the done callback for you.

pre functions may be specified globally or for each test. They are called called before the wrap block starts, in the original context (In Mocha, it may be used to configure timeouts).

The example below is using the Mocha test framework.

# Assumes that the selenium server is running
# Use 'mocha --compilers coffee:coffee-script' to run (npm install -g mocha)

wdSync = require 'wd-sync'

chai = require 'chai'
chai.should()

# 4/ wrap example

describe "WdWrap", ->

  describe "wrap", ->

    browser = null
    wrap = wdSync.wrap
      with: -> browser
      pre: -> #optional
        @timeout 30000

    before (done) ->
      {browser} = wdSync.remote()
      done()

    it "should work", wrap -> # may also pass a pre here
      @init()

      @get "http://google.com"
      @title().toLowerCase().should.include 'google'

      queryField = @elementByName 'q'
      @type queryField, "Hello World"
      @type queryField, "\n"

      @setWaitTimeout 3000
      @elementByCss '#ires' # waiting for new page to load
      @title().toLowerCase().should.include 'hello world'

      @quit()

to retrieve the browser currently in use

The current browser is automatically stored in the Fiber context. It can be retrieved with the wd.current() function.

This is useful when writing test helpers.

# assumes that selenium server is running

wdSync = require 'wd-sync'
  
# 5/ retrieving the current browser

{browser, sync} = wdSync.remote()

myOwnGetTitle = ->
  wdSync.current().title()

sync ->
  @init browserName:'firefox'

  @get "http://google.com"
  console.log myOwnGetTitle()

  @quit()

api

doc

Doc modifications must be done in the doc/template directory.

running tests

local / selenium server:

1/ Install and start Selenium server

./node_modules/.bin/install_selenium
./node_modules/.bin/install_chromedriver
./node_modules/.bin/start_selenium_with_chromedriver

2/ run tests

make test 

remote / Sauce Labs

1/ configure sauce environment

export SAUCE_USERNAME=<SAUCE_USERNAME>
export SAUCE_ACCESS_KEY=<SAUCE_ACCESS_KEY>
# if using sauce connect
./node_modules/.bin/install_sauce_connect
./node_modules/.bin/start_sauce_connect

2/ run tests

make test_e2e_sauce
make test_midway_sauce_connect

building doc/mapping

README + doc

1/ Update the templates

2/ run make build_doc

mappings

1/ Upgrade wd

2/ run make build_mapping

publishing

npm version [patch|minor|major]
git push --tags
npm publish