Skip to content

Commit 99aed79

Browse files
committed
Include basic tests using extent reports
1 parent 30da67f commit 99aed79

File tree

5 files changed

+186
-1
lines changed

5 files changed

+186
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ target
1111
build
1212
allure-results
1313
bin
14-
test-output
14+
test-output
15+
*.html

junit-vs-testng/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ dependencies {
8888
testImplementation("org.assertj:assertj-core:3.27.4")
8989
testImplementation("org.seleniumhq.selenium:selenium-java:4.35.0")
9090
testImplementation("io.github.artsok:rerunner-jupiter:2.1.6")
91+
testImplementation("com.aventstack:extentreports:5.1.2")
9192
}

junit-vs-testng/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<version>2.1.6</version>
5959
<scope>test</scope>
6060
</dependency>
61+
<dependency>
62+
<groupId>com.aventstack</groupId>
63+
<artifactId>extentreports</artifactId>
64+
<version>5.1.2</version>
65+
<scope>test</scope>
66+
</dependency>
6167
</dependencies>
6268

6369
<build>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* (C) Copyright 2025 Boni Garcia (https://bonigarcia.github.io/)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.github.bonigarcia.junit.selenium;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.jupiter.api.AfterAll;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.TestInfo;
27+
import org.openqa.selenium.OutputType;
28+
import org.openqa.selenium.TakesScreenshot;
29+
import org.openqa.selenium.WebDriver;
30+
import org.openqa.selenium.chrome.ChromeDriver;
31+
32+
import com.aventstack.extentreports.ExtentReports;
33+
import com.aventstack.extentreports.ExtentTest;
34+
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
35+
36+
class ExtentReportsJupiterTest {
37+
38+
static ExtentReports report;
39+
ExtentTest test;
40+
WebDriver driver;
41+
42+
@BeforeAll
43+
static void setupClass() {
44+
report = new ExtentReports();
45+
ExtentSparkReporter htmlReporter = new ExtentSparkReporter(
46+
"report-junit.html");
47+
report.attachReporter(htmlReporter);
48+
}
49+
50+
@BeforeEach
51+
void setup(TestInfo testInfo) {
52+
test = report.createTest(testInfo.getDisplayName());
53+
54+
driver = new ChromeDriver();
55+
}
56+
57+
@AfterEach
58+
void teardown() {
59+
test.addScreenCaptureFromBase64String(getScreenshotAsBase64());
60+
61+
driver.quit();
62+
}
63+
64+
@AfterAll
65+
static void teardownClass() {
66+
report.flush();
67+
}
68+
69+
@Test
70+
void testReporting1() {
71+
checkIndex(driver);
72+
}
73+
74+
@Test
75+
void testReporting2() {
76+
checkIndex(driver);
77+
}
78+
79+
void checkIndex(WebDriver driver) {
80+
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
81+
assertThat(driver.getTitle()).contains("Selenium WebDriver");
82+
}
83+
84+
String getScreenshotAsBase64() {
85+
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
86+
}
87+
88+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* (C) Copyright 2025 Boni Garcia (https://bonigarcia.github.io/)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.github.bonigarcia.testng.selenium;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.lang.reflect.Method;
22+
23+
import org.openqa.selenium.OutputType;
24+
import org.openqa.selenium.TakesScreenshot;
25+
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.chrome.ChromeDriver;
27+
import org.testng.annotations.AfterClass;
28+
import org.testng.annotations.AfterMethod;
29+
import org.testng.annotations.BeforeClass;
30+
import org.testng.annotations.BeforeMethod;
31+
import org.testng.annotations.Test;
32+
33+
import com.aventstack.extentreports.ExtentReports;
34+
import com.aventstack.extentreports.ExtentTest;
35+
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
36+
37+
public class ExtentReportsNGTest {
38+
39+
ExtentReports report;
40+
ExtentTest test;
41+
WebDriver driver;
42+
43+
@BeforeClass
44+
public void setupClass() {
45+
report = new ExtentReports();
46+
ExtentSparkReporter htmlReporter = new ExtentSparkReporter(
47+
"report-testng.html");
48+
report.attachReporter(htmlReporter);
49+
}
50+
51+
@BeforeMethod
52+
public void setup(Method method) {
53+
test = report.createTest(method.getName());
54+
55+
driver = new ChromeDriver();
56+
}
57+
58+
@AfterMethod
59+
public void teardown() {
60+
test.addScreenCaptureFromBase64String(getScreenshotAsBase64());
61+
62+
driver.quit();
63+
}
64+
65+
@AfterClass
66+
public void teardownClass() {
67+
report.flush();
68+
}
69+
70+
@Test
71+
public void testReporting1() {
72+
checkIndex(driver);
73+
}
74+
75+
@Test
76+
public void testReporting2() {
77+
checkIndex(driver);
78+
}
79+
80+
void checkIndex(WebDriver driver) {
81+
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
82+
assertThat(driver.getTitle()).contains("Selenium WebDriver");
83+
}
84+
85+
String getScreenshotAsBase64() {
86+
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)