at.willhaben.willtest.rule.PageSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Core classes with as few dependencies as possible
package at.willhaben.willtest.rule;
import at.willhaben.willtest.config.SeleniumProvider;
import at.willhaben.willtest.util.TestReportFile;
import com.google.common.io.Files;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.charset.StandardCharsets;
/**
* Saves the page source into a file in case of a test error
*/
public class PageSource extends TestFailureAwareRule {
private static final Logger LOGGER = LoggerFactory.getLogger(PageSource.class);
private final SeleniumProvider seleniumProvider;
public PageSource(SeleniumProvider seleniumProvider) {
this.seleniumProvider = seleniumProvider;
}
@Override
protected void onError(Description description, Throwable testFailure) throws Throwable {
super.onError(description, testFailure);
String pageSource = seleniumProvider.getWebDriver().getPageSource();
PageContentException pageContentException = new PageContentException(pageSource);
testFailure.addSuppressed(pageContentException);
File destFile = TestReportFile.forTest(description).withPostix(".html").build().getFile();
Files.write(pageSource.getBytes(StandardCharsets.UTF_8), destFile);
LOGGER.info("Saved page source as " + destFile.getAbsolutePath());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy