Skip to content
Open
Show file tree
Hide file tree
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
58 changes: 57 additions & 1 deletion java-reporter-junit/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
allure-results/

.claude
CLAUDE.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

**/logs/
**/target/
**/log/
*.jar

**/testomatio.properties

### IntelliJ IDEA ###
**/.idea/
.idea
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
38 changes: 31 additions & 7 deletions java-reporter-junit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

This simple demo shows how Testomat.io Java reporter works in your project.

- Some will fail on purpose and other will be disabled for demo.
>NOTE: Some will fail on purpose and other will be disabled for demo.
---

## Installation

Expand All @@ -21,31 +22,54 @@ This simple demo shows how Testomat.io Java reporter works in your project.
3. Install dependencies with test skip

```sh
mvn clean install -DskipTests
mvn clean install -DskipTests ##Skip tests as there are some tests to fail on purpose, but you need to install the dependencies though.
```

For correct handling of tests structure, artifact handling and tests source code importing to the Testomat.io
you need to sync your tests with Testomat.io server.
It's easy to do with Java-Check-Tests CLI. Now there already is the **testomatio.jar** in the root of the project and you don't need
to download it. Just run the command:

```bash
java -jar testomatio.jar sync --apikey=... ##provide your project api key
```
> NOTE: more details about Java-Check-Tests you can find in its [repository](https://github.com/testomatio/java-check-tests)

---
## Configurations

**By default, the library runs with properties default values except `testomatio.api.key` and `testomatio.listening`**
**By default, the reporter runs when Testomat.io api key is provided in any way:**
Reporting can be disabled with property `testomatio.reporting.disable=1`

![properties image](img/properties.png)
1. As **ENV_VARIABLE** as `TESTOMATIO`
2. Add your project API key to the **testomatio.properties** file as `testomatio` (in the `src/main/resources/testomatio.properties`)
3. As JVM property on as `-Dtestomatio=...`
>NOTE: JVM property will take precedence over ENV variable, ENV variable will take precedence over property file.

Add your project API key to the `testomatio.properties` file ad `testomatio.api.key`
---
## Artifacts
The reporter supports test artifacts.
As you can see in the `src/test/java/artifact` folder, there are some tests that produce artifacts.
The artifacts are passed to the facade method `Testomatio.artifact(String ...)` as path to the file including extensions.
Artifacts handling is enabled by default and can be disabled with `testomatio.artifact.disable=1` property usage.
If artifacts are not disabled but there are no artifacts passed to the facade method - it won't affect reporting and is ok to use.

---
## Run

Run tests with

```bash
mvn test -Dtestomatio.api.key=tstmt_key #if you did not provide it in the `testomatio.properties` file
mvn test -Dtestomatio.api.key=tstmt_key #if you did not provide it in any other way
```

where `tstmt_key` is your Testomat.io key from a particular project.

As a result, you will see a run report in your Project tab -> Runs on Testomat.io.

<div align="center">
<div>
<img src="img/runReport.png" alt="demo report result png" style="max-width: 70%; max-height: 420px;">
</div>



Binary file modified java-reporter-junit/img/runReport.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions java-reporter-junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.9.2</junit.version>

<selenium.version>4.15.0</selenium.version>
<webdrivermanager.version>5.6.2</webdrivermanager.version>
</properties>

<dependencies>
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-junit</artifactId>
<version>0.7.2</version>
<version>0.8.0</version>
</dependency>

<dependency>
Expand All @@ -35,6 +36,18 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
13 changes: 3 additions & 10 deletions java-reporter-junit/src/main/resources/testomatio.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#Change to https://beta.testomat.io/ if you use it
testomatio.url=https://app.testomat.io/
#Particular project api key, starts with "tstmt_" (required, but can be provided here or as ENV_VARIABLE, or JVM property)
testomatio.api.key=tstmt_eP6i7hP8ukSYMe87QY44kG_au-Pj0o5M4A1757529796

#define the run title, or it will be default_run_title
testomatio.run.title=junit-example-run

#Particular project api key, starts with "tstmt_"
testomatio.api.key=

#enables/disables the reporting (remove value to disable)
testomatio.listening=true
testomatio.run.title=ExampleRunTitle
183 changes: 183 additions & 0 deletions java-reporter-junit/src/test/java/artifact/WebDriverArtifactTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package artifact;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.testomat.core.facade.Testomatio;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

@DisplayName("WebDriver Artifact Generation Tests")
public class WebDriverArtifactTest {

private WebDriver driver;

private String artifactDir;

@BeforeEach
void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-gpu");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
artifactDir = "target/test-artifacts";
createArtifactDirectory();
}

@AfterEach
void tearDown() {
if (driver != null) {
driver.quit();
}
}

@Test
@DisplayName("Should create artifacts with WebDriver")
void shouldCreateArtifactsWithWebDriver() {
try {
driver.get("https://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement searchBox = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
assertNotNull(searchBox, "Search box should be present");
searchBox.sendKeys("Selenium WebDriver");
searchBox.submit();
Thread.sleep(2000);
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
takeScreenshot("search_results_" + timestamp + ".png");
savePageSource("page_source_" + timestamp + ".html");
saveTestInfo("test_info.txt");
String currentTitle = driver.getTitle();
String currentUrl = driver.getCurrentUrl();
assertTrue(currentUrl.contains("search"), "URL should contain search results");
Testomatio.artifact(artifactDir + "/test_info.txt");
} catch (Exception e) {
fail("Test failed with exception: " + e.getMessage());
}
}

@Test
@DisplayName("Should create artifacts with WebDriver")
void shouldCreateArtifactsWithWebDriver1() {
try {
driver.get("https://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement searchBox = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
assertNotNull(searchBox, "Search box should be present");
searchBox.sendKeys("Selenium WebDriver");
searchBox.submit();
Thread.sleep(2000);
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
takeScreenshot("search_results.png");
savePageSource("page_source_" + timestamp + ".html");
saveTestInfo("test_info.txt");
String currentTitle = driver.getTitle();
String currentUrl = driver.getCurrentUrl();
assertTrue(currentUrl.contains("search"), "URL should contain search results");
Testomatio.artifact(artifactDir + "/test_info.txt", artifactDir + "/search_results.png");
} catch (Exception e) {
fail("Test failed with exception: " + e.getMessage());
}
}

@Test
@DisplayName("Should create artifacts with WebDriver")
void shouldCreateArtifactsWithWebDriver2() {
try {
driver.get("https://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement searchBox = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
assertNotNull(searchBox, "Search box should be present");
searchBox.sendKeys("Selenium WebDriver");
searchBox.submit();
Thread.sleep(2000);
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
takeScreenshot("search_results_" + timestamp + ".png");
savePageSource("page_source.html");
saveTestInfo("test_info.txt");
String currentTitle = driver.getTitle();
String currentUrl = driver.getCurrentUrl();
assertTrue(currentUrl.contains("search"), "URL should contain search results");
Testomatio.artifact(artifactDir + "/test_info.txt");
Testomatio.artifact(artifactDir + "/page_source.html");
} catch (Exception e) {
fail("Test failed with exception: " + e.getMessage());
}
}

private void createArtifactDirectory() {
try {
Path path = Paths.get(artifactDir);
if (!Files.exists(path)) {
Files.createDirectories(path);
}
} catch (IOException e) {
System.err.println("Failed to create artifact directory: " + e.getMessage());
}
}

private void takeScreenshot(String fileName) {
try {
TakesScreenshot screenshot = (TakesScreenshot) driver;
byte[] screenshotBytes = screenshot.getScreenshotAs(OutputType.BYTES);
Path screenshotPath = Paths.get(artifactDir, fileName);
Files.write(screenshotPath, screenshotBytes);
System.out.println("Screenshot saved: " + screenshotPath.toAbsolutePath());
} catch (IOException e) {
System.err.println("Failed to save screenshot: " + e.getMessage());
}
}

private void savePageSource(String fileName) {
try {
String pageSource = driver.getPageSource();
Path sourcePath = Paths.get(artifactDir, fileName);
Files.write(sourcePath, pageSource.getBytes());
System.out.println("Page source saved: " + sourcePath.toAbsolutePath());
} catch (IOException e) {
System.err.println("Failed to save page source: " + e.getMessage());
}
}

private void saveTestInfo(String fileName) {
try {
Path infoPath = Paths.get(artifactDir, fileName);
try (FileWriter writer = new FileWriter(infoPath.toFile())) {
writer.write("Test Execution Info\n");
writer.write("==================\n");
writer.write("Timestamp: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "\n");
writer.write("URL: " + driver.getCurrentUrl() + "\n");
writer.write("Title: " + driver.getTitle() + "\n");
writer.write("Browser: " + ((ChromeDriver) driver).getCapabilities().getBrowserName() + "\n");
writer.write("Browser Version: " + ((ChromeDriver) driver).getCapabilities().getBrowserVersion() + "\n");
writer.write("Window Size: " + driver.manage().window().getSize() + "\n");
}
System.out.println("Test info saved: " + infoPath.toAbsolutePath());
} catch (IOException e) {
System.err.println("Failed to save test info: " + e.getMessage());
}
}
}
3 changes: 2 additions & 1 deletion java-reporter-junit/src/test/java/library/AuthorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.library.Author;
import com.library.Book;
import com.library.Genre;
Expand Down Expand Up @@ -195,4 +196,4 @@ void testEqualsWithSameObject() {
void testEqualsWithDifferentClass() {
assertNotEquals("Not an author", author);
}
}
}
1 change: 1 addition & 0 deletions java-reporter-junit/src/test/java/library/BookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.library.Author;
import com.library.Book;
import com.library.Genre;
Expand Down
Loading