Skip to content

Instantly share code, notes, and snippets.

@saikrishna321
Created April 22, 2016 14:53
Show Gist options
  • Select an option

  • Save saikrishna321/da28b258d7ec083d024385492c012fa2 to your computer and use it in GitHub Desktop.

Select an option

Save saikrishna321/da28b258d7ec083d024385492c012fa2 to your computer and use it in GitHub Desktop.
package com.appium.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by saikrisv on 17/03/16.
*/
public class ImageUtils {
public List<String> fileList = new ArrayList<>();
public void wrapDeviceFrames(String deviceFrame, String deviceScreenToBeFramed, String framedDeviceScreen)
throws InterruptedException, IOException, IM4JavaException {
IMOperation op = new IMOperation();
ConvertCmd cmd = new ConvertCmd();
op.addImage(deviceFrame);
op.addImage(deviceScreenToBeFramed);
op.gravity("center");
op.composite();
op.opaque("none");
op.addImage(framedDeviceScreen);
cmd.run(op);
}
@Test
public void testApp() throws IOException {
File dir = new File(System.getProperty("user.dir")+"/target/screenshot/android/");
/*System.out.println("Getting all files in " + dir.getCanonicalPath() + " including those in subdirectories");
List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
JsonArray mainObj = new JsonArray();
JsonObject jsonObject = new JsonObject();
String deviceName;
FileWriter file1 = new FileWriter("test.json");
for (File file : files) {
JsonObject ja;
if(file.getCanonicalFile().toString().contains("results")){
ja=new JsonObject();
deviceName=file.getName().split("_")[0];
String deviceModel=file.getName().split("_")[1];
String screenName=file.getName().split("_")[2];
String imagePath=file.getPath().toString();
ja.addProperty("Device Name",deviceName);
ja.addProperty("Device Model",deviceModel);
ja.addProperty("Screen Name",screenName);
ja.addProperty("Image Path",imagePath);
jsonObject.add(deviceName, ja);
}
}
mainObj.add(jsonObject);
file1.write(mainObj.toString());
file1.flush();
file1.close();*/
JsonArray mainObj = new JsonArray();
JsonObject jsonObject = new JsonObject();
FileWriter file1 = new FileWriter("test.json");
JsonObject ja=new JsonObject();;
String deviceName;
displayDirectoryContents(dir);
for(int i = 0;i<fileList.size();i++){
System.out.println(fileList.get(i));
deviceName=fileList.get(i).split("_")[0];
String deviceModel=fileList.get(i).split("_")[1];
String screenName=fileList.get(i).split("_")[2];
String imagePath=fileList.get(i).toString();
ja.addProperty("Device Name",deviceName);
ja.addProperty("Device Model",deviceModel);
ja.addProperty("Screen Name",screenName);
ja.addProperty("Image Path",imagePath);
jsonObject.add(deviceName, ja);
}
mainObj.add(jsonObject);
file1.write(mainObj.toString());
file1.flush();
file1.close();
}
public List<String> displayDirectoryContents(File dir) {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
displayDirectoryContents(file);
} else {
if(file.getName().toString().contains("results.png")){
fileList.add(file.getName());
}
}
}
return fileList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment