Yes, Selenide takes screenshots automatically on every test failure. This is very useful for error analysis.
By default Selenide puts screenshots to folder build/reports/tests
.
Yes.
You can use property -Dselenide.reportsFolder=test-result/reports
to set any directory to store screenshots to.
For v4.x use -Dselenide.reports=test-result/reports
Another option is to set this folder directly from your code:
Configuration.reportsFolder = "test-result/reports";
For JUnit and TestNG, there is a special support for taking screenshots also on successful tests.
Let me remind: normally you donβt need to setup screenshots in most cases because Selenide automatically takes
a screenshot when some of its checks like $.shouldBe(..)
fails.
But if you want to also automatically take screenshots
assertEquals
or assertThat
),then you can do it by adding couple of lines. See below.
To automatically take screenshot of every failed test:
import com.codeborne.selenide.junit.ScreenShooter;
public class MyTest {
@Rule
public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
// ...
}
To take screenshot of every test (even succeeded), use the following command:
@Rule
public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests().succeededTests();
import com.codeborne.selenide.testng.ScreenShooter;
@Listeners({ ScreenShooter.class})
public class MyTest {
// ...
}
To automatically take screenshots after every test (even succeeded), execute the following command before running tests:
ScreenShooter.captureSuccessfulTests = true;
@ExtendWith({ScreenShooterExtension.class})
public class MyTest {
// ...
}
How to use in Java (with customization):
public class MyTest {
@RegisterExtension
static ScreenShooterExtension screenshotEmAll = new ScreenShooterExtension(true).to("target/screenshots");
}
@ExtendWith(ScreenShooterExtension::class)
class MyTest {
// ...
}
How to use in Kotlin (with customization):
class MyTest {
companion object {
@JvmField
@RegisterExtension
val screenshotEmAll: ScreenShooterExtension = ScreenShooterExtension(true).to("target/screenshots");
}
}
Additionally, you can take screenshot at any moment with a single line of code:
import static com.codeborne.selenide.Selenide.screenshot;
String pngFileName = screenshot("my_file_name");
Selenide will create two files: my_file_name.png
& my_file_name.html
Later we also added a method that can return a screenshot in a desired format (BASE64
, BYTES
or FILE
):
String screenshotAsBase64 = Selenide.screenshot(OutputType.BASE64);
byte[] decoded = Base64.getDecoder().decode(screenshotAsBase64);