我撰寫了一個方法,它接收一個 Callable 和一個任意長度的字串陣列,我用它來計算 Callable 的執行時間,然后將結果寫入資料庫:
public static <T> T logTimes(Callable<T> callable, String actionToTest, String... testData) throws Exception {
LocalDateTime before = null;
T call = null;
try {
before = LocalDateTime.now();
call = callable.call();
logToDocument(before, LocalDateTime.now(), actionToTest, testData);
} catch (TimeoutException ex) {
logToDocument(before, LocalDateTime.now(), actionToTest, testData);
}
return call;
}
這作業正常,但我現在遇到了一種情況,我需要將 Callable 的回傳值作為引數傳遞給同一個方法:
public static String loadedPageName(WebDriver driver, int seconds) throws Exception {
String originPage = pageName(driver);
By loadingBar = By.xpath("//div[contains(@class, 'progress')]");
WebDriverWait w = new WebDriverWait(driver, seconds);
Function<WebDriver, Boolean> pageFinishedLoading = wd -> wd.findElements(loadingBar).size() == 0;
AtomicReference<String> dest = new AtomicReference<>();
try {
return ExtentTestManager.logTimes(()-> {
w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(loadingBar));
w.until(pageFinishedLoading);
dest.set(driver.findElement(By.tagName("body")).getAttribute("page-name"));
return dest;
}, REDIRECT, ORIGIN, originPage, DESTINATION, dest.get()).get();
} catch (TimeoutException ex) {
ExtentTestManager.reporterLog("Timeout after waiting ".concat(String.valueOf(seconds)).concat(" seconds for a page to load"));
ExtentTestManager.logToDocument(LocalDateTime.now(), seconds, REDIRECT, ORIGIN, originPage, DESTINATION, "timeout");
return null;
}
}
此方法等待頁面加載完畢并回傳已加載頁面的名稱。我想使用的回傳值作為引數在同一個呼叫ExtentTestManager.logTimes(Callable<T> callable, String actionToTest, String... testData)的Callable<T> callable是它的一個引數。
就像現在一樣,loadedPageName(WebDriver driver, int seconds)按預期回傳頁面名稱,但logTimes將null結果寫入資料庫callable。
有沒有辦法實作這一目標?
uj5u.com熱心網友回復:
您在 logTimes 中的代碼等效于:
try {
before = LocalDateTime.now();
call = callable.call();
} catch (TimeoutException ex) {
// do nothing with the exception
}
logToDocument(before, LocalDateTime.now(), actionToTest, testData);
也許你想用 TimeoutException 做點什么?此外,loadedPageName 中的呼叫代碼永遠不會遇到 TimeoutException,因為它已在 logTimes 中被捕獲(并忽略)。
編輯:您宣告“logTimes 將 null 寫入資料庫以獲得可呼叫的結果。” 我不明白,logTimes 不會在任何地方寫入可呼叫的結果,而只會回傳它。
也許你可以通過這個實作我認為你想要的:
AtomicReference<String> dest = new AtomicReference<>();
try {
return ExtentTestManager.logTimes(()-> {
w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(loadingBar));
w.until(pageFinishedLoading);
dest.set(driver.findElement(By.tagName("body")).getAttribute("page-name"));
return dest.get();
}, REDIRECT, ORIGIN, originPage, DESTINATION, dest.get());
不過,我發現最后一個 testData 值有問題:dest.get(). 當它被評估時,它不會被填充一個值。可能這就是您的問題:您將空 AtomicReference 的值作為 testData 傳遞給顯示。
編輯:例如:
public static <T> T logTimes(Callable<T> callable, String actionToTest, String... testData) throws Exception {
LocalDateTime before = LocalDateTime.now();
T call = callable.call();
List<String> allTestData = new ArrayList<String>(Arrays.asList(testData));
allTestData.add(String.valueOf(call));
logToDocument(before, LocalDateTime.now(), actionToTest, allTestData.toArray(new String[0]));
return call;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313799.html
上一篇:“串列”物件沒有屬性“文本”
