com.browserstack.UtilityMethods Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-tool Show documentation
Show all versions of gradle-tool Show documentation
A Gradle plugin to run espresso test with Browserstack SDK
The newest version!
package com.browserstack;
import com.browserstack.config.Constants;
import com.fasterxml.jackson.databind.ObjectMapper;
import javassist.ClassPool;
import javassist.NotFoundException;
import org.gradle.api.Project;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
public class UtilityMethods {
private static final browserstack.shaded.org.slf4j.Logger logger = com.browserstack.logger.BrowserstackLoggerFactory.getLogger(UtilityMethods.class);
private final static HashMap classNameFilePathMap = new HashMap<>();
private static Project project = null;
private static final HashMap> fileMap = new HashMap<>();
private static final HashMap frameworkDetail = new HashMap<>();
private static boolean cucumberSession = false;
public static boolean isCucumberSession() {
return cucumberSession;
}
public static void setCucumberSession(boolean cucumberSession) {
UtilityMethods.cucumberSession = cucumberSession;
}
public static HashMap> getFileMap() {return fileMap;}
public static void setProject(Project value) {
project = value;
}
public static Project getProject(){
return project;
}
public static HashMap getFrameworkDetail() {
return frameworkDetail;
}
public static void setFrameworkDetail(String jarPath) {
logger.info ("Jar Path Found " + jarPath);
if (isCucumberSession()) {return;}
if(!jarPath.endsWith(".jar")) {return;}
try {
Path path = Paths.get(jarPath);
int pathLength = path.getNameCount();
ArrayList splitPath = new ArrayList<>();
for (int i = 0; i < pathLength; i++) {
splitPath.add(path.getName(i).toString());
}
String jarName = splitPath.get(pathLength - 1).toLowerCase();
if (splitPath.contains("io.cucumber") && splitPath.contains("cucumber-core")) {
String frameworkVersion = jarName.replace("cucumber-core-", "").replace(".jar", "");
frameworkDetail.put("frameworkName", com.browserstack.config.Constants.FRAMEWORK_ESPRESSO_CUCUMBER_JUNIT4);
frameworkDetail.put("frameworkVersion", frameworkVersion);
setCucumberSession(true);
} else if (splitPath.contains("junit") && splitPath.get(pathLength - 1).contains("junit-4")) {
String frameworkVersion = jarName.replace("junit-", "").replace(".jar", "");
frameworkDetail.put("frameworkName", Constants.FRAMEWORK_ESPRESSO_JUNIT4);
frameworkDetail.put("frameworkVersion", frameworkVersion);
setCucumberSession(false);
}
if(splitPath.contains("browserstack-java-sdk")) {
String version = jarName.replace("browserstack-java-sdk-", "").replace(".jar", "");
frameworkDetail.put("sdkVersion", version);
}
} catch (Throwable e) {
logger.error("Error in discovering framework details: " + com.browserstack.utils.UtilityMethods.getStackTraceAsString(e));
}
}
public static void insertRequiredClassPath(List jarPaths, ClassPool pool) throws NotFoundException {
for(String jarPath : jarPaths) {
String jarName = new File(jarPath).getName().toLowerCase();
if ((jarName.startsWith("gherkin-")
|| jarName.startsWith("cucumber-core-")
|| jarName.startsWith("browserstack-java-sdk"))
&& jarPath.endsWith(".jar")) {
pool.insertClassPath(jarPath);
logger.info("Adding Class Path: " + jarPath);
}
setFrameworkDetail(jarPath);
}
}
public static void setFileMap() {
String directoryPath = getProject().getProjectDir().getAbsolutePath();
Path startPath = Paths.get(directoryPath);
ArrayList javaFiles = new ArrayList<>();
ArrayList featureFiles = new ArrayList<>();
String projectBuildDir = getProject().getLayout().getBuildDirectory().get().getAsFile().getAbsolutePath().toString();
try {
Files.walkFileTree(startPath, new SimpleFileVisitor() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
String filePath = file.toString();
if (filePath.endsWith(".java") && !filePath.contains(".gradle")) {
javaFiles.add(filePath);
}
if (filePath.endsWith(".feature") && !filePath.contains(projectBuildDir)) {
featureFiles.add(filePath);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
logger.error("Failed to access file: " + file.toString());
return FileVisitResult.CONTINUE;
}
});
fileMap.put("javaFiles", javaFiles);
fileMap.put("featureFiles", featureFiles);
} catch (Throwable e) {
logger.error("Error while creating file map: " + com.browserstack.utils.UtilityMethods.getStackTraceAsString(e));
}
}
public static void setClassNameFilePathMap() {
// className: { absFilePath }
HashMap> filesList = getFileMap();
ArrayList javaFiles = filesList.getOrDefault("javaFiles", new ArrayList<>());
for (String javaFile: javaFiles) {
ArrayList classList = ClassExtractor.extract(javaFile);
for (String fullClassName: classList) {
classNameFilePathMap.put(fullClassName, javaFile);
}
}
}
public static String getClassNameFilePathMap() {
try{
return new ObjectMapper().writeValueAsString(classNameFilePathMap);
}catch (Throwable e){
logger.error("Failed to get classNameFilePathMap as string: " + com.browserstack.utils.UtilityMethods.getStackTraceAsString(e));
}
return "{}";
}
public static String getFeatureFileList() {
try{
return new ObjectMapper().writeValueAsString(getFileMap().getOrDefault("featureFiles", new ArrayList<>()));
} catch (Throwable e) {
logger.error("Failed to get feature file list as string: " + com.browserstack.utils.UtilityMethods.getStackTraceAsString(e));
}
return "[]";
}
public static String sanitizeJsonString(String jsonString) {
return jsonString
.replace("\"", "\\\"")
.replace("\\/", "\\\\/");
}
}