Skip to content

Commit

Permalink
add VideoConfiguration
Browse files Browse the repository at this point in the history
* record video for all tests, or only for annotated with @video
* save video for all tests, or only for failed.
* etc.
  • Loading branch information
asolntsev committed Nov 24, 2024
1 parent af14c84 commit d4ebe07
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package integration.videorecorder.junit5;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.selenide.videorecorder.junit5.VideoRecorderExtension;

public class MarkFailedTestsAsPassed extends VideoRecorderExtension {
@Override
public void afterTestExecution(ExtensionContext context) {
boolean testFailed = "secondTest".equals(context.getRequiredTestMethod().getName());
afterTestExecution(context, testFailed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.selenide.videorecorder.junit5.Video;
import org.junit.jupiter.api.extension.ExtendWith;
import org.selenide.videorecorder.junit5.VideoRecorderExtension;

import java.nio.file.Path;

import static com.codeborne.selenide.Configuration.config;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.closeWebDriver;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.TypeOptions.text;
import static java.time.Duration.ofMillis;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@Video
public class VideoRecorderJunit1Test {
@ExtendWith(MarkFailedTestsAsPassed.class)
class VideoRecorderJunit1Test {
@Test
public void firstTest() {
open(config().browserPosition("50x0"));
void firstTest() {
open(config().browserPosition("50x0").browserSize("800x500"));
for (int i = 0; i < 3; i++) {
open(requireNonNull(getClass().getClassLoader().getResource("search.html")));
$("[name=q]").type(text("#1 JUnit JUnit JUnit JUnit JUnit JUnit JUnit JUnit JUnit #111")
Expand All @@ -28,9 +27,14 @@ public void firstTest() {
}

@AfterEach
public void afterEach() {
Path path = VideoRecorderExtension.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
void closeBrowser() {
closeWebDriver();
}

@AfterEach
void afterEach() {
assertThat(VideoRecorderExtension.getRecordedVideo())
.as("Test is annotated with @Video, but did not fail -> no video")
.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.selenide.videorecorder.junit5.Video;
import org.junit.jupiter.api.extension.ExtendWith;
import org.selenide.videorecorder.core.Video;
import org.selenide.videorecorder.junit5.VideoRecorderExtension;

import java.nio.file.Path;

import static com.codeborne.selenide.Configuration.config;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.closeWebDriver;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.TypeOptions.text;
import static java.time.Duration.ofMillis;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@Video
public class VideoRecorderJunit2Test {
@ExtendWith(MarkFailedTestsAsPassed.class)
class VideoRecorderJunit2Test {
@Test
public void secondTest() {
open(config().browserPosition("200x300"));
@Video
void secondTest() {
open(config().browserPosition("200x300").browserSize("800x500"));
for (int i = 0; i < 3; i++) {
open(requireNonNull(getClass().getClassLoader().getResource("search.html")));
$("[name=q]").type(text("#2 JUnit JUnit JUnit JUnit JUnit JUnit JUnit JUnit JUnit #222")
Expand All @@ -28,7 +31,12 @@ public void secondTest() {
}

@AfterEach
public void afterEach() {
void closeBrowser() {
closeWebDriver();
}

@AfterEach
void afterEach() {
Path path = VideoRecorderExtension.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.selenide.videorecorder.junit5.Video;
import org.junit.jupiter.api.extension.ExtendWith;
import org.selenide.videorecorder.junit5.VideoRecorderExtension;

import java.nio.file.Path;

import static com.codeborne.selenide.Configuration.config;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.closeWebDriver;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.TypeOptions.text;
import static java.time.Duration.ofMillis;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@Video
public class VideoRecorderJunit3Test {
@ExtendWith(MarkFailedTestsAsPassed.class)
class VideoRecorderJunit3Test {
@Test
public void thirdTest() {
open(config().browserPosition("50x600"));
void thirdTest() {
open(config().browserPosition("50x600").browserSize("800x500"));
for (int i = 0; i < 3; i++) {
open(requireNonNull(getClass().getClassLoader().getResource("search.html")));
$("[name=q]").type(text("#3 JUnit JUnit JUnit JUnit JUnit JUnit JUnit JUnit JUnit #333")
Expand All @@ -28,9 +27,14 @@ public void thirdTest() {
}

@AfterEach
public void afterEach() {
Path path = VideoRecorderExtension.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
void closeBrowser() {
closeWebDriver();
}

@AfterEach
void afterEach() {
assertThat(VideoRecorderExtension.getRecordedVideo())
.as("Test failed, but it's not annotated with @Video -> no video")
.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
selenide.video.enabled=true
selenide.video.fps=6
selenide.video.crf=50
#selenide.video.mode=ALL
selenide.video.save.mode=FAILED_ONLY
#selenide.video.save.mode=ALL
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package integration.videorecorder.testng;

import com.codeborne.selenide.ex.ElementNotFound;
import org.selenide.videorecorder.testng.VideoRecorderListener;
import org.testng.ITestResult;

import java.util.regex.Pattern;

import static java.util.regex.Pattern.DOTALL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.ITestResult.SUCCESS;

public class MarkFailedTestsAsPassed extends VideoRecorderListener {
@Override
public void onTestFailure(ITestResult result) {
super.onTestFailure(result);
if ("secondTest".equals(result.getName())) {
assertThat(result.getThrowable()).isInstanceOf(ElementNotFound.class);
assertThat(result.getThrowable().getMessage()).matches(Pattern.compile(".+Video: file:/.+\\.webm.*", DOTALL));
}

result.setStatus(SUCCESS);
result.setThrowable(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package integration.videorecorder.testng;

import com.codeborne.selenide.TypeOptions;
import org.selenide.videorecorder.testng.VideoRecorderListener;
import org.selenide.videorecorder.core.Video;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.nio.file.Path;

import static com.codeborne.selenide.Configuration.config;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.closeWebDriver;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.TypeOptions.text;
import static java.time.Duration.ofMillis;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.selenide.videorecorder.testng.VideoRecorderListener.getRecordedVideo;

@Listeners(VideoRecorderListener.class)
@Listeners(MarkFailedTestsAsPassed.class)
public class VideoRecorderTestNg1Test {

@AfterMethod
public void afterMethod() {
Path path = VideoRecorderListener.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
}

@Test
@Video
public void firstTest() {
open(config().browserPosition("50x50"));
open(config().browserPosition("50x50").browserSize("800x500"));
for (int i = 0; i < 3; i++) {
open(requireNonNull(getClass().getClassLoader().getResource("search.html")));
$("[name=q]").type(TypeOptions.text("#1 TestNG TestNG TestNG TestNG TestNG TestNG TestNG #111")
$("[name=q]").type(text("#1 TestNG TestNG TestNG TestNG TestNG TestNG TestNG #111")
.withDelay(ofMillis(5)));
}
}

@AfterMethod
final void checkVideo() {
assertThat(getRecordedVideo())
.as("Test is annotated with @Video, but did not fail -> no video")
.isEmpty();
}

@AfterMethod
final void closeBrowser() {
closeWebDriver();
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
package integration.videorecorder.testng;

import com.codeborne.selenide.TypeOptions;
import org.selenide.videorecorder.core.Video;
import org.selenide.videorecorder.testng.VideoRecorderListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.nio.file.Path;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Configuration.config;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.closeWebDriver;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.TypeOptions.text;
import static java.time.Duration.ofMillis;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@Listeners(VideoRecorderListener.class)
@Listeners(MarkFailedTestsAsPassed.class)
public class VideoRecorderTestNg2Test {

@AfterMethod
public void afterMethod() {
Path path = VideoRecorderListener.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
}
private static final Logger log = LoggerFactory.getLogger(VideoRecorderTestNg2Test.class);

@Test
@Video
public void secondTest() {
open(config().browserPosition("300x500"));
open(config().browserPosition("200x300").browserSize("800x500"));
for (int i = 0; i < 3; i++) {
open(requireNonNull(getClass().getClassLoader().getResource("search.html")));
$("[name=q]").type(TypeOptions.text("#2 TestNG TestNG TestNG TestNG TestNG TestNG TestNG #222")
$("[name=q]").type(text("#2 TestNG TestNG TestNG TestNG TestNG TestNG TestNG #222")
.withDelay(ofMillis(5)));
}
$("#nope").shouldBe(visible.because("We want this test to fail and save the video"), ofMillis(1));
}

@AfterMethod
final void assertVideoHasBeenSaved(ITestResult result) {
Path path = VideoRecorderListener.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
}

@AfterMethod
final void closeBrowser() {
closeWebDriver();
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package integration.videorecorder.testng;

import com.codeborne.selenide.TypeOptions;
import org.selenide.videorecorder.testng.VideoRecorderListener;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.nio.file.Path;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Configuration.config;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.closeWebDriver;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.TypeOptions.text;
import static java.time.Duration.ofMillis;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.selenide.videorecorder.testng.VideoRecorderListener.getRecordedVideo;

@Listeners(VideoRecorderListener.class)
@Listeners(MarkFailedTestsAsPassed.class)
public class VideoRecorderTestNg3Test {

@AfterMethod
public void afterMethod() {
Path path = VideoRecorderListener.getRecordedVideo().orElseThrow();
assertThat(path.toFile().length()).isGreaterThan(0);
assertThat(path.toFile()).hasExtension("webm");
}

@Test
public void thirdTest() {
open(config().browserPosition("50x1000"));
open(config().browserPosition("50x600").browserSize("800x500"));
for (int i = 0; i < 3; i++) {
open(requireNonNull(getClass().getClassLoader().getResource("search.html")));
$("[name=q]").type(TypeOptions.text("#3 TestNG TestNG TestNG TestNG TestNG TestNG TestNG #333")
$("[name=q]").type(text("#3 TestNG TestNG TestNG TestNG TestNG TestNG TestNG #333")
.withDelay(ofMillis(5)));
}
$("#nope").shouldBe(visible.because("We want this test to fail, but video will not be saved."), ofMillis(1));
}

@AfterMethod
final void assertNoVideoHasBeenSaved() {
assertThat(getRecordedVideo())
.as("Test failed, but it's not annotated with @Video -> no video")
.isEmpty();
}

@AfterMethod
final void closeBrowser() {
closeWebDriver();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#selenide.video.enabled=true
selenide.video.fps=6
#selenide.video.crf=50
#selenide.video.mode=ALL
#selenide.video.save.mode=ALL
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public String generateErrorDetails(AssertionError error, Driver driver, Screensh
protected String videoUrl() {
Optional<Path> recordedVideo = RecordedVideos.getRecordedVideo(currentThread().getId());
return recordedVideo
.map(videoFile -> String.format("Video url: %s", videoFile.toUri()))
.map(videoFile -> String.format("Video: %s", videoFile.toUri()))
.orElse("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

/**
* This annotation is used for indicating that the test will not be recorded.
* Should be used in different extensions(JUnit) and listeners(TestNG)
*
* Used only if {@code RecordingMode == ALL}.
*
* Created by Serhii Bryt
* 09.05.2024 15:00
Expand Down
Loading

0 comments on commit d4ebe07

Please sign in to comment.