将JavaScript转换为Groovy

将JavaScript转换为Groovy

本文介绍了将JavaScript转换为Groovy/Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些javascript代码(邮递员),需要将其转换为在其他API测试工具(Katalon)中使用.在使用时区差异更新日期时出现错误.

I have some javascript code (Postman) that needs to be converted for use in another API test tool (Katalon). I am getting errors while updating the date with the timezone difference.

尝试使用TZ差异更新ExpectedDate时发生错误.

The error occurs when trying to update the expectedDate with the TZ difference.

原始Javascript

Original Javascript

//Postman - Validate Date
/*var jsonData = pm.response.json();
var expectedDate = new Date();
var firstDate = new Date(jsonData[0].Date);
var locationOffset = Number(pm.environment.get("locationOffset"));
var tzDifference = locationOffset * 60 +       expectedDate.getTimezoneOffset();
expectedDate = new Date(expectedDate.getTime() + tzDifference * 60 *  1000);
firstDate = new Date(firstDate.getTime() + tzDifference * 60 * 1000);
pm.test("Testing Date - Expected: " + expectedDate + " & Returned: "  + firstDate, function (){
    pm.expect(firstDate.getDate()).to.be.eql(expectedDate.getDate());
});*/

已转换

import java.text.SimpleDateFormat

//get expected date
Date expectedDate = new Date()
println('ExpDate: ' + expectedDate)

//get first date
String newDateAdded = parsedJson.DailyForecasts[0].Date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM- dd'T'HH:mm:ss")
Date firstDate = dateFormat.parse(newDateAdded)
println("FirstDate: " + firstDate)

//get offset
def locationOffset = GlobalVariable.gmt_offset.toDouble() //gmt_offset = -4

//get TZ difference
def tzDifference = locationOffset * 60 +   expectedDate.getTimezoneOffset()
println("tzDifference: " + tzDifference)

//update exp date (error here: groovy.lang.GroovyRuntimeException:  Could not find matching constructor for:  java.util.Date(java.lang.Double)
expectedDate = new Date(expectedDate.getTime() + tzDifference * 60 *  1000)
println('ExpDate: ' + expectedDate)

//update first date
firstDate = new Date(firstDate.getTime() + tzDifference * 60 * 1000)

错误:groovy.lang.GroovyRuntimeException:找不到与以下对象匹配的构造函数:java.util.Date(java.lang.Double)

Error: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Date(java.lang.Double)

谢谢

马特

推荐答案

要在Katalon Studio中运行JS代码,您可以使用JavaScript 执行器:

To run JS code in Katalon Studio, you can use JavaScript Executor:

String postman ='''
var jsonData = pm.response.json();
var expectedDate = new Date();
var firstDate = new Date(jsonData[0].Date);
var locationOffset = Number(pm.environment.get("locationOffset"));
var tzDifference = locationOffset * 60 +       expectedDate.getTimezoneOffset();
expectedDate = new Date(expectedDate.getTime() + tzDifference * 60 *  1000);
firstDate = new Date(firstDate.getTime() + tzDifference * 60 * 1000);
pm.test("Testing Date - Expected: " + expectedDate + " & Returned: "  + firstDate, function (){
    pm.expect(firstDate.getDate()).to.be.eql(expectedDate.getDate());
});
'''
WebUI.executeJavaScript(postman, null)

这篇关于将JavaScript转换为Groovy/Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:07