Protractor: How should I propagate an error from within browser.executeAsync?

て烟熏妆下的殇ゞ 提交于 2019-12-12 18:27:59

问题


If from a Protractor spec, I execute a script within browser.executeAsyncScript, how should I communicate that the script has indeed failed? Consider the following call to browser.executeAsyncScript:

browser.executeAsyncScript((callback) ->
  # How do I communicate an error condition here!?
  callback()
)
.then((data) ->
  console.log("Browser async finished without errors: #{data}")
, (data) ->
  console.log("Browser async finished with errors: #{data}")
)

What I want to happen is that the error callback to then is invoked. How do I do this?


回答1:


From the webdriverjs doc:

driver.executeAsyncScript(function() {
  var callback = arguments[arguments.length - 1];
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/resource/data.json", true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      callback(xhr.responseText);
    }
  }
  xhr.send('');
}).then(function(str) {
  console.log(JSON.parse(str)['food']);
});

So, it does not seem to have an error callback, but you can pass some arguments to the callback method. You can use it to propagate errors.



来源:https://stackoverflow.com/questions/21803115/protractor-how-should-i-propagate-an-error-from-within-browser-executeasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!