sebv / node-wd-sync

sync version of wd
MIT License
53 stars 15 forks source link

How do we trap SE errors? #7

Closed alindsay55661 closed 11 years ago

alindsay55661 commented 11 years ago

For example, when I execute:

browser.elementByCssSelector(bogusSelector);

Sauce Labs correctly reports: "The element could not be found". Also wd-sync fails with:

Error: Error response status: 7.
    at EventEmitter.webdriver._newError (/Users/alindsay/projects/insight-ui/node_modules/wd-sync/node_modules/wd/lib/webdriver.js:53:13)
    at /Users/alindsay/projects/insight-ui/node_modules/wd-sync/node_modules/wd/lib/webdriver.js:123:25
    at Request._callback (/Users/alindsay/projects/insight-ui/node_modules/wd-sync/node_modules/wd/lib/webdriver.js:330:5)
    at Request.self.callback (/Users/alindsay/projects/insight-ui/node_modules/wd-sync/node_modules/wd/node_modules/request/index.js:142:22)
    at Request.EventEmitter.emit (events.js:98:17)
    at Request.<anonymous> (/Users/alindsay/projects/insight-ui/node_modules/wd-sync/node_modules/wd/node_modules/request/index.js:856:14)
    at Request.EventEmitter.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/Users/alindsay/projects/insight-ui/node_modules/wd-sync/node_modules/wd/node_modules/request/index.js:808:12)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:872:14

How can I trap this? I can't figure it out.

sebv commented 11 years ago

This looks normal. I think you should just be able to check the error status. alternatively you may use elementByXXXOrNull or elementByXXXIfExists, which avoids the exception, using the elements method on the server instead of elements.

alindsay55661 commented 11 years ago

Great thanks.

viruschidai commented 11 years ago

@alindsay55661 A hack I used for displaying better error message. It is in coffeescript

{browser} = wdSync.remote()

# Hack to display better error message
# And display the line actually caused the error
for k of browser
  do (k) ->
    fn = browser[k]

    browser[k] = ->
      args = Array.prototype.slice.call arguments, 0
      try
        return fn.apply browser, args
      catch e
        message = e.cause?.value.message ? e['jsonwire-error']?.detail ? e
        message += "\n Function: #{k}, Args: #{JSON.stringify args}"
        throw new Error message
millenniummike commented 8 years ago

How to check an element exists before trying to use it example.

var element=false; element=browser.elementByXPathIfExists(bogusSelector); if (element){title=element.text();}