четвер, 29 січня 2015 р.

Selenium WebDriver: скріншот теста за допомогою JUnit4 @Rule

Якщо необхідно зробити скріншот теста в момент, коли він фейлиться, то можна використати наступний приклад батьківського класу:



package screenshot;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CommonTest {

public static WebDriver driver;

@BeforeClass
public static void setUpDriver() {
driver = new FirefoxDriver();
}

@Rule
public TestWatcher screenshotRule = new TestWatcher() {

@Override
protected void failed(Throwable e, Description description) {
captureScreenshot(description.getMethodName());
}
};

private void captureScreenshot(String name) {
File screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
String path = "./target/screenshots/" + screenshot.getName();
try {
FileUtils.copyFile(screenshot, new File(path));
} catch (IOException e)

{

}
}

@AfterClass
public static void quitDriver() {
driver.quit();
}


Взято звідси

Немає коментарів:

Дописати коментар