Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to set multiple settings #1409

Merged
Merged
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
28 changes: 28 additions & 0 deletions src/main/java/io/appium/java_client/HasSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

import org.openqa.selenium.remote.Response;

import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public interface HasSettings extends ExecutesMethod {

Expand Down Expand Up @@ -52,6 +55,31 @@ default HasSettings setSetting(String settingName, Object value) {
return this;
}

/**
* Sets settings for this test session.
*
* @param settings a map with settings, where key is the setting name you wish to set and value is the value of
* the setting.
* @return Self instance for chaining.
*/
default HasSettings setSettings(EnumMap<Setting, Object> settings) {
Map<String, Object> convertedSettings = settings.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey().toString(), Entry::getValue));
return setSettings(convertedSettings);
}

/**
* Sets settings for this test session.
*
* @param settings a map with settings, where key is the setting name you wish to set and value is the value of
* the setting.
* @return Self instance for chaining.
*/
default HasSettings setSettings(Map<String, Object> settings) {
CommandExecutionHelper.execute(this, setSettingsCommand(settings));
return this;
}

/**
* Get settings stored for this test session It's probably better to use a
* convenience function, rather than use this function directly. Try finding
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,11 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
}

public static Map.Entry<String, Map<String, ?>> setSettingsCommand(String setting, Object value) {
return new AbstractMap.SimpleEntry<>(SET_SETTINGS, prepareArguments("settings",
prepareArguments(setting, value)));
return setSettingsCommand(prepareArguments(setting, value));
}

public static Map.Entry<String, Map<String, ?>> setSettingsCommand(Map<String, Object> settings) {
return new AbstractMap.SimpleEntry<>(SET_SETTINGS, prepareArguments("settings", settings));
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/io/appium/java_client/android/SettingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.junit.Test;

import java.time.Duration;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -103,6 +106,24 @@ public class SettingTest extends BaseAndroidTest {
.get("shouldUseCompactResponses"));
}

@Test public void setMultipleSettings() {
EnumMap<Setting, Object> enumSettings = new EnumMap<>(Setting.class);
enumSettings.put(Setting.IGNORE_UNIMPORTANT_VIEWS, true);
enumSettings.put(Setting.ELEMENT_RESPONSE_ATTRIBUTES, "type,label");
driver.setSettings(enumSettings);
Map<String, Object> actual = driver.getSettings();
assertEquals(true, actual.get(Setting.IGNORE_UNIMPORTANT_VIEWS.toString()));
assertEquals("type,label", actual.get(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString()));

Map<String, Object> mapSettings = new HashMap<>();
mapSettings.put(Setting.IGNORE_UNIMPORTANT_VIEWS.toString(), false);
mapSettings.put(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString(), "");
driver.setSettings(mapSettings);
actual = driver.getSettings();
assertEquals(false, actual.get(Setting.IGNORE_UNIMPORTANT_VIEWS.toString()));
assertEquals("", actual.get(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString()));
}

private void assertJSONElementContains(Setting setting, long value) {
assertEquals(driver.getSettings().get(setting.toString()), value);
}
Expand Down
27 changes: 23 additions & 4 deletions src/test/java/io/appium/java_client/ios/SettingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import static org.junit.Assert.assertEquals;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

public class SettingTest extends AppIOSTest {

Expand All @@ -34,10 +37,10 @@ public class SettingTest extends AppIOSTest {
}

@Test public void testSetElementResponseAttributes() {
assertEquals("type,label", driver.getSettings()
assertEquals("", driver.getSettings()
.get(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString()));
driver.setElementResponseAttributes("name");
assertEquals("name", driver.getSettings()
driver.setElementResponseAttributes("type,label");
assertEquals("type,label", driver.getSettings()
.get(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString()));
}

Expand Down Expand Up @@ -94,5 +97,21 @@ public class SettingTest extends AppIOSTest {
.get("shouldUseCompactResponses"));
}


@Test public void setMultipleSettings() {
EnumMap<Setting, Object> enumSettings = new EnumMap<>(Setting.class);
enumSettings.put(Setting.IGNORE_UNIMPORTANT_VIEWS, true);
enumSettings.put(Setting.ELEMENT_RESPONSE_ATTRIBUTES, "type,label");
driver.setSettings(enumSettings);
Map<String, Object> actual = driver.getSettings();
assertEquals(true, actual.get(Setting.IGNORE_UNIMPORTANT_VIEWS.toString()));
assertEquals("type,label", actual.get(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString()));

Map<String, Object> mapSettings = new HashMap<>();
mapSettings.put(Setting.IGNORE_UNIMPORTANT_VIEWS.toString(), false);
mapSettings.put(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString(), "");
driver.setSettings(mapSettings);
actual = driver.getSettings();
assertEquals(false, actual.get(Setting.IGNORE_UNIMPORTANT_VIEWS.toString()));
assertEquals("", actual.get(Setting.ELEMENT_RESPONSE_ATTRIBUTES.toString()));
}
}