如何等待RxJS Websocket在量角器中得到响应?
我有一个简单的登录页面对象,填写登录信息并单击按钮。在该点击服务使用Observable Observable.webSocket(url)
调用WebSocket。然后,一旦websocket响应被发回,我们将前进到另一条路线。测试问题是,在运行click()
之后,它仍然需要几ms,直到我们获得身份验证响应。这意味着下一行会在websocket返回并且路由器被重定向之前运行(这里有一些猜测)。我曾尝试之类的东西elem.click().then(function(){...})
和browser.wait
但似乎工作setTimeout
如何等待RxJS Websocket在量角器中得到响应?
有没有一种方法我可以测试用量角器这些UI副作用排序的WebSocket消息的唯一的事情?
单就地解决方案:你可以写browser.wait
自己的断言函数,将在浏览器端执行JS,如果关闭套接字返回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')
作为一个长期为您的测试项目提供长期的全球解我会建议看看量角器插件功能。你可以定义你自己的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
我认为你可能会做些什么,但是,因为我使用的是量角器的事实可能意味着我在Angular 2中工作。所以我不确定我会在伪代码中存取什么。服务。此外,我们保持websocket的推送通知。我认为像你这样的工作可以描述我是否能够以某种方式获取我正在使用的WS服务,并获取可观察的主题。我真的不知道如何做到这一点。 – Jackie
你说,一旦你收到回应,您导航到另一条路线。你能用吗?将'browser.wait()'与'getCurrentUrl()'合并,并在url包含新路由时返回 – Gunderson
我可以试试看,我也在考虑嘲笑我的websocket服务,但后来我想我离开一个真正的E2E测试。 – Jackie
事实上,如果我做了类似'browser.wait(function(){return browser.getCurrentUrl()。then(function(x){x ===“...”})}''但我我不确定我喜欢那样。当我收到一条不会更改网址或其他内容的消息时会发生什么。 – Jackie