-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.java
More file actions
195 lines (175 loc) · 6.88 KB
/
BaseTest.java
File metadata and controls
195 lines (175 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.cdTester.tests.selenium.web;
import java.io.*;
import java.time.Duration;
import java.util.HashMap;
import com.cdTester.utils.ConfigManager;
import io.qameta.allure.Allure;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BaseTest {
protected WebDriver driver;
protected WebDriverWait wait;
protected static String browser;
// protected String trustStorePassword = "seleniumkeystore";
protected static ConfigManager config;
@BeforeAll
protected static void setUpSuite() {
File outputDir = new File("test-output");
if (!outputDir.exists()) {
outputDir.mkdirs();
}
config = ConfigManager.getInstance();
browser = (config.getBrowser() != null) ? config.getBrowser() : "chrome";
setEnvironmentInfo();
}
@BeforeEach
protected void allureBeforeEachReporter() {
Allure.parameter("Browser", System.getProperty("browser", "chrome"));
}
public static void setEnvironmentInfo() {
String resultsDir = "target/allure-results";
// Create directory if it doesn't exist
File directory = new File(resultsDir);
if (!directory.exists()) {
directory.mkdirs();
}
// Write environment.properties (this shows in the report)
try (PrintWriter writer = new PrintWriter(new FileWriter(resultsDir + "/environment.properties"))) {
writer.println("Environment=" + System.getProperty("env", "qa"));
writer.println("Browser=" + System.getProperty("browser", "chrome"));
writer.println("Headless=" + System.getProperty("headless", "false"));
writer.println("Java.Version=" + System.getProperty("java.version"));
writer.println("OS=" + System.getProperty("os.name") + " " + System.getProperty("os.version"));
writer.println("User=" + System.getProperty("user.name"));
writer.println("Test.Date=" + java.time.LocalDateTime.now().toString());
} catch (IOException e) {
System.err.println("Failed to write environment.properties: " + e.getMessage());
}
}
// protected WebDriver startBrowserDriver() {
// if (browser.equalsIgnoreCase("firefox")) {
// return startFirefoxDriver();
// }
// else {
// return startChromeDriver(1);
// }
// }
protected ChromeDriver startChromeDriver(int waitInSeconds) {
ChromeOptions options = Allure.step("ChromeOptions", step -> {
ChromeOptions option = new ChromeOptions();
option.setImplicitWaitTimeout(Duration.ofSeconds(waitInSeconds));
option.addArguments("disable-search-engine-choice-screen");
if (config.getEnv().equals("prod")) {
option.addArguments("--headless=new");
option.addArguments("--window-size=1200,800");
}
step.parameter("Options", option.toString());
return option;
});
return startChromeDriver(options);
}
protected ChromeDriver startChromeDriver(int waitInSeconds, String[] arguments, HashMap<String, Object> capabilities) {
ChromeOptions options = Allure.step("ChromeOptions", step -> {
ChromeOptions option = new ChromeOptions();
option.setImplicitWaitTimeout(Duration.ofSeconds(waitInSeconds));
for (String args : arguments ) {
option.addArguments(args);
}
for (String opt : capabilities.keySet()) {
option.setCapability(opt, capabilities.get(opt));
}
if (config.getEnv().equals("prod")) {
option.addArguments("--headless=new");
option.addArguments("--window-size=1200,800");
}
step.parameter("Options", option.toString());
return option;
});
return startChromeDriver(options);
}
protected ChromeDriver startChromeDriver(ChromeOptions options) {
Allure.step("Starting ChromeDriver", step -> {
driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(1200, 800));
step.parameter("Driver", driver.toString());
});
return (ChromeDriver) driver;
}
protected FirefoxDriver startFirefoxDriver() {
return startFirefoxDriver(new FirefoxOptions());
}
protected FirefoxDriver startFirefoxDriver(FirefoxOptions options) {
options.setImplicitWaitTimeout(Duration.ofSeconds(1));
driver = new FirefoxDriver(options);
return (FirefoxDriver) driver;
}
// protected static ChromeOptions getDefaultChromeOptions() {
// ChromeOptions options = new ChromeOptions();
// options.addArguments("--no-sandbox");
// return options;
// }
// protected static EdgeOptions getDefaultEdgeOptions() {
// EdgeOptions options = new EdgeOptions();
// options.addArguments("--no-sandbox");
// return options;
// }
// protected URL startStandaloneGrid() {
// int port = PortProber.findFreePort();
// try {
// Main.main(
// new String[] {
// "standalone",
// "--port",
// String.valueOf(port),
// "--selenium-manager",
// "true",
// "--enable-managed-downloads",
// "true",
// "--log-level",
// "WARNING"
// });
// return new URL("http://localhost:" + port);
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
// protected URL startStandaloneGridAdvanced() {
// int port = PortProber.findFreePort();
// try {
// System.setProperty("javax.net.ssl.trustStore", Path.of("src/test/resources/server.jks").toAbsolutePath().toString());
// System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
// System.setProperty("jdk.internal.httpclient.disableHostnameVerification", "true");
// Main.main(
// new String[] {
// "standalone",
// "--port",
// String.valueOf(port),
// "--selenium-manager",
// "true",
// "--enable-managed-downloads",
// "true",
// "--log-level",
// "WARNING",
// "--username",
// username,
// "--password",
// password,
// "--https-certificate",
// Path.of("src/test/resources/tls.crt").toAbsolutePath().toString(),
// "--https-private-key",
// Path.of("src/test/resources/tls.key").toAbsolutePath().toString()
// });
// return new URL("https://localhost:" + port);
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
}