Skip to content

Commit ab152de

Browse files
authored
Merge pull request #20 from Shahnawaz-Sk/master
changes for app automation capability service & report
2 parents 0951150 + 5e865af commit ab152de

25 files changed

+1366
-39
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ plugins {
3030

3131
group = 'org.jenkins-ci.plugins'
3232

33-
version = '1.20.5'
33+
version = '1.20.6'
3434

3535
description = 'LambdaTest Plugin is used to run automated selenium tests on LambdaTest Cloud'
3636

src/main/java/com/lambdatest/jenkins/credential/MagicPlugCredentialsImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.Collections;
55
import java.util.List;
6+
import java.util.logging.Logger;
67

78
import javax.annotation.CheckForNull;
89
import javax.annotation.Nullable;
@@ -32,15 +33,15 @@ public class MagicPlugCredentialsImpl extends BaseStandardCredentials implements
3233
private final String userName;
3334
private final Secret accessToken;
3435

36+
private static final Logger logger = Logger.getLogger(MagicPlugCredentialsImpl.class.getName());
37+
3538
@DataBoundConstructor
3639
public MagicPlugCredentialsImpl(CredentialsScope scope, String id, String description, String userName,
3740
String accessToken) throws Exception {
3841
super(scope, id, description);
3942
try {
40-
System.out.println("MagicPlugCredentialsImpl");
4143
this.userName = userName;
4244
this.accessToken = Secret.fromString(accessToken);
43-
System.out.println("Here We can Verify Credentials Also before Adding");
4445
if (!CapabilityService.isValidUser(userName, accessToken)) {
4546
throw new Exception("Invalid username and access Token");
4647
}
@@ -82,7 +83,6 @@ public String getCredentialsPage() {
8283

8384
public FormValidation doVerifyCredentials(@QueryParameter("userName") final String userName,
8485
@QueryParameter("accessToken") final String accessToken) throws IOException, ServletException {
85-
System.out.println(userName + ":" + accessToken);
8686
if (StringUtils.isBlank(userName) || StringUtils.isBlank(accessToken)) {
8787
return FormValidation.error("Please enter valid username and authKey");
8888
}

src/main/java/com/lambdatest/jenkins/freestyle/MagicPlugBuildWrapper.java

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.lambdatest.jenkins.freestyle;
22

3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
35
import java.io.IOException;
46
import java.io.Serializable;
57
import java.lang.reflect.Field;
@@ -25,6 +27,7 @@
2527
import com.lambdatest.jenkins.freestyle.api.Constant;
2628
import com.lambdatest.jenkins.freestyle.api.auth.UserAuthResponse;
2729
import com.lambdatest.jenkins.freestyle.api.service.CapabilityService;
30+
import com.lambdatest.jenkins.freestyle.api.service.AppAutomationCapabilityService;
2831
import com.lambdatest.jenkins.freestyle.data.LocalTunnel;
2932
import com.lambdatest.jenkins.freestyle.service.LambdaTunnelService;
3033
import com.lambdatest.jenkins.freestyle.service.LambdaWebSocketTunnelService;
@@ -43,6 +46,8 @@
4346
public class MagicPlugBuildWrapper extends BuildWrapper implements Serializable {
4447

4548
private List<JSONObject> seleniumCapabilityRequest;
49+
private List<JSONObject> appAutomationCapabilityRequest;
50+
private String appId;
4651
private String credentialsId;
4752
private String username;
4853
private Secret accessToken;
@@ -65,7 +70,7 @@ public class MagicPlugBuildWrapper extends BuildWrapper implements Serializable
6570
private static final Logger logger = Logger.getLogger(MagicPlugBuildWrapper.class.getName());
6671

6772
@DataBoundConstructor
68-
public MagicPlugBuildWrapper(StaplerRequest req, @CheckForNull List<JSONObject> seleniumCapabilityRequest,
73+
public MagicPlugBuildWrapper(StaplerRequest req, @CheckForNull List<JSONObject> seleniumCapabilityRequest, @CheckForNull List<JSONObject> appAutomationCapabilityRequest,
6974
@CheckForNull String credentialsId, String choice, boolean useLocalTunnel, LocalTunnel localTunnel,
7075
ItemGroup context) throws Exception {
7176
try {
@@ -77,6 +82,13 @@ public MagicPlugBuildWrapper(StaplerRequest req, @CheckForNull List<JSONObject>
7782
validateTestInput(seleniumCapabilityRequest);
7883
this.seleniumCapabilityRequest = seleniumCapabilityRequest;
7984
}
85+
if (appAutomationCapabilityRequest == null) {
86+
// prevent null pointer
87+
this.appAutomationCapabilityRequest = new ArrayList<JSONObject>();
88+
} else {
89+
validateTestInput(appAutomationCapabilityRequest);
90+
this.appAutomationCapabilityRequest = appAutomationCapabilityRequest;
91+
}
8092
// Setting up credentials in both case if input capabilities are there or not
8193
this.choice = choice;
8294
setLambdaTestCredentials(credentialsId, context);
@@ -160,8 +172,13 @@ public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener l
160172
}
161173

162174
// Create Grid URL
163-
this.gridURL = CapabilityService.buildHubURL(this.username, this.accessToken.getPlainText(),"production");
164-
logger.info(this.gridURL);
175+
if (!CollectionUtils.isEmpty(seleniumCapabilityRequest)) {
176+
this.gridURL = CapabilityService.buildHubURL(this.username, this.accessToken.getEncryptedValue(),"production");
177+
} else if (!CollectionUtils.isEmpty(appAutomationCapabilityRequest)) {
178+
logger.info("appAutomationCR : " + appAutomationCapabilityRequest);
179+
this.gridURL = AppAutomationCapabilityService.appAutomationBuildHubURL(this.username, this.accessToken.getEncryptedValue(),"production");
180+
}
181+
logger.info("grid URL : " + this.gridURL);
165182
return new MagicPlugEnvironment(build);
166183
}
167184

@@ -207,7 +224,17 @@ public void buildEnvVars(Map<String, String> env) {
207224
env.put(Constant.LT_BROWSER_VERSION, seleniumCapability.getString(Constant.BROWSER_VERSION));
208225
env.put(Constant.LT_RESOLUTION, seleniumCapability.getString(Constant.RESOLUTION));
209226
}
227+
if (!CollectionUtils.isEmpty(appAutomationCapabilityRequest)) {
228+
JSONObject appAutomationCapability = appAutomationCapabilityRequest.get(0);
229+
env.put(Constant.LT_PLATFORM_NAME, appAutomationCapability.getString(Constant.PLATFORM_NAME));
230+
env.put(Constant.LT_BRAND_NAME, appAutomationCapability.getString(Constant.BRAND_NAME));
231+
env.put(Constant.LT_DEVICE_NAME, appAutomationCapability.getString(Constant.DEVICE_NAME));
232+
env.put(Constant.LT_DEVICE_VERSION, appAutomationCapability.getString(Constant.DEVICE_VERSION));
233+
env.put(Constant.LT_APP_ID, appAutomationCapability.getString(Constant.APP_ID));
234+
}
210235
env.put(Constant.LT_BROWSERS, createBrowserJSON(seleniumCapabilityRequest));
236+
env.put(Constant.LT_BRANDS, createBrandJSON(appAutomationCapabilityRequest));
237+
env.put(Constant.LT_DEVICES, createDeviceJSON(appAutomationCapabilityRequest));
211238
env.put(Constant.LT_GRID_URL, gridURL);
212239
env.put(Constant.LT_BUILD_NAME, buildname);
213240
env.put(Constant.LT_BUILD_NUMBER, buildnumber);
@@ -246,40 +273,95 @@ private String createBrowserJSON(List<JSONObject> seleniumCapabilityRequests) {
246273
}
247274
return config;
248275
}
276+
private String createDeviceJSON(List<JSONObject> appAutomationCapabilityRequests) {
277+
String config = Constant.NOT_AVAILABLE;
278+
try {
279+
ObjectMapper objectMapper = new ObjectMapper();
280+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
281+
config = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(appAutomationCapabilityRequests);
282+
} catch (JsonProcessingException e) {
283+
logger.warning(e.getMessage());
284+
}
285+
return config;
286+
}
287+
288+
private String createBrandJSON(List<JSONObject> appAutomationCapabilityRequests) {
289+
String config = Constant.NOT_AVAILABLE;
290+
try {
291+
ObjectMapper objectMapper = new ObjectMapper();
292+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
293+
config = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(appAutomationCapabilityRequests);
294+
} catch (JsonProcessingException e) {
295+
logger.warning(e.getMessage());
296+
}
297+
return config;
298+
}
299+
249300

250301
@Override
251302
public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
252303
/*
253304
* Runs after the build
254305
*/
306+
String buildnumber = String.valueOf(build.getNumber());
307+
255308
try {
256309
logger.info("tearDown");
257-
int x=1;
258-
while(x!=-1) {
259-
x=stopTunnel();
310+
int result = stopTunnel(buildnumber, build.getWorkspace());
311+
312+
if (result == -1) {
313+
logger.info("Tunnel was not active.");
314+
} else {
315+
logger.info("Tunnel stopped successfully.");
260316
}
261317
} catch (Exception e) {
262-
logger.warning("Forcefully Tear Down due to :"+e.getMessage());
318+
logger.warning("Forcefully Tear Down due to: " + e.getMessage());
263319
if (tunnelProcess != null && tunnelProcess.isAlive()) {
264320
tunnelProcess.destroy();
265321
}
266322
}
267323
return super.tearDown(build, listener);
268-
}
269-
270-
private int stopTunnel() throws IOException, InterruptedException {
324+
}
325+
326+
private int stopTunnel(String buildnumber, FilePath workspacePath) throws IOException, InterruptedException {
271327
if (tunnelProcess != null && tunnelProcess.isAlive()) {
272-
logger.info("tunnel is active, going to stop tunnel binary");
328+
logger.info("Tunnel is active, going to stop the tunnel binary");
329+
273330
if (OSValidator.isWindows()) {
274331
tunnelProcess.destroy();
275332
logger.info("Windows Tunnel Stopped");
276333
return 10;
277334
}
278-
long tunnelProcessId=getPidOfProcess(tunnelProcess);
335+
336+
long tunnelProcessId = getPidOfProcess(tunnelProcess);
337+
338+
logger.info("Tunnel is active, with tunnelProcessId: " + tunnelProcessId);
339+
340+
if (tunnelProcessId == -1) {
341+
String pidFilePath = workspacePath + "/lambda-tunnel/" + buildnumber + ".pid";
342+
logger.info(pidFilePath);
343+
344+
try (BufferedReader br = new BufferedReader(new FileReader(pidFilePath))) {
345+
String pidString = br.readLine();
346+
347+
if (pidString != null) {
348+
long pid = Integer.parseInt(pidString.trim());
349+
stopTunnelProcessUsingPID(pid);
350+
return 10;
351+
} else {
352+
logger.info("PID not found in the file.");
353+
return -1;
354+
}
355+
} catch (IOException e) {
356+
logger.info("Error reading PID file: " + e.getMessage());
357+
return -1;
358+
}
359+
}
360+
279361
stopTunnelProcessUsingPID(tunnelProcessId);
280362
Thread.sleep(2000);
281363
return 10;
282-
}else {
364+
} else {
283365
logger.info("Tunnel Stopped");
284366
return -1;
285367
}
@@ -320,6 +402,21 @@ public List<JSONObject> getSeleniumCapabilityRequest() {
320402
public void setSeleniumCapabilityRequest(List<JSONObject> seleniumCapabilityRequest) {
321403
this.seleniumCapabilityRequest = seleniumCapabilityRequest;
322404
}
405+
public List<JSONObject> getAppAutomationCapabilityRequest() {
406+
return appAutomationCapabilityRequest;
407+
}
408+
409+
public void setAppAutomationCapabilityRequest(List<JSONObject> appAutomationCapabilityRequest) {
410+
this.appAutomationCapabilityRequest = appAutomationCapabilityRequest;
411+
}
412+
413+
public String getAppId() {
414+
return appId;
415+
}
416+
417+
public void setAppid(String appId) {
418+
this.appId = appId;
419+
}
323420

324421
public String getUsername() {
325422
return username;

0 commit comments

Comments
 (0)