com.codacy.scoobydoo.web.ScreenShotHelper Maven / Gradle / Ivy
package com.codacy.scoobydoo.web;
import com.codacy.scoobydoo.Constant.Key;
import com.codacy.scoobydoo.Constant.Status;
import com.codacy.scoobydoo.configuration.Configuration;
import com.codacy.scoobydoo.LoggingHelper;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class ScreenShotHelper {
private final TakesScreenshot driver;
private String status;
public ScreenShotHelper(TakesScreenshot driver, Configuration config) {
this.driver = driver;
setStatus(config);
}
public void takesScreenshotOnAction(String message) {
if (status != null && status.equalsIgnoreCase(Status.ENABLED)) {
LoggingHelper.infoWithScreenshot(message, getScreenshotAbsolutePath());
} else {
LoggingHelper.info(message);
}
}
public void takesScreenshotOnError(String message, Exception e) {
if ((status != null) && (status.equalsIgnoreCase(Status.ENABLED) || status.equalsIgnoreCase(Status.ENABLED_ON_FAILURE))) {
LoggingHelper.errorWithScreenshot(message, getScreenshotAbsolutePath(), e);
} else {
LoggingHelper.error(message, e);
}
}
private String getScreenshotAbsolutePath() {
return driver.getScreenshotAs(OutputType.FILE).getAbsolutePath();
}
private void setStatus(Configuration config) {
String screenshotsToReport = config.getData(Key.SCREENSHOTS_TO_REPORT);
if(screenshotsToReport != null
&& !screenshotsToReport.equalsIgnoreCase(Status.ENABLED)
&& !screenshotsToReport.equalsIgnoreCase(Status.DISABLED)) {
final String errorMessage = "Values for key [" + Key.SCREENSHOTS_TO_REPORT
+ "] can only be [" + Status.ENABLED + ", " + Status.DISABLED + ", null].";
IllegalArgumentException exception = new IllegalArgumentException(errorMessage);
LoggingHelper.error(errorMessage, exception);
throw exception;
} else {
status = screenshotsToReport;
}
}
}