无法识别JS文件中的缓冲区(使用Karate Framework对API进行自动化测试) [英] Buffer in JS file isn't recognized (API tests automation with Karate Framework)

查看:52
本文介绍了无法识别JS文件中的缓冲区(使用Karate Framework对API进行自动化测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用空手道框架自动化测试.在我们的功能之一中,我们需要解码令牌并获得响应中的作用域.一切正常,除了js中的这段代码.

function(token) {
    return JSON.parse(new Buffer(token.split('.')[1],'base64').toString('ascii')).scope;
}

错误:

Caused by: <eval>:2 ReferenceError: "Buffer" is not defined
Caused by: jdk.nashorn.internal.runtime.ECMAException

在官方教程中,据说javascript是空手道的本机",所以我们不明白为什么不识别Buffer?我们该怎么办?感谢您的帮助

解决方案

我非常确定Buffer是高级/非标准或NodeJS,因此JVM JS引擎(Nashorn)可能不支持它. /p>

这是我的推荐.对于这种情况,请使用Java实用程序进行工作.

例如,请参阅文档中的空手道基本认证示例使用Base64编码.

如果真的很复杂,只需创建一个Java静态函数,将它作为附带的好处进行测试就容易得多.希望这会有所帮助!

We're automating our test with karate framework. In one of our features we need to decode a token and get a scope in the response. Everything works well, except this code in js.

function(token) {
    return JSON.parse(new Buffer(token.split('.')[1],'base64').toString('ascii')).scope;
}

Error:

Caused by: <eval>:2 ReferenceError: "Buffer" is not defined
Caused by: jdk.nashorn.internal.runtime.ECMAException

In official tutorials it is said that javascript is 'native' to karate, so we don't understand why Buffer is not recognized? What we should do? Thanks for any help

解决方案

I'm pretty sure that Buffer is advanced / non-standard or NodeJS so it probably is not supported by the JVM JS engine (Nashorn).

Here's my recommendation. For this case, do the work using Java utilities.

For example, look at the Karate basic-auth example in the doc which uses Base64 encoding.

If it is really complex, simply create a Java static function, it will be much easier to test as a side-benefit. Hope this helps !

这篇关于无法识别JS文件中的缓冲区(使用Karate Framework对API进行自动化测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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