我有一個方法執行 aRunnable然后將它完成所需的時間傳遞給另一個方法:
public static void benchmark(Runnable runnable) {
LocalDateTime before = LocalDateTime.now();
runnable.run();
logToDocument(before, LocalDateTime.now());
}
當我使用此方法時一個網頁的加載,這兩個時間戳是完全一樣的,即LocalDateTime before = LocalDateTime.now()與logToDocument(before, LocalDateTime.now())發生在同一時間,即使我看到,在現實中,它需要幾秒鐘的每個頁面負載。我呼叫的方法benchmark如下:
public static void subTestDelay(boolean log, boolean condition, int delay, String message) {
if (log) {
SoftAssert softAssert = new SoftAssert();
ExtentTestManager.benchmark(() -> {
if (!condition) {
tempData.failStatus();
softAssert.fail("Timeout after waiting ".concat(String.valueOf(delay)).concat(" seconds for ").concat(message));
}
});
softAssert.assertAll();
} else {
Assert.assertTrue(condition, "Timeout after waiting ".concat(String.valueOf(delay)).concat(" seconds for ").concat(message));
}
}
condition在這種情況下,變數是這樣的:
public static boolean expectedPageIsLoaded(WebDriver driver, int seconds, String pageName) {
final By loadingBar = By.cssSelector("div[class*='progress']");
final Wait<WebDriver> wait = new FluentWait<>(driver).withTimeout(Duration.ofSeconds(seconds)).pollingEvery(Duration.ofSeconds(1)).ignoring(NoSuchElementException.class).ignoring(NullPointerException.class);
String originalPageName = driver.findElement(By.tagName("body")).getAttribute("page-name");
if (originalPageName == null) {
originalPageName = "no previous page";
tempData.setAction(tempData.REDIRECT);
tempData.setArbitraryData(driver,tempData.TRIGGER, tempData.NAVIGATION, tempData.ORIGIN, originalPageName, tempData.DESTINATION, "user authentication");
}
ExtentTestManager.log(logger, Level.INFO, "Loading page: ".concat(pageName).concat("; Waiting a maximum of: ").concat(String.valueOf(seconds)).concat(" seconds"));
try {
startDateTime = LocalDateTime.now();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(loadingBar));
wait.until(wd -> (GenericTestMethods.pageName(wd).equals(pageName) && wd.findElements(loadingBar).size() == 0));
endDateTime = LocalDateTime.now();
ExtentTestManager.log(logger, Level.INFO, "Page: ".concat(pageName).concat(" loaded after: ").concat(String.valueOf(Duration.between(startDateTime, endDateTime).toMillis())).concat(" milliseconds"));
return true;
} catch (TimeoutException ex) {
return false;
}
}
當我連續測驗 4 個網頁時,我得到了每個頁面加載的正確持續時間,如下所示:
12:35:36.774 [main] INFO test_classes.base.DriverInit - Loading page: user authentication; Waiting a maximum of: 180 seconds
12:35:39.902 [main] INFO test_classes.base.DriverInit - Page: user authentication loaded after: 3124 milliseconds
12:35:41.333 [main] INFO test_classes.base.DriverInit - Loading page: user_cockpit; Waiting a maximum of: 180 seconds
12:35:46.474 [main] INFO test_classes.base.DriverInit - Page: user_cockpit loaded after: 5140 milliseconds
12:35:47.947 [main] INFO test_classes.base.DriverInit - Loading page: organization_overview_time_focus; Waiting a maximum of: 180 seconds
12:35:50.013 [main] INFO test_classes.base.DriverInit - Page: organization_overview_time_focus loaded after: 2066 milliseconds
12:35:51.210 [main] INFO test_classes.base.DriverInit - Loading page: project_time_overview; Waiting a maximum of: 180 seconds
12:35:52.604 [main] INFO test_classes.base.DriverInit - Page: project_time_overview loaded after: 1393 milliseconds
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 25.446 s - in TestSuite
此反饋來自以下行boolean expectedPageIsLoaded:
startDateTime = LocalDateTime.now();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(loadingBar));
wait.until(wd -> (GenericTestMethods.pageName(wd).equals(pageName) && wd.findElements(loadingBar).size() == 0));
endDateTime = LocalDateTime.now();
ExtentTestManager.log(logger, Level.INFO, "Page: ".concat(pageName).concat(" loaded after: ").concat(String.valueOf(Duration.between(startDateTime, endDateTime).toMillis())).concat(" milliseconds"));
然而,我得到的反饋benchmark是每次加載頁面之前和之后的兩個時間戳,它們總是相同的。為了說明這一點,我修改benchmark為列印出Runnable. 這是我得到的:
Before loading: 2021-11-03T12:42:42.855008800
After loading: 2021-11-03T12:42:42.855008800
Before loading: 2021-11-03T12:42:51.505117900
After loading: 2021-11-03T12:42:51.505117900
Before loading: 2021-11-03T12:42:55.145636200
After loading: 2021-11-03T12:42:55.145636200
Before loading: 2021-11-03T12:42:57.555875200
After loading: 2021-11-03T12:42:57.555875200
似乎condition在我的subTestDelay方法中被正確評估,但它所花費的實際時間被忽略了。我懷疑這是因為runnable.run()該benchmark方法在另一個執行緒上執行,但我缺乏找出解決方案的經驗。任何人都可以引導我朝著正確的方向前進嗎?
uj5u.com熱心網友回復:
我終于能夠解決這個問題了!對于那些感興趣的人,這就是我所做的。我編輯subTestDelay以接收 aSupplier<Boolean> condition而不是boolean condition. 現在,對 的評估condition.get()是計時的。這似乎是解決問題的原因(雖然我不知道為什么)。
subTestDelay 現在看起來像這樣:
public static void subTestDelay(boolean log, Supplier<Boolean> condition, int delay, String message) {
String msg = "Timeout after waiting ".concat(String.valueOf(delay)).concat(" seconds for ");
if (log) {
Assert.assertTrue(ExtentTestManager.benchmark(() -> {
if (!condition.get()) {
tempData.failStatus();
return false;
}
return true;
}), msg.concat(message));
} else {
Assert.assertTrue(condition.get(), msg.concat(message));
}
}
benchmark也被編輯為采用 aSupplier<Boolean>而不是 a Runnable:
public static boolean benchmark(Supplier<Boolean> supplier) {
LocalDateTime before = LocalDateTime.now();
boolean returnValue = supplier.get();
logToDocument(before, LocalDateTime.now());
return returnValue;
}
相同的測驗現在產生正確的結果:
Before loading:2021-11-05T11:08:39.758627100
After loading:2021-11-05T11:08:43.953063700
Before loading:2021-11-05T11:08:45.309372500
After loading:2021-11-05T11:08:52.510232700
Before loading:2021-11-05T11:08:53.963683900
After loading:2021-11-05T11:08:56.048520300
Before loading:2021-11-05T11:08:57.233896600
After loading:2021-11-05T11:08:58.438004100
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/350243.html
上一篇:JavaSelenium黃瓜錯誤:java.lang.NoClassDefFoundError:io/cucumber/core/runtime/TypeRegistryConfigurerSuppl
