Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse Selenide screenshots #440

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.impl.ScreenShotLaboratory;
import com.codeborne.selenide.logevents.LogEvent;
import com.codeborne.selenide.logevents.LogEventListener;
import com.codeborne.selenide.logevents.SelenideLog;
Expand All @@ -31,6 +32,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -91,10 +95,24 @@ public AllureSelenide disableLogs(final LogType logType) {
}

private static Optional<byte[]> getScreenshotBytes() {
return ScreenShotLaboratory.getInstance().getLastThreadScreenshot()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you are using saveScreenshots=true and make a single screenshot manually at test start? It seems like you'll get the same screenshot in all the steps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, it should not be a case because Selenide will take a separate screenshot on each fail

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dstekanov and what if we got a case, when someone manually produces a screenshot on fail to attach it to the Main Test Result Page and not under the steps, while also wanting that screenshot to be under given step? Also what if we use Junit assertAll with selenide boolean methods?

.map(AllureSelenide::convertScreenshotFileToByteArray)
.orElseGet(AllureSelenide::takeNewScreenshot);
}

private static Optional<byte[]> convertScreenshotFileToByteArray(final File file) {
try {
return Optional.of(Files.readAllBytes(file.toPath()));
} catch (IOException e) {
return Optional.empty();
}
}

private static Optional<byte[]> takeNewScreenshot() {
try {
return WebDriverRunner.hasWebDriverStarted()
? Optional.of(((TakesScreenshot) WebDriverRunner.getWebDriver()).getScreenshotAs(OutputType.BYTES))
: Optional.empty();
? Optional.of(((TakesScreenshot) WebDriverRunner.getWebDriver()).getScreenshotAs(OutputType.BYTES))
: Optional.empty();
} catch (WebDriverException e) {
LOGGER.warn("Could not get screen shot", e);
return Optional.empty();
Expand Down