Open Jacobvu84 opened 4 years ago
DeviceManage Class
package jacob.vu.appium;
import jacob.vu.Manager;
public class DeviceManager extends Manager{
// TODO: something
}
package jacob.vu;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import jacob.vu.screenplay.models.Resource;
public abstract class Manager {
private static final Gson gson = new Gson();
public static ManagerBuilder manage(Resource resource) {
return new ManagerBuilder(resource);
}
public static class ManagerBuilder {
private Map<String, Map<String, String>> records;
private Resource resource;
public ManagerBuilder(Resource resource) {
this.resource = resource;
this.records = getAllRecords();
}
public Map<String, Map<String, String>> getAllRecords() {
if (records == null) {
records = read(
System.getProperty("user.dir") + "/src/test/resources/" + this.resource.getResource(),
new TypeToken<Map<String, Map<String, String>>>() {
}.getType());
}
return records;
}
public Map<String, String> findMetaDataByKey(String keyValue) {
return records.get(keyValue);
}
}
@SuppressWarnings("unchecked")
static <T extends Map<?, ?>> T read(String prefixName, Type type) {
Map<String, Object> data;
try {
data = readData(prefixName, type);
return (T) data;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unused")
private static Map<String, Object> readData(String resourceName, Type type) throws FileNotFoundException {
InputStream is = new FileInputStream(resourceName);
if (is == null) {
return new HashMap<>();
}
Reader reader = new InputStreamReader(is, Charset.defaultCharset());
return gson.fromJson(reader, type);
}
}
package jacob.vu.screenplay.models;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.openqa.selenium.remote.DesiredCapabilities;
import jacob.vu.appium.DeviceManager;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
public class User {
private final String name;
public static boolean noReset;
public EndUser(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
// Device Manager
public Map<String, Map<String, String>> getAllMetadataDevices() {
return DeviceManager.manage(Resource.DEVICES).getAllRecords();
}
public Map<String, String> getMetadataDeviceByUdid() {
return DeviceManager.manage(Resource.DEVICES).findMetaDataByKey(getUDIDOfActor());
}
public String getAutomationName() {
return getMetadataDeviceByUdid().get("automationName");
}
public String getPlatformName() {
return getMetadataDeviceByUdid().get("platformName");
}
public String getUDIDOfDevice() {
return getMetadataDeviceByUdid().get("udid");
.....
Resource.DEVICES is src/test/resources/devices.json
Json file to store device information