Skip to content
Closed

As map #1356

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/io/appium/java_client/remote/MobileOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.remote.CapabilityType;

import java.net.URL;
import java.time.Duration;
import java.util.Map;
import java.util.stream.Collectors;

import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
import static java.util.Collections.unmodifiableMap;

public class MobileOptions<T extends MobileOptions<T>> extends MutableCapabilities {

Expand Down Expand Up @@ -512,4 +518,14 @@ protected T amend(String optionName, Object value) {
setCapability(optionName, value);
return (T) this;
}

@Override
public Map<String, Object> asMap() {
AcceptedW3CCapabilityKeys acceptedW3CCapabilityKeys = new AcceptedW3CCapabilityKeys();

return unmodifiableMap(super.asMap().entrySet().stream().collect(Collectors.toMap(
entry -> (acceptedW3CCapabilityKeys.test(entry.getKey()) || entry.getKey().startsWith(APPIUM_PREFIX)) ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this handler into a separate method for better readability. Also, in Appium server code we don't add prefix if a capability name already contains : character, not just the appium: prefix. Should we consider to do the same here?

entry.getKey() : APPIUM_PREFIX + entry.getKey(),
Map.Entry::getValue)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import static java.util.Collections.unmodifiableMap;
import static org.junit.Assert.*;

public class MobileOptionsTest {
Expand Down Expand Up @@ -106,4 +108,39 @@ public void setsMobileBooleanCapabilities() {
assertFalse(mobileOptions.doesFullReset());
assertFalse(mobileOptions.doesPrintPageSourceOnFindFailure());
}

@Test
public void createsAsMap() {
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("deviceName", "Pixel");
capabilities.setCapability("platformVersion", "10");
capabilities.setCapability("newCommandTimeout", 60);

mobileOptions = new MobileOptions<>(capabilities);

mobileOptions.setAutomationName(AutomationName.ANDROID_UIAUTOMATOR2)
.setPlatformName("android")
.setOtherApps("/path/to/app.apk")
.setLocale("fr_CA")
.setUdid("1ae203187fc012g")
.setOrientation(ScreenOrientation.LANDSCAPE)
.setLanguage("fr");

Map actual = mobileOptions.asMap();

System.out.println(Duration.ofSeconds(60).getClass());
TreeMap<String, Object> expected = new TreeMap<>();
expected.put("appium:automationName", "UIAutomator2");
expected.put("platformName", "android");
expected.put("appium:platformVersion", "10");
expected.put("appium:deviceName", "Pixel");
expected.put("appium:otherApps", "/path/to/app.apk");
expected.put("appium:locale", "fr_CA");
expected.put("appium:udid", "1ae203187fc012g");
expected.put("appium:orientation", ScreenOrientation.LANDSCAPE);
expected.put("appium:newCommandTimeout", 60);
expected.put("appium:language", "fr");

assertEquals(unmodifiableMap(expected), actual);
}
}