我如何等待RxJS Websocket在Protractor中获得响应?

时间:2017-03-01 17:46:10

标签: angular protractor rxjs

我有一个简单的登录页面对象,用于填写登录信息并单击按钮。在该单击上,服务使用Observable Observable.webSocket(url)调用websocket。然后,一旦发回websocket响应,我们通过导航到另一条路线向前移动。测试问题是在运行click()之后,它仍然需要几毫秒才能获得身份验证响应。这意味着下一行在websocket返回之前运行,并且路由器已被重定向(这里有一种猜测)。我尝试了elem.click().then(function(){...})browser.wait之类的内容,但唯一可行的方法是setTimeout

有没有办法可以使用量角器测试这些UI副作用的websocket消息?

1 个答案:

答案 0 :(得分:1)

作为单一位置解决方案:您可以为browser.wait编写自己的谓词函数,该函数将在浏览器端执行JS,如果socket已关闭(或您自己的检查),则返回true:

let waitForWebSocket = () => {
    let jsFunction = () => {//some js code that will be executed on browser side
        //pseudocode. I beleive this will be something with promises and event listeners
        if (webSocketConnectionClosed) {
            return true
        } else {
            return false
        }
    }
    return browser.executeScript(jsFunction)    
}

browser.wait(waitForWebSocket, 10000, 'WebSocket connection expected to be closed')

作为测试项目的长期全球解决方案。我建议看一下Protractor Plugins功能。您可以定义自己的waitForPromise挂钩,它将在每个操作之间执行。

http://www.protractortest.org/#/plugins

/**
 * Between every webdriver action, Protractor calls browser.waitForAngular() to
 * make sure that Angular has no outstanding $http or $timeout calls.
 * You can use waitForPromise() to have Protractor additionally wait for your
 * custom promise to be resolved inside of browser.waitForAngular().
 *
 * @this {Object} bound to module.exports
 *
 * @throws {*} If this function throws an error, a failed assertion is added to
 *     the test results.
 *
 * @return {q.Promise=} Can return a promise, in which case protractor will wait
 *     for the promise to resolve before continuing.  If the promise is
 *     rejected, a failed assertion is added to the test results, and protractor
 *     will continue onto the next command.  If nothing is returned or something
 *     other than a promise is returned, protractor will continue onto the next
 *     command.
 */
exports.waitForPromise = function() {};

类似的:

exports.waitForCondition