javascript - 将 JavaScript 转换为 Groovy/Java

标签 javascript groovy timezone katalon-studio

我有一些 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)

关于javascript - 将 JavaScript 转换为 Groovy/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238677/

相关文章:

javascript - 从 5 个组合框中获取选定的值

javascript - 在 react 中对字符串应用跨度会返回 [object Object] 而不是给我跨度文本

javascript - 如何在选中复选框的 jquery 的情况下从第一个框中克隆输入的每个输入?

javascript - 从 iframe 获取 ElementById

HTML5 日期时间-本地 (Chrome) : How to input datetime in current time zone

java - 如何在 Eclipse 中使用 JUnit 和 groovy?

java - 如何使用父实体的组合键进行 JPA @OneToMany 单向映射?

groovy - 在 Jenkinsfile 中调用可变参数函数会意外失败

node.js - 如何在 Node js 中为 windows 设置默认时区

amazon-web-services - python获取本地时区?