forked from LambdaTest/java-testng-selenium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestNGTodo1.java
287 lines (250 loc) · 13.5 KB
/
TestNGTodo1.java
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.lambdatest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNGTodo1 {
private RemoteWebDriver driver;
private String testStatus;
@BeforeMethod
public void setup(Method m, ITestContext ctx) throws MalformedURLException {
String username = "deepanshulambdatest";
String accessKey = "SMKwpdntRhBpUbdLrUqk0tU0TjAZquV9kz0YxnOWUo56EaqtQw";
String hub = "@hub.lambdatest.com/wd/hub";
DesiredCapabilities capabilities = new DesiredCapabilities();
HashMap<String, Object> ltOptions = new HashMap<>();
ltOptions.put("platform", "windows 11");
ltOptions.put("browserName", "chrome");
ltOptions.put("version", "125");
ltOptions.put("build", "Qmetry test case build");
ltOptions.put("console", true);
ltOptions.put("visual", true);
ltOptions.put("network", true);
ltOptions.put("name", "SCRUM-TC-");
ltOptions.put("w3c", true);
capabilities.setCapability("lt:options", ltOptions);
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + hub), capabilities);
}
@Test
public void basicTest() {
driver.get("https://lambdatest.com/");
testStatus = "passed";
}
private String getGETApiResponse(String apiUrl, String requestPropertyKey, String requestPropertyValue) throws IOException {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("accept", "application/json");
connection.setRequestProperty(requestPropertyKey, requestPropertyValue);
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
return response.toString();
}
}
private String getPOSTApiResponse(String apiUrl, String requestBody) throws IOException {
URL url = new URL(apiUrl);
String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("apiKey", apiKey);
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
StringBuilder errorResponse = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
errorResponse.append(inputLine);
}
throw new IOException("Error: " + errorResponse.toString());
}
}
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
return response.toString();
}
}
private void getPUTApiResponse(String apiUrl, BigInteger dynamicId) throws IOException {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
JSONObject jsonBody = new JSONObject();
jsonBody.put("executionResultId", dynamicId);
String requestBody = jsonBody.toString();
String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75";
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("apiKey", apiKey);
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
}
private String getAddExecutionResultResponse(String apiUrl, String userTestResultStatus) throws IOException {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
JSONObject jsonBody = new JSONObject();
jsonBody.put("color", userTestResultStatus.equals("passed") ? "#BAB86C" : "#800000");
jsonBody.put("name", userTestResultStatus);
jsonBody.put("description", "Test result updated according to the USER's test case result");
String requestBody = jsonBody.toString();
String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75";
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("apiKey", apiKey);
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
BufferedReader in;
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
} else {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
private Map<String, String> getStatusIndFromJsonResponse(String jsonResponse) {
JSONObject jsonObject = new JSONObject(jsonResponse);
JSONObject dataObject = jsonObject.getJSONObject("data");
String testName = dataObject.optString("name", "unknown");
String status = dataObject.optString("status_ind", "unknown");
// Create a map to hold the results
Map<String, String> resultMap = new HashMap<>();
resultMap.put("testName", testName);
resultMap.put("status", status);
return resultMap; // Return the map
}
private BigInteger getExecutionResultUsersStatus(String jsonResponse) {
JSONObject jsonObject = new JSONObject(jsonResponse);
return jsonObject.optBigInteger("id", BigInteger.valueOf(0));
}
private JSONArray getSearchTestCycleResponse(String jsonResponse) {
JSONObject jsonObject = new JSONObject(jsonResponse);
return jsonObject.getJSONArray("data");
}
@AfterMethod
public void tearDown() {
try {
if (driver != null) {
driver.executeScript("lambda-status=" + testStatus);
SessionId session = driver.getSessionId();
String sessionApiUrl = "https://api.lambdatest.com/automation/api/v1/sessions/" + session;
String authKey = "Basic ZGVlcGFuc2h1bGFtYmRhdGVzdDpTTUt3cGRudFJoQnBVYmRMclVxazB0VTBUakFacXVWOWt6MFl4bk9XVW81NkVhcXRRdw==";
String response = getGETApiResponse(sessionApiUrl, "Authorization", authKey);
Map<String, String> result = getStatusIndFromJsonResponse(response);
String userTestResultStatus = result.get("status");
String userTestResultName = result.get("testName");
// POST API for getting test cycle
String searchTestCycleApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/testcycles/search/";
String searchTestCycleResponse = getPOSTApiResponse(searchTestCycleApiUrl, "{\"filter\":{\"projectId\":10000}}");
JSONArray searchTestCycleResult = getSearchTestCycleResponse(searchTestCycleResponse);
String testCycleId = "";
for (int i = 0; i < searchTestCycleResult.length(); i++) {
JSONObject obj = searchTestCycleResult.getJSONObject(i);
String key = obj.getString("key");
if ("SCRUM-TR-9".equals(key)) {
String id = obj.getString("id");
testCycleId = id;
break;
}
}
// POST API for get linked test cases of test cycle
String getLinkedTestCasesApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/testcycles/" + testCycleId + "/testcases/search/";
String linkedTestCasesResponse = getPOSTApiResponse(getLinkedTestCasesApiUrl, "{\n" +
" \"filter\": {\n" +
" \"status\": [\n" +
" \"In Progress\",\n" +
" \"To Do\",\n" +
" \"Passed\",\n" +
" \"Failed\",\n" +
" \"Blocked\",\n" +
" \"Not Executed\"\n" +
" ]\n" +
" }\n" +
"}");
JSONArray linkedTestCasesResult = getSearchTestCycleResponse(linkedTestCasesResponse);
BigInteger testCaseExecutionId = BigInteger.valueOf(0);
for (int i = 0; i < linkedTestCasesResult.length(); i++) {
JSONObject obj = linkedTestCasesResult.getJSONObject(i);
String key = obj.getString("key");
if (userTestResultName.equals(key)) {
BigInteger id = BigInteger.valueOf(obj.getLong("testCaseExecutionId"));
System.out.println("----------LT id-----------" + id);
testCaseExecutionId = id;
break;
}
}
// JSONObject linkedTestCaseResponseObj = linkedTestCasesResult.getJSONObject(0);
// BigInteger testCaseExecutionId = linkedTestCaseResponseObj.getBigInteger("testCaseExecutionId");
// GET execution result
String getExecutionResultApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/projects/10000/execution-results";
String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75";
String getExecutionResultResponse = getGETApiResponse(getExecutionResultApiUrl, "apiKey", apiKey);
JSONArray jsonArray = new JSONArray(getExecutionResultResponse);
BigInteger executionResultId = BigInteger.valueOf(0);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject executionResultResponseObj = jsonArray.getJSONObject(i); // get the individual obj
String name = executionResultResponseObj.getString("name");
if (userTestResultStatus.equals(name)) {
executionResultId = executionResultResponseObj.getBigInteger("id");
break;
}
}
if (executionResultId.equals(BigInteger.valueOf(0))) {
String addExecutionResultApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/projects/10000/execution-results";
String addExecutionResultResponse = getAddExecutionResultResponse(addExecutionResultApiUrl, userTestResultStatus);
executionResultId = getExecutionResultUsersStatus(addExecutionResultResponse);
}
String updateTestCaseExecutionApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/testcycles/" + testCycleId + "/testcase-executions/" + testCaseExecutionId;
getPUTApiResponse(updateTestCaseExecutionApiUrl, executionResultId);
}
} catch (Exception e) {
System.out.println("----------EXCEPTIONS-----------" + e);
} finally {
if (driver != null) {
driver.quit(); // Ensure driver quits
}
}
}
}