まとめ
Junit5やJUnit6では、OpenTestReportingを用いて画像や動画などを添付できる
Playwrightなどの画像や動画の取得に長けたフレームワークと併用することにより、Excelスクショペタペタをなくすことができる。
そもそもOpenTestReportingとは?
JunitXMLを新しく標準化した形式。
ユースケース
Playwright-Javaで取得したスクリーンショットをHTMLレポートに添付し、表示する。
(Nodejs版Playwirghtでできることと同じ内容を実施する)
実例
pom
要点は、下記
-
junit.platform.reporting.open.xml.enabled = trueにすること -
junit-platform-reportingを依存関係に追加すること -
junit.platform.reporting.output.dirを、target/surefire-reports以外にすること - テスト引数の最初に、
TestReporter testReporterを付けること
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>examples</artifactId>
<version>1.56.0</version>
<name>Playwright Client Examples</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<playwright.version>1.56.0</playwright.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.14.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>${playwright.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-reporting</artifactId>
<version>1.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
<configuration>
<properties>
<configurationParameters>
junit.platform.reporting.open.xml.enabled = true
junit.platform.reporting.output.dir = target/surefire-reports2
junit.platform.output.capture.stdout = true
</configurationParameters>
</properties>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<disable>false</disable>
<version>3.0.2</version>
<usePhrasedFileName>false</usePhrasedFileName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
<consoleOutputReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5ConsoleOutputReporter">
<disable>false</disable>
<encoding>UTF-8</encoding>
<usePhrasedFileName>false</usePhrasedFileName>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
<disable>false</disable>
<usePhrasedFileName>false</usePhrasedFileName>
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
<usePhrasedClassNameInTestCaseSummary>true</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</project>
TestExample.java
package org.example;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;
import com.microsoft.playwright.options.AriaRole;
import org.junit.jupiter.api.MediaType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@UsePlaywright(TestExample.CustomOptions.class)
public class TestExample {
private static final Path RECORD_VIDEO_DIR = Paths.get("videos/");
public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options()
.setHeadless(false)
.setContextOptions(new Browser.NewContextOptions()
.setRecordVideoSize(1920,1080)
.setRecordVideoDir(RECORD_VIDEO_DIR));
}
}
@Test
void shouldSearchWiki(TestReporter testReporter, Page page) {
page.navigate("https://en.wikipedia.org/wiki/Main_Page");
page.getByRole(AriaRole.SEARCHBOX, new Page.GetByRoleOptions().setName("Search Wikipedia")).click();
page.getByRole(AriaRole.COMBOBOX, new Page.GetByRoleOptions().setName("Search Wikipedia")).fill("playwright");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Playwright (software) End-to-")).click();
System.out.println("page is: " + page.url());
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Playwright_(software)");
// これでスクリーンショット取れる
byte[] shot = page.screenshot(new Page.ScreenshotOptions().setFullPage(true));
testReporter.publishFile("test1.png", MediaType.IMAGE_PNG,file -> Files.write(file, shot));
// ブラウザが閉じられてしまうが、動画も保存できる。ただし、HTMLレポートには埋め込まれない。
final Path vp = Paths.get("t" + page.video().path().toString());
page.close();
page.video().saveAs(vp);
testReporter.publishFile(vp, MediaType.create("video", "webm"));
}
}
テスト実施
今の所、open-test-reportingのmavenプラグインはまだ無さそう。open-test-reporting-cliをインストールする必要がある。
mvn surefire:test
java -jar open-test-reporting-cli-0.2.5.jar html-report --output "target\surefire-reports2\open-test-report.html" "target\surefire-reports2\open-test-report.xml
結果
実例を参照。
