本文介绍了如何在Karate API中为所有功能文件设置全局头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发布请求以获取授权令牌,并将其包含在所有后续测试请求的标头中.该令牌每次都会更改,但是只要我不断发送请求,它对于整个测试会话都有效.在每个功能文件中,我可以调用另一个功能文件来获取此令牌.但是我不想对每个功能文件都执行此操作.我只想一次在测试开始时获得令牌并将其用于所有功能文件.我怎么做?我已经在GitHub上阅读了空手道信息,但没有找到答案.

I need to post a request to get an authorization token and include it in the header for all subsequent test requests. This token changes every time but it is valid for the entire test session as long as I keep sending requests. In each feature file I can call another feature file to get this token. But I don't want to do this for every feature file. I just want to get the token one time at the start of the test and use it for all feature files. How do I do that? I've read the Karate information on GitHub but did not find the answer.

推荐答案

演示中的第二个示例回答了您所有的问题:空手道演示.

The second example in the demos answers all your questions: karate-demo.

对不起,我读你的问题太快了.您可以在 karate-config.js 中使用 karate.call(),以便将其应用于所有功能文件.我不建议您这样做,因为您总会拥有一些不需要的功能.只需使用功能调用,就不会过度设计测试.

Sorry, I read your question too fast. You can use karate.call() in karate-config.js so it applies to all feature files. I don’t recommend this because you will always have some features where you DONT need this. Just use a call to a feature and don’t over-engineer your tests.

我还想了一点,如果您对Java感到满意,则可以在每个功能的开头(甚至在全局 karate-config.js ),然后在该单例中缓存auth令牌的值.因此,您可以按照自己的需要做,这将很灵活.

I thought about this a little more, if you are comfortable with Java, you could make a call to a singleton at the start of each feature (or even in the global karate-config.js) and in that singleton cache the value of the auth token. So you can do exactly what you need and it will be flexible.

基于此问题-我们将此功能作为 karate.callSingle()操作添加到了Karate中,这是文档:"> https://github.com/intuit/karate/tree/develop#the-karate-object

based on this question - we added this functionality to Karate as a karate.callSingle() operation, here's the doc: https://github.com/intuit/karate/tree/develop#the-karate-object

这篇关于如何在Karate API中为所有功能文件设置全局头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:03