How can I make a POST request from a Protractor test?(如何从量角器测试发出 POST 请求?)
问题描述
我想在运行 Protractor 测试之前向数据库服务器发出 POST 请求(带有 JSON 有效负载),在为了注入测试数据.如果可能的话,我该怎么做?
I would like to make a POST request (with JSON payload) to a database server prior to running a Protractor test, in order to inject test data. How can I do this, if at all possible?
推荐答案
在 Andres D 的帮助下,我找到了一种方法.它的要点是通过 browser 在浏览器中运行脚本.执行AsyncScript
并在其中注入 $http 服务.然后告诉 $http 服务发出 POST 请求.这是如何完成的示例 CoffeeScript:
I found a way to do it, with the help of Andres D. The gist of it is to run a script in the browser via browser.executeAsyncScript
and inject the $http service in there. The $http service is then told to make a POST request. Here's example CoffeeScript of how it's done:
browser.get('http://your-angular-app.com')
browser.executeAsyncScript((callback) ->
$http = angular.injector(["ng"]).get("$http")
$http(
url: "http://yourservice.com"
method: "post"
data: yourData
dataType: "json"
)
.success(->
callback([true])
).error((data, status) ->
callback([false, data, status])
)
)
.then((data) ->
[success, response] = data
if success
console.log("Browser async finished without errors")
else
console.log("Browser async finished with errors", response)
)
这篇关于如何从量角器测试发出 POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从量角器测试发出 POST 请求?


基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01