Jacobvu84 / serenity-screenplay-junit-appium

0 stars 0 forks source link

Device Pools Manager #14

Open Jacobvu84 opened 5 years ago

Jacobvu84 commented 5 years ago

Json file to store device information

{
    "jacobvuiphone": {
        "automationName": "XCUITest",
        "platformName": "iOS",
        "udid": "jacobvuiphone",
        "deviceName": "iPhone X",
        "platformVersion": "12.4.1",
        "app": "BETA.ipa",
        "bundleId": "talk",
        "xcodeOrgId": "GFWWWSN",
        "xcodeSigningId": "iPhone Developer",
        "wdaLocalPort": "8100",
        "usePrebuiltWDA": true,
        "noReset": false,
        "newCommandTimeout": "6000"
    },
    "jacobvuandroid": {
        "automationName": "UiAutomator2",
        "platformName": "Android",
        "udid": "jacobandroid",
        "deviceName": "Galaxy A70",
        "platformVersion": "9",
        "appPackage": "android",
        "appActivity": "activity.SplashActivity",
        "app": "beta.apk",
        "noReset": false,
        "systemPort": "8290",
        "newCommandTimeout": "6000",
        "adbExecTimeout": "6000",
        "autoGrantPermissions": true,
        "chromedriverExecutable": "drivers\chromedriver"
    }
}
Jacobvu84 commented 5 years ago

DeviceManage Class

package jacob.vu.appium;

import jacob.vu.Manager;

public class DeviceManager extends Manager{
    // TODO: something
}
Jacobvu84 commented 5 years ago
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);
    }

}
Jacobvu84 commented 5 years ago
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