空手道:空手道中是否有一个 http-request 钩子,它在每次 API 调用后自动调用,我可以修改谁的行为? [英] Karate: Is there a http-request hook in karate, that gets called automatically after every API call, and whose behaviour I can modify?

查看:17
本文介绍了空手道:空手道中是否有一个 http-request 钩子,它在每次 API 调用后自动调用,我可以修改谁的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个通用的实现来编写:request-body、request-method &response-body 到我在任何空手道功能文件中发出的所有 HTTP 请求的输出文件.

I want a common implementation to write the: request-body, request-method & response-body to an output file for all the HTTP requests that I make in any of the karate feature files.

我已阅读文档,发现最接近的选项是 afterScenario钩.但是,如果我想在同一场景中进行多个 API 调用,这对我没有帮助.

I have read through the documentation, and the closest option that I found was the afterScenario hook. However this doesn't help me in cases where I want to make multiple API calls in the same scenario.

我还尝试通过将 karate.prevRequest 对象传递给我编写的 java 函数来使用它.虽然这有助于我捕获我想要的信息,但我必须在每次 API 调用后继续调用我的 java 函数,这是我不想做的.

I also tried using karate.prevRequest object by passing it to java function that I wrote. While this helps me capture the information that I want, I have to keep calling my java function after every API call, which I don't want to do.

我希望能够编写一个在每次 API 调用后被调用的钩子,并允许我访问请求主体、请求标头、响应标头、响应主体、请求方法和请求 URL.

I want to be able to write a hook that gets called after every API call and gives me access to request-body, request-headers, response-headers, response-body, request-method and request URL.

这应该是相当直截了当的,显然我似乎在这里遗漏了一些东西.

This should be fairly straight forward, clearly I seem to be missing something here.

推荐答案

首先,我个人认为这是对空手道的误用,你似乎对报告更感兴趣,而不是实际做测试.花点时间考虑一下.空手道具有出色的报告集成功能,以我非常诚实,真诚的观点,您只是在浪费时间.没有人要求这个.

First, I personally think that this is a mis-use of Karate, and you seem to be more interested in reports than actually doing testing. Take some time to think about it. Karate has excellent report integration and you are just wasting your time doing this in my very honest, sincere opinion. No one has asked for this.

无论如何.0.9.5.RC4 中有一个新的 ExecutionHook,详情请看:https://github.com/intuit/karate/issues/970#issuecomment-557443551

Anyway. There is a new ExecutionHook in 0.9.5.RC4, details here: https://github.com/intuit/karate/issues/970#issuecomment-557443551

你可以实现afterStep,看看是不是method步骤然后再做.如果您需要更改,请打开一个问题.另请参阅:https://stackoverflow.com/a/60944060/143475

You can implement afterStep, see if it is a method step and then do this. If you need changes, open an issue. Also see: https://stackoverflow.com/a/60944060/143475

刚刚记得 - 在 develop 分支中,我们添加了一种拦截所有请求和响应的方法:https://twitter.com/karatedsl/status/1195240779213496320 - 很高兴收到您的反馈,看看我们是否可以将此概念与上述挂钩合并 - 或在需要时改进界面

just remembered - in the develop branch we added a way to intercept all requests and responses: https://twitter.com/karatedsl/status/1195240779213496320 - will be happy to get your feedback and see if we can merge this concept with the above hook - or improve the interfaces if needed

在许多情况下,afterScenario 可能就足够了,请注意您可以调用任何空手道功能,这意味着您可以执行 HTTP 请求:https://stackoverflow.com/a/51467470/143475

in many cases, the afterScenario may be sufficient, note that you can call any Karate feature, which means you can do HTTP requests: https://stackoverflow.com/a/51467470/143475

编辑 - 钩子 API 已针对 1.0 进行了更改:https://github.com/intuit/karate/wiki/1.0-upgrade-guide

EDIT - the hooks API has changed for 1.0: https://github.com/intuit/karate/wiki/1.0-upgrade-guide

这篇关于空手道:空手道中是否有一个 http-request 钩子,它在每次 API 调用后自动调用,我可以修改谁的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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