-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record screen source code for failing tests.
- Loading branch information
Showing
8 changed files
with
141 additions
and
24 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
serenity-core/src/main/java/net/serenitybdd/core/model/FailureDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.serenitybdd.core.model; | ||
|
||
import net.thucydides.core.model.TestOutcome; | ||
import net.thucydides.core.model.TestStep; | ||
import net.thucydides.core.screenshots.ScreenshotAndHtmlSource; | ||
|
||
public class FailureDetails { | ||
private final TestOutcome testOutcome; | ||
|
||
public FailureDetails(TestOutcome testOutcome) { | ||
this.testOutcome = testOutcome; | ||
} | ||
|
||
public String getConciseErrorMessage() { | ||
if (testOutcome.firstStepWithErrorMessage().isPresent()) { | ||
return testOutcome.firstStepWithErrorMessage().get().getConciseErrorMessage(); | ||
} | ||
return testOutcome.testFailureMessage().or(""); | ||
} | ||
|
||
public String getPageSourceLink() { | ||
for(TestStep testStep : testOutcome.getFlattenedTestSteps()) { | ||
for(ScreenshotAndHtmlSource screenshot : testStep.getScreenshots()) { | ||
if (screenshot.getHtmlSourceName() != null) { | ||
return screenshot.getHtmlSourceName(); | ||
} | ||
} | ||
} | ||
return "#"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
serenity-core/src/main/java/net/serenitybdd/core/photography/DisabledPageSourceRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package net.serenitybdd.core.photography; | ||
|
||
import com.google.common.base.Optional; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
public class DisabledPageSourceRecorder extends PageSourceRecorder { | ||
public DisabledPageSourceRecorder(WebDriver driver) { | ||
super(driver); | ||
} | ||
|
||
@Override | ||
public Optional<File> intoDirectory(Path path) { | ||
return Optional.absent(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
serenity-core/src/main/java/net/serenitybdd/core/photography/PageSourceRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.serenitybdd.core.photography; | ||
|
||
|
||
import com.google.common.base.Optional; | ||
import org.openqa.selenium.WebDriver; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class PageSourceRecorder { | ||
private final WebDriver driver; | ||
|
||
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); | ||
|
||
public PageSourceRecorder(WebDriver driver) { | ||
this.driver = driver; | ||
} | ||
|
||
public Optional<File> intoDirectory(Path path) { | ||
try { | ||
Path pageSourceFile = Files.createTempFile(path, "pagesource", ".html.txt"); | ||
Files.write(pageSourceFile, pageSource()); | ||
return Optional.of(pageSourceFile.toFile()); | ||
} catch(IOException couldNotCreatePageSourcce) { | ||
LOGGER.warn("Could not save the page source HTML file", couldNotCreatePageSourcce); | ||
} | ||
return Optional.absent(); | ||
} | ||
|
||
private byte[] pageSource() { | ||
if (driver.getPageSource() == null) { return new byte[]{}; } | ||
return driver.getPageSource().getBytes(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
serenity-core/src/main/java/net/serenitybdd/core/photography/SoundEngineer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.serenitybdd.core.photography; | ||
|
||
import com.google.common.base.Optional; | ||
import net.thucydides.core.model.TestResult; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
import java.io.File; | ||
|
||
public class SoundEngineer { | ||
|
||
private boolean recordPageSource = true; | ||
|
||
public SoundEngineer ifRequiredForResult(TestResult result) { | ||
recordPageSource = (result == TestResult.FAILURE || result == TestResult.ERROR); | ||
return this; | ||
} | ||
|
||
public PageSourceRecorder recordPageSourceUsing(WebDriver driver) { | ||
return (recordPageSource) ? new PageSourceRecorder(driver) : new DisabledPageSourceRecorder(driver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters