Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Improve handling of alerts during screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Feb 19, 2016
1 parent a0f7b56 commit 77f2190
Showing 1 changed file with 8 additions and 6 deletions.
Expand Up @@ -23,10 +23,12 @@
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
Expand Down Expand Up @@ -64,8 +66,9 @@ public void updateTestID(String testId) {
}

private void createScreenshot(String ofType) {
if (isAlertPresent(driver)) {
log.info("[Screenshot]: Prevented by Alert");
Optional<Alert> alert = getAlert(driver);
if (alert.isPresent()) {
log.error("Screenshot({}) prevented by browser alert: {}", testId, alert.get().getText());
return;
}
File testIDDir = null;
Expand Down Expand Up @@ -100,12 +103,11 @@ private String generateFileName(String ofType) {
.concat(ofType).concat(".png");
}

private boolean isAlertPresent(WebDriver driver) {
private Optional<Alert> getAlert(WebDriver driver) {
try {
driver.switchTo().alert();
return true;
return Optional.of(driver.switchTo().alert());
} catch (NoAlertPresentException nape) {
return false;
return Optional.empty();
}
}

Expand Down

0 comments on commit 77f2190

Please sign in to comment.