Jacobvu84 / serenity-screenplay-junit-appium

0 stars 0 forks source link

Actor Pools Manager #15

Open Jacobvu84 opened 5 years ago

Jacobvu84 commented 5 years ago
{
        "Jacob": {
            "actor": "Jacob",
            "lineId": "jacob.line",
            "device": "xxyyyzzz",
            "platform": "Android",
            "country": "Vietnam",
            "phone": "0984848484"
        },
        "Bella": {
            "actor": "Bella",
            "device": "yyyxxxzzz",
            "platform": "iOS",
            "country": "Vietnam",
            "phone": "0984303104"
        }

}
Jacobvu84 commented 5 years ago
public class ActorManager extends Manager {
}

and

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
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.remote.DesiredCapabilities;

import jacob.vu.actors.ActorManager;

import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;

public class EndUser {

    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;
    }

    // Actor manager
    public Map<String, String> getMetaDataOfActor() {
        return ActorManager.manage(Resource.ACTORS).findMetaDataByKey(name);
    }

    public String getPhone() {
        return getMetaDataOfActor().get("phone");

    }

    public String getCountry() {
        return getMetaDataOfActor().getOrDefault("country", "Japan");
    }

    public String getVerificationCode() {
        return getMetaDataOfActor().getOrDefault("verificationCode", "123456");
    }

    public String getPassword() {
        return getMetaDataOfActor().getOrDefault("password", "JACOBVN2019");
    }

    public List<String> getFriends() {
        return Arrays.asList(getMetaDataOfActor().get("friends").split(","));
    }
.....
Jacobvu84 commented 5 years ago

Test

        EndUser endUser = new EndUser(Liner.JACOB.getName());

        System.out.println(endUser.getAlias());

         Map<String, Map<String, String>>  actorPool = new HashMap<String, Map<String, String>>();
         actorPool = ActorManager.manage(Resource.ACTORS).getAllRecords();

            for (String name : actorPool.keySet()) {

                Map<String, String> actor = actorPool.get(name); 

                 if("312345164".equals(actor.get("phone")))

                     System.out.println(name);
            }