com.testvagrant.optimus.core.parser.MobileTestFeedParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optimus-lite Show documentation
Show all versions of optimus-lite Show documentation
Optimus Lite API to manage test devices and create appium driver based on platform
package com.testvagrant.optimus.core.parser;
import com.testvagrant.optimus.commons.AppFinder;
import com.testvagrant.optimus.commons.PortGenerator;
import com.testvagrant.optimus.commons.SystemProperties;
import com.testvagrant.optimus.commons.filehandlers.GsonParser;
import com.testvagrant.optimus.commons.filehandlers.TestFeedJsonParser;
import com.testvagrant.optimus.core.exceptions.NoTestFeedException;
import com.testvagrant.optimus.core.exceptions.TestFeedTargetsNotFoundException;
import com.testvagrant.optimus.core.mobile.OptimusServerFlag;
import com.testvagrant.optimus.core.models.OptimusSupportedPlatforms;
import com.testvagrant.optimus.core.models.mobile.*;
import com.testvagrant.optimus.core.models.web.SiteConfig;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.flags.AndroidServerFlag;
import io.appium.java_client.service.local.flags.GeneralServerFlag;
import io.appium.java_client.service.local.flags.IOSServerFlag;
import io.appium.java_client.service.local.flags.ServerArgument;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class MobileTestFeedParser {
private final MobileTestFeed mobileTestFeed;
private final MobileTestFeedDetails mobileTestFeedDetails;
private final String testFeedName;
public MobileTestFeedParser(String testFeedName) {
this.testFeedName = testFeedName;
mobileTestFeed = getTestFeed(testFeedName);
mobileTestFeedDetails = getMobileTestFeedDetails(mobileTestFeed);
}
public OptimusSupportedPlatforms getPlatform() {
try {
return OptimusSupportedPlatforms.valueOf(SystemProperties.TARGET);
} catch (Exception ex) {
System.out.println("Target not specified. Setting target to ANDROID...");
return OptimusSupportedPlatforms.ANDROID;
}
}
public Map getServerArgumentsMap() {
return addServerArguments(
GeneralServerFlag.values(),
IOSServerFlag.values(),
AndroidServerFlag.values(),
OptimusServerFlag.values());
}
public SiteConfig getSiteConfig() {
return mobileTestFeed.getSiteConfig();
}
public boolean isMobileWeb() {
return mobileTestFeedDetails
.getDesiredCapabilities()
.containsKey(MobileCapabilityType.BROWSER_NAME);
}
private Map addServerArguments(ServerArgument[]... values) {
Map serverArgumentsMap = new HashMap<>();
Map serverFlags =
Arrays.stream(values)
.flatMap(Arrays::stream)
.collect(Collectors.toMap(ServerArgument::getArgument, item -> item));
mobileTestFeed.getServerArguments().parallelStream()
.forEach(
arg -> {
String[] serverArg =
arg.contains("=") ? arg.trim().split("=") : arg.trim().split(" ");
if (serverFlags.containsKey(serverArg[0])) {
ServerArgument argument = serverFlags.get(serverArg[0]);
String value = serverArg.length == 2 ? serverArg[1] : "";
serverArgumentsMap.put(argument, value);
}
});
return serverArgumentsMap;
}
public DesiredCapabilities getDesiredCapabilities() {
Map generalCapabilities =
getPlatform().equals(OptimusSupportedPlatforms.IOS)
? getIOSCapabilities().toDesiredCapabilities()
: getAndroidCapabilities().toDesiredCapabilities();
Map mergedCapabilities = mergeDesiredCapabilities(generalCapabilities);
Map desiredCapabilitiesMap = updateMandatoryDesiredCaps(mergedCapabilities);
return new DesiredCapabilities(desiredCapabilitiesMap);
}
public DeviceFilters getDeviceFilters() {
return mobileTestFeed.getDeviceFilters();
}
private AndroidOnlyCapabilities getAndroidCapabilities() {
GsonParser gsonParser = GsonParser.toInstance();
String capabilities = gsonParser.serialize(mobileTestFeedDetails.getDesiredCapabilities());
return gsonParser.deserialize(capabilities, AndroidOnlyCapabilities.class);
}
private IOSOnlyCapabilities getIOSCapabilities() {
GsonParser gsonParser = GsonParser.toInstance();
String capabilities = gsonParser.serialize(mobileTestFeedDetails.getDesiredCapabilities());
return gsonParser.deserialize(capabilities, IOSOnlyCapabilities.class);
}
private Map mergeDesiredCapabilities(Map desiredCapabilitiesMap) {
Map testFeedDesiredCapabilitiesMap =
mobileTestFeedDetails.getDesiredCapabilities();
testFeedDesiredCapabilitiesMap.entrySet().parallelStream()
.forEach(
entry -> {
if (!desiredCapabilitiesMap.containsKey(entry.getKey())) {
desiredCapabilitiesMap.put(entry.getKey(), entry.getValue());
}
});
return desiredCapabilitiesMap;
}
private Map updateMandatoryDesiredCaps(
Map desiredCapabilitiesMap) {
desiredCapabilitiesMap.put(CapabilityType.PLATFORM_NAME, getPlatform().name());
String appPath = getAppPath();
if (!appPath.isEmpty()) {
desiredCapabilitiesMap.put(MobileCapabilityType.APP, appPath);
}
if (getPlatform().equals(OptimusSupportedPlatforms.ANDROID)) {
desiredCapabilitiesMap.put(
AndroidMobileCapabilityType.SYSTEM_PORT,
PortGenerator.aRandomOpenPortOnAllLocalInterfaces());
}
return desiredCapabilitiesMap;
}
private String getAppPath() {
if (isMobileWeb()) return "";
if (Objects.isNull(mobileTestFeedDetails.getApp()) || mobileTestFeedDetails.getApp().isEmpty())
return "";
return mobileTestFeedDetails.getApp().contains(":")
? mobileTestFeedDetails.getApp()
: AppFinder.getInstance()
.getDefaultPath(mobileTestFeed.getAppDir(), mobileTestFeedDetails.getApp());
}
private MobileTestFeed getTestFeed(String testFeedName) {
if (testFeedName == null) {
throw new NoTestFeedException("mobileFeed");
}
TestFeedJsonParser jsonParser = new TestFeedJsonParser();
return jsonParser.deserialize(testFeedName, MobileTestFeed.class);
}
private MobileTestFeedDetails getMobileTestFeedDetails(MobileTestFeed mobileTestFeed) {
if (mobileTestFeed.getTargets().isEmpty())
throw new TestFeedTargetsNotFoundException(testFeedName);
return mobileTestFeed.getTargets().stream()
.filter(target -> target.getPlatform().equalsIgnoreCase(SystemProperties.TARGET))
.findFirst()
.orElse(mobileTestFeed.getTargets().get(0));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy