如何获取JSystem测试场景的启动时间?
Hey there! I’ve dealt with exactly this need in JSystem before, so let’s break down how to grab your scenario’s start time easily.
JSystem’s built-in Scenario class provides a direct way to get the start time. You just need to fetch the current running scenario instance first, then call its getStartTime() method. Here’s a code example you can drop into your test script or case:
import jsystem.framework.system.SystemImpl; import jsystem.framework.scenario.Scenario; import java.util.Date; import java.text.SimpleDateFormat; // Get the current active scenario instance Scenario currentScenario = SystemImpl.getInstance().getScenario(); // Retrieve the scenario's start time as a Date object Date scenarioStartTime = currentScenario.getStartTime(); // Optional: Format the date for readable output SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String formattedStartTime = dateFormatter.format(scenarioStartTime); System.out.println("Scenario started at: " + formattedStartTime);
JSystem automatically stores the scenario start time as a context property, so you can also pull it from the TestProperties instance. This is useful if you want a more lightweight approach:
import jsystem.framework.TestProperties; import java.util.Date; // Fetch the start time directly from test properties Date scenarioStartTime = (Date) TestProperties.getInstance().getProperty("scenario.start.time");
Quick Note: Make sure you call these methods after the scenario has started. If you run this code during the initial setup phase before the scenario launches, you’ll get a null value. The safest place is in your test steps or in the test() method of your test case.
内容的提问来源于stack exchange,提问作者user9769489

