使用量角器进行e2e测试时记录xhr请求 [英] Logging xhr requests while doing e2e-tests with protractor

查看:63
本文介绍了使用量角器进行e2e测试时记录xhr请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为前端用AngularJS编写的应用程序进行e2e测试,这些测试通常涉及填写表单,将数据发送到后端,然后更新页面并确保数据持久化.测试是用量角器编写的.

I'm doing e2e-tests for an app whose frontend is written in AngularJS, and these tests would typically involve filling in forms, sending the data to the backend, then updating the page and making sure that the data persists. The tests are written in protractor.

其中一个测试失败,并且没有明显原因,因此不一致,因此,我希望获得尽可能多的调试信息.因此,我一直在想,是否有可能将我的前端在测试期间发送给后端的xhr POST请求记录下来,或者更好的是,是否可以捕获并检查浏览器发送的数据?从量角器内部?也许使用browser对象?我谷歌搜索,但没有成功.

One of these tests fails, inconsistently and for no apparent reason, so I would like to get as much information for debugging as possible. So I’ve been wondering whether it’s possible at all to log the xhr POST requests that my frontend is sending to the backend during the test in question, or better yet, whether the data that are being sent by the browser can be captured and examined from within protractor? Perhaps, using the browser object? I googled, and googled, but without success.

是的,我意识到e2e-tests仅旨在与接口交互,而ajax请求对于此类测试而言太底层了.是的,也许将整个后端都剔除掉,而仅仅测试前端会更好.但是,请幽默我.是否可以获取有关用量角器进行e2e测试期间浏览器向服务器发布的内容的信息?

Yes, I realise that e2e-tests are intended only to interact with the interface and that ajax requests are too low-level for such kind of tests. Yes, perhaps stubbing the whole backend out and just testing the frontend would have been much better. But please humor me. Is it possible to get the information about what is being posted by the browser to the server during e2e-tests with protractor?

推荐答案

量角器使用webdriverjs API来驱动"浏览器,因此它无法访问比其他任何Selenium webdriver应用程序都更多的信息.请参阅此处的文档: http://docs. seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations

Protractor uses the webdriverjs API to "drive" the browser, so it won't have access to any more information than any other Selenium webdriver app would have. See the docs here: http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations

除了一些用于控制浏览器的API(添加Cookie,打开新标签)之外,Protractor和WebdriverJS的大多数功能都来自在浏览器中运行JavaScript片段(例如,检查DOM).因此,我认为其中任何一个都不能拦截浏览器和服务器之间的通信.

Outside of some APIs for controlling the browser (adding cookies, opening new tabs), most of the functionality in Protractor and WebdriverJS comes from running snippets of JavaScript in the browser (e.g., to inspect the DOM). So, I don't think any of that qualifies for intercepting communications between the browser and the server.

我认为您可能会使用用于量角器基础结构进行注入代码/模块添加到应用程序开始(这是我可以找到的有关此功能的最佳文档).您应该能够插入一个模块,该模块可以插入$http调用中,并在调用时记录它们(或者,当然,将它们完全模拟掉).

I think you might have luck using the Protractor infrastructure for injecting code/modules into the app start (this is the best doc I can find for this feature). You should be able to inject a module that can interpose on the $http calls an log them as they go (or, of course, fully mock them out).

这篇关于使用量角器进行e2e测试时记录xhr请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆