Skip to content

Commit 49c578e

Browse files
committed
Update Selenium 4.29.0 support CDP 133
1 parent d6a8c0d commit 49c578e

File tree

5 files changed

+420
-1
lines changed

5 files changed

+420
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ ExtentReports/
88
logs/
99
target/
1010
test-output/
11-
reports/
1211
*.mp4
1312
*.avi
1413
allure-report/
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2022 Anh Tester
3+
* Automation Framework Selenium
4+
*/
5+
6+
package com.anhtester.reports;
7+
8+
import com.anhtester.constants.FrameworkConstants;
9+
import com.anhtester.driver.DriverManager;
10+
import com.anhtester.enums.Browser;
11+
import com.anhtester.helpers.FileHelpers;
12+
import com.anhtester.utils.BrowserInfoUtils;
13+
import com.anhtester.utils.LogUtils;
14+
import com.github.automatedowl.tools.AllureEnvironmentWriter;
15+
import com.google.common.collect.ImmutableMap;
16+
import io.qameta.allure.Allure;
17+
import io.qameta.allure.Attachment;
18+
import org.openqa.selenium.TakesScreenshot;
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.IOException;
23+
24+
import static com.anhtester.constants.FrameworkConstants.EXPORT_VIDEO_PATH;
25+
import static org.openqa.selenium.OutputType.BYTES;
26+
27+
public class AllureManager {
28+
29+
private AllureManager() {
30+
}
31+
32+
public static void setAllureEnvironmentInformation() {
33+
AllureEnvironmentWriter.allureEnvironmentWriter(
34+
ImmutableMap.<String, String>builder().
35+
put("Test URL", FrameworkConstants.URL_CRM).
36+
put("Target Execution", FrameworkConstants.TARGET).
37+
put("Global Timeout", String.valueOf(FrameworkConstants.WAIT_DEFAULT)).
38+
put("Page Load Timeout", String.valueOf(FrameworkConstants.WAIT_PAGE_LOADED)).
39+
put("Headless Mode", FrameworkConstants.HEADLESS).
40+
put("Local Browser", String.valueOf(Browser.CHROME)).
41+
put("Remote URL", FrameworkConstants.REMOTE_URL).
42+
put("Remote Port", FrameworkConstants.REMOTE_PORT).
43+
build());
44+
45+
System.out.println("Allure Reports is installed.");
46+
47+
}
48+
49+
@Attachment(value = "Failed test Screenshot", type = "image/png")
50+
public static byte[] takeScreenshotToAttachOnAllureReport() {
51+
try {
52+
return ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(BYTES);
53+
} catch (Exception ex) {
54+
ex.getMessage();
55+
}
56+
return new byte[0];
57+
}
58+
59+
@Attachment(value = "Step Screenshot", type = "image/png")
60+
public static byte[] takeScreenshotStep() {
61+
try {
62+
return ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(BYTES);
63+
} catch (Exception ex) {
64+
ex.getMessage();
65+
}
66+
return new byte[0];
67+
}
68+
69+
@Attachment(value = "Browser Information", type = "text/plain")
70+
public static String addBrowserInformationOnAllureReport() {
71+
return BrowserInfoUtils.getOSInfo();
72+
}
73+
74+
75+
//Text attachments for Allure
76+
@Attachment(value = "{0}", type = "text/plain")
77+
public static String saveTextLog(String message) {
78+
return message;
79+
}
80+
81+
//HTML attachments for Allure
82+
@Attachment(value = "{0}", type = "text/html")
83+
public static String attachHtml(String html) {
84+
return html;
85+
}
86+
87+
public static void addAttachmentVideoAVI() {
88+
if (FrameworkConstants.VIDEO_RECORD.toLowerCase().trim().equals(FrameworkConstants.YES)) {
89+
try {
90+
//Get file Last Modified in folder
91+
File video = FileHelpers.getFileLastModified(EXPORT_VIDEO_PATH);
92+
if (video != null) {
93+
Allure.addAttachment("Video record AVI", "video/avi", new FileInputStream(video), "avi");
94+
} else {
95+
LogUtils.warn("Video record not found.");
96+
LogUtils.warn("Can not attachment Video in Allure reports");
97+
}
98+
99+
} catch (IOException e) {
100+
LogUtils.error("Can not attachment Video in Allure reports");
101+
e.printStackTrace();
102+
}
103+
}
104+
}
105+
106+
public static void addAttachmentVideoMP4() {
107+
try {
108+
//Get file Last Modified in folder
109+
File video = FileHelpers.getFileLastModified(EXPORT_VIDEO_PATH);
110+
//File video = new File("ExportData/Videos/SampleVideo.mp4");
111+
if (video != null) {
112+
Allure.addAttachment("Video record MP4", "video/mp4", new FileInputStream(video), "mp4");
113+
} else {
114+
LogUtils.warn("Video record not found.");
115+
LogUtils.warn("Can not attachment Video in Allure reports");
116+
}
117+
118+
} catch (IOException e) {
119+
LogUtils.error("Can not attachment Video in Allure reports");
120+
e.printStackTrace();
121+
}
122+
}
123+
124+
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package com.anhtester.reports;
2+
3+
import com.anhtester.constants.FrameworkConstants;
4+
import com.anhtester.driver.DriverManager;
5+
import com.anhtester.enums.AuthorType;
6+
import com.anhtester.enums.CategoryType;
7+
import com.anhtester.utils.*;
8+
import com.aventstack.extentreports.ExtentReports;
9+
import com.aventstack.extentreports.MediaEntityBuilder;
10+
import com.aventstack.extentreports.Status;
11+
import com.aventstack.extentreports.markuputils.Markup;
12+
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
13+
import com.aventstack.extentreports.reporter.configuration.Theme;
14+
import org.openqa.selenium.OutputType;
15+
import org.openqa.selenium.TakesScreenshot;
16+
//import tech.grasshopper.reporter.ExtentPDFReporter;
17+
18+
import java.io.File;
19+
import java.util.Objects;
20+
21+
public class ExtentReportManager {
22+
23+
private static ExtentReports extentReports;
24+
private static String link = "";
25+
26+
public static void initReports() {
27+
if (Objects.isNull(extentReports)) {
28+
extentReports = new ExtentReports();
29+
30+
if (FrameworkConstants.OVERRIDE_REPORTS.trim().equals(FrameworkConstants.NO)) {
31+
LogUtils.info("OVERRIDE EXTENT REPORTS = " + FrameworkConstants.OVERRIDE_REPORTS);
32+
link = FrameworkConstants.EXTENT_REPORT_FOLDER_PATH + File.separator + DateUtils.getCurrentDateTimeCustom("_") + "_" + FrameworkConstants.EXTENT_REPORT_FILE_NAME;
33+
LogUtils.info("Link Extent Report: " + link);
34+
} else {
35+
LogUtils.info("OVERRIDE EXTENT REPORTS = " + FrameworkConstants.OVERRIDE_REPORTS);
36+
link = FrameworkConstants.EXTENT_REPORT_FILE_PATH;
37+
LogUtils.info("Link Extent Report: " + link);
38+
}
39+
40+
// ExtentPDFReporter pdf = new ExtentPDFReporter("reports/ExtentReports/PdfReport.pdf");
41+
// try {
42+
// pdf.loadJSONConfig(new File("src/test/resources/pdf-config.json"));
43+
// } catch (IOException e) {
44+
// throw new RuntimeException(e);
45+
// }
46+
// extentReports.attachReporter(pdf);
47+
48+
ExtentSparkReporter spark = new ExtentSparkReporter(link);
49+
extentReports.attachReporter(spark);
50+
spark.config().setTheme(Theme.STANDARD);
51+
spark.config().setDocumentTitle(FrameworkConstants.REPORT_TITLE);
52+
spark.config().setReportName(FrameworkConstants.REPORT_TITLE);
53+
extentReports.setSystemInfo("Framework Name", FrameworkConstants.REPORT_TITLE);
54+
extentReports.setSystemInfo("Author", FrameworkConstants.AUTHOR);
55+
56+
LogUtils.info("Extent Reports is installed.");
57+
}
58+
}
59+
60+
public static void flushReports() {
61+
if (Objects.nonNull(extentReports)) {
62+
extentReports.flush();
63+
}
64+
ExtentTestManager.unload();
65+
ReportUtils.openReports(link);
66+
}
67+
68+
public static void createTest(String testCaseName) {
69+
ExtentTestManager.setExtentTest(extentReports.createTest(IconUtils.getBrowserIcon() + " " + testCaseName));
70+
//ExtentTestManager.setExtentTest(extentReports.createTest(testCaseName));
71+
}
72+
73+
public static void createTest(String testCaseName, String description) {
74+
ExtentTestManager.setExtentTest(extentReports.createTest(testCaseName, description));
75+
}
76+
77+
public static void removeTest(String testCaseName) {
78+
extentReports.removeTest(testCaseName);
79+
}
80+
81+
/**
82+
* Adds the screenshot.
83+
*
84+
* @param message the message
85+
*/
86+
public static void addScreenShot(String message) {
87+
String base64Image = "data:image/png;base64," + ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BASE64);
88+
89+
//Base64 from Screenshot of Selenium
90+
ExtentTestManager.getExtentTest().log(Status.INFO, MediaEntityBuilder.createScreenCaptureFromBase64String(base64Image).build());
91+
92+
//File Path from Screenshot of Java
93+
//ExtentTestManager.getExtentTest().log(Status.INFO, MediaEntityBuilder.createScreenCaptureFromPath(String.valueOf(CaptureHelpers.getScreenshotFile(message))).build());
94+
95+
}
96+
97+
/**
98+
* Adds the screenshot.
99+
*
100+
* @param status the status
101+
* @param message the message
102+
*/
103+
public static void addScreenShot(Status status, String message) {
104+
String base64Image = "data:image/png;base64," + ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BASE64);
105+
106+
//Base64 from Screenshot of Selenium
107+
ExtentTestManager.getExtentTest().log(status, MediaEntityBuilder.createScreenCaptureFromBase64String(base64Image).build());
108+
109+
//File Path from Screenshot of Java
110+
//ExtentTestManager.getExtentTest().log(status, MediaEntityBuilder.createScreenCaptureFromPath(CaptureHelpers.getScreenshotAbsolutePath(message)).build());
111+
112+
}
113+
114+
synchronized public static void addAuthors(AuthorType[] authors) {
115+
if (authors == null) {
116+
ExtentTestManager.getExtentTest().assignAuthor("ANHTESTER");
117+
} else {
118+
for (AuthorType author : authors) {
119+
ExtentTestManager.getExtentTest().assignAuthor(author.toString());
120+
}
121+
}
122+
}
123+
124+
// public static void addCategories(String[] categories) {
125+
synchronized public static void addCategories(CategoryType[] categories) {
126+
if (categories == null) {
127+
ExtentTestManager.getExtentTest().assignCategory("REGRESSION");
128+
} else {
129+
// for (String category : categories) {
130+
for (CategoryType category : categories) {
131+
ExtentTestManager.getExtentTest().assignCategory(category.toString());
132+
}
133+
}
134+
}
135+
136+
synchronized public static void addDevices() {
137+
ExtentTestManager.getExtentTest().assignDevice(BrowserInfoUtils.getBrowserInfo());
138+
// ExtentReportManager.getExtentTest()
139+
// .assignDevice(BrowserIconUtils.getBrowserIcon() + " : " + BrowserInfoUtils.getBrowserInfo());
140+
}
141+
142+
public static void logMessage(String message) {
143+
ExtentTestManager.getExtentTest().log(Status.INFO, message);
144+
}
145+
146+
public static void logMessage(Status status, String message) {
147+
ExtentTestManager.getExtentTest().log(status, message);
148+
}
149+
150+
public static void logMessage(Status status, Object message) {
151+
ExtentTestManager.getExtentTest().log(status, (Throwable) message);
152+
}
153+
154+
public static void pass(String message) {
155+
//LogUtils.info("ExtentReportManager class: " + ExtentTestManager.getExtentTest());
156+
ExtentTestManager.getExtentTest().pass(message);
157+
}
158+
159+
public static void pass(Markup message) {
160+
ExtentTestManager.getExtentTest().pass(message);
161+
}
162+
163+
public static void fail(String message) {
164+
ExtentTestManager.getExtentTest().fail(message);
165+
}
166+
167+
public static void fail(Object message) {
168+
ExtentTestManager.getExtentTest().fail((String) message);
169+
}
170+
171+
public static void fail(Markup message) {
172+
ExtentTestManager.getExtentTest().fail(message);
173+
}
174+
175+
public static void skip(String message) {
176+
ExtentTestManager.getExtentTest().skip(message);
177+
}
178+
179+
public static void skip(Markup message) {
180+
ExtentTestManager.getExtentTest().skip(message);
181+
}
182+
183+
public static void info(Markup message) {
184+
ExtentTestManager.getExtentTest().info(message);
185+
}
186+
187+
public static void info(String message) {
188+
ExtentTestManager.getExtentTest().info(message);
189+
}
190+
191+
public static void warning(String message) {
192+
ExtentTestManager.getExtentTest().log(Status.WARNING, message);
193+
}
194+
195+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.anhtester.reports;
2+
3+
import com.aventstack.extentreports.ExtentTest;
4+
5+
public class ExtentTestManager {
6+
7+
private static ThreadLocal<ExtentTest> extentTest = new ThreadLocal<>();
8+
9+
public static ExtentTest getExtentTest() {
10+
//System.out.println("ExtentTestManager class: " + extentTest.get());
11+
return extentTest.get();
12+
}
13+
14+
public static void setExtentTest(ExtentTest test) {
15+
extentTest.set(test);
16+
}
17+
18+
public static void unload() {
19+
extentTest.remove();
20+
}
21+
22+
}

0 commit comments

Comments
 (0)