Skip to content

Instantly share code, notes, and snippets.

@thibaut-sticky
Last active August 29, 2015 14:06
Show Gist options
  • Save thibaut-sticky/e72408cb5df18a411891 to your computer and use it in GitHub Desktop.
Save thibaut-sticky/e72408cb5df18a411891 to your computer and use it in GitHub Desktop.
WebdriverIO - useful commands
# Public : Base on the `label` and the `fieldset`, return the element id of the
# linked input.
#
# label - {String} Label of the input targeted.
# fieldset - {String} In case several label with the same value are visible,
# the fieldset can be provided to limit the research. (default: '').
#
# Returns
# an {Error}
# a {Number} which is the element id
command = (label, fieldset) ->
callback = arguments[arguments.length - 1]
prefix = ''
if arguments.length is 3 and fieldset
prefix = "//fieldset[legend/text()='#{fieldset}']"
self = this
async.waterfall [
(cb) -> self.visibleElementId "#{prefix}//label[.='#{label}']", cb
(id, cb) -> self.elementIdAttribute id, 'for', cb
(res, cb) -> self.element "##{res.value}", cb
], (err, res) -> if res then callback(err, res.value.ELEMENT) else callback(err)
browser.addCommand 'inputElementId', command
'use strict'
# Public: Select an option within a select element.
#
# value - {String} The option value
# options - {Object} Hash with the following keys:
# :label - {String} Value of the label linked to the select element
# :fieldset - {String} Legend of the fieldset containing the select element
# :multiple - {Boolean} Should be set to true in case the select element has
# the attribute multiple.
#
# Examples
#
# browser.selectOption 'Minimum STR', label: 'Model'
# browser.selectOption 'YouTube', label: 'Formats', multiple: true
# browser.selectOption 'Video'
#
command = (value, options) ->
#:fieldset, :multiple
callback = arguments[arguments.length - 1]
if typeof options is 'function'
option = {}
inputId = null
self = this
async.waterfall [
(cb) ->
if options.label
self.inputElementId options.label, options.fieldset, cb
else
selectPath = "//select[option/text() = '#{value}']"
fieldsetPath = "//fieldset[legend/text() = '#{options.fieldset}']"
if options.fieldset
self.element "#{fieldsetPath}#{selectPath}", cb
else
self.element selectPath, cb
(id, cb) ->
inputId = if id.value? then id.value.ELEMENT else id
unless options.multiple
self.elementIdClick inputId, (err) -> cb err
else
cb null
(cb) -> self.elementIdElement inputId, "//option[.='#{value}']", cb
(res, cb) -> self.elementIdClick res.value.ELEMENT, cb
], (err) -> callback err
browser.addCommand 'selectOption', command
Returns the id of the first visible DOM element matching the `selector`.
'use strict'
#workaround. Waiting for webdriverio/webdriverio#257
ErrorHandler = require '../../../../node_modules/grunt-webdriver/node_modules/webdriverio/lib/utils/ErrorHandler.js'
command = (selector) ->
callback = arguments[arguments.length - 1]
self = this
async.waterfall [
(cb) -> self.elements selector, cb
(res, cb) ->
iterator = (val, seriesCb) ->
self.elementIdDisplayed val.ELEMENT, (err, isVisible) ->
if err
seriesCb({err: err})
else if isVisible and isVisible.value
seriesCb({id: val.ELEMENT})
else
seriesCb()
async.eachSeries res.value, iterator, cb
], (res) ->
if res
callback res.err, res.id
else
callback new ErrorHandler(7)
browser.addCommand 'visibleElementId', command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment