執行 JUnit
Selenium 測試時,它可以建立瀏覽器階段作業的畫面擷取,來顯示錯誤狀況。您可以在測試案例或測試套組執行作業的執行結果中包含這些畫面擷取,以便之後可以檢視。
關於這項作業
在
JUnit
Selenium 配接器執行測試時,它會在執行
JUnit
Selenium 測試的 JVM 中,設定一個名稱為 "selenium.screenshot.dir" 的 Java™ 系統內容。
這個 Java 系統內容的值是執行配接器之機器上的目錄完整路徑。在每一項
JUnit
Selenium 測試執行結束時,會搜尋 "selenium.screenshot.dir" 指定的目錄,以找出影像檔(.png、.jpg、.jpeg、.gif、.tif、.bmp)。只要有找到,就會在「品質管理」伺服器上建立檔案附件,且執行結果中會包含那些檔案附件的參照。
範例
下列的 JUnit 程式碼範例顯示如何擷取瀏覽器現行狀況的畫面,並將畫面擷取檔放在 "selenium.screenshot.dir" 指定的目錄中。
@Test
public void testWikipediaJUnit4_test1() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys("blueberries");
takeScreenShot("blueberry2.png"); // Capture screenshot of current state
driver.findElement(By.name("go")).click();
driver.findElement(By.cssSelector("p > i > a[title=\"Vaccinium\"]")).click();
takeScreenShot("vaccinium2.png"); // Capture screenshot of current state
driver.findElement(By.linkText("huckleberry")).click();
takeScreenShot("huckleberry2.png"); // Capture screenshot of current state
}
private void takeScreenShot(String fname) throws Exception {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String imageFileDir = System.getProperty("selenium.screenshot.dir");
if (imageFileDir == null)
imageFileDir = System.getProperty("java.io.tmpdir");
FileUtils.copyFile(scrFile, new File(imageFileDir, fname));
}