将JavaScript转换为Groovy/Java



我有一些JavaScript代码(Postman(,需要将其转换为另一个API测试工具(Katalon(。我在更新时间区差的日期时会遇到错误。

试图用TZ差异更新预期日期时发生错误。

原始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(

谢谢,

马特

要在Katalon Studio中运行JS代码,您可以使用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)

相关内容

  • 没有找到相关文章

最新更新