Converting JavaScript to Groovy/Java

亡梦爱人 提交于 2019-12-24 04:09:29

问题


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.

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

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());
});*/

Converted

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)

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

Thanks,

Matt


回答1:


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)


来源:https://stackoverflow.com/questions/56238677/converting-javascript-to-groovy-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!