如何将 getStartMillis 转换为 Selenium 中的正常时间格式(通过 jenkins 从 Selenium 代码将结果发布到 Splunk)

How to convert getStartMillis to normal time format in Selenium (Posting results to Splunk from Selenium code through jenkins)

我正在使用这个 ITestResult 接口并使用其中的方法。但是我 运行 遇到了一个问题,在我的 splunk 报告中,StartTime 和 EndTime 以奇怪的格式出现,例如 EndTime: 1574335356061 和 StartTime: 1574334748190。 Total time TotalTime: 607871.

也是如此

有什么方法可以获取正常格式的开始时间、结束时间和总时间。

下面是我写的代码。

public static void postLabTestResult(ITestResult test) {
        String testResult = test.getStatus()==1 ? "PASS" : "FAIL" ;
        String testFailReason = test.getThrowable() != null ? test.getThrowable().toString() : "NotDefined";
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String dateTime = dateTimeFormat.format(new Date());
        StringBuilder Entry = new StringBuilder();
        DesktopOSType desktopOS = TestRun.getOS();
        Entry.append("{\"DateTime\": " + "\"" + dateTime + "\", ");
        Entry.append("\"StartTime\": " + "\"" + test.getStartMillis() + "\", ");
        Entry.append("\"EndTime\": " + "\"" + test.getEndMillis() + "\", ");
        Entry.append("\"TotalTime\": " + "\"" + (test.getEndMillis() - test.getStartMillis()) + "\", ");
        Entry.append("\"LabTestFailReason\": " + "\"" + testFailReason.replace("\n", "").replace("\t", "") + "\"}]}");

        //post to splunk
        Logger.logMessage("LabTestData posted:  "+ SplunkManager.postEvent(ConfigProps.SPLUNK_INDEX, Entry.toString()));
    }

splunk 报告显示如下内容

   EndTime: 1574335356061
   LabTestFailReason: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
   LabTestName: verifyLoginTest
   LabTestResult: FAIL
   StartTime: 1574334748190
   TotalTime: 607871

感谢帮助

这是正确的,当您调用 test.getStartMillis() 时,您将获得 Epoch 时间格式 https://www.epochconverter.com。您需要将其转换为 "human" 日期格式。

类似于:

  long yourEpochTime = 1574335356061;
Date d = new Date(yourEpochTime * 1000);

DateFormat date = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
System.out.println(date.format(date));