Skip to content
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
19 changes: 19 additions & 0 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.UnsupportedCommandException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DriverCommand;
Expand All @@ -48,7 +49,9 @@
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
import static io.appium.java_client.remote.MobileCapabilityType.AUTOMATION_NAME;
Expand All @@ -64,13 +67,15 @@ public class AppiumDriver extends RemoteWebDriver implements
ExecutesDriverScript,
LogsEvents,
HasBrowserCheck,
CanRememberExtensionPresence,
HasSettings {

private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
// frequently used command parameters
private final URL remoteAddress;
protected final RemoteLocationContext locationContext;
private final ExecuteMethod executeMethod;
private final Set<String> absentExtensionNames = new HashSet<>();

/**
* Creates a new instance based on command {@code executor} and {@code capabilities}.
Expand Down Expand Up @@ -327,4 +332,18 @@ public X convertFromPngBytes(byte[] png) {
}
});
}

@Override
public AppiumDriver assertExtensionExists(String extName) {
if (absentExtensionNames.contains(extName)) {
throw new UnsupportedCommandException();
}
return this;
}

@Override
public AppiumDriver markExtensionAbsence(String extName) {
absentExtensionNames.add(extName);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.appium.java_client;

import org.openqa.selenium.UnsupportedCommandException;

public interface CanRememberExtensionPresence {
/**
* Verifies if the given extension is not present in the list of absent extensions
* for the given driver instance.
* This API is designed for private usage.
*
* @param extName extension name.
* @return self instance for chaining.
* @throws UnsupportedCommandException if the extension is listed in the list of absents.
*/
ExecutesMethod assertExtensionExists(String extName);

/**
* Marks the given extension as absent for the given driver instance.
* This API is designed for private usage.
*
* @param extName extension name.
* @return self instance for chaining.
*/
ExecutesMethod markExtensionAbsence(String extName);
}
19 changes: 12 additions & 7 deletions src/main/java/io/appium/java_client/HasAppStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@
import static io.appium.java_client.MobileCommand.GET_STRINGS;
import static io.appium.java_client.MobileCommand.prepareArguments;

public interface HasAppStrings extends ExecutesMethod {
public interface HasAppStrings extends ExecutesMethod, CanRememberExtensionPresence {
/**
* Get all defined Strings from an app for the default language.
* See the documentation for 'mobile: getAppStrings' extension for more details.
*
* @return a map with localized strings defined in the app
*/
default Map<String, String> getAppStringMap() {
final String extName = "mobile: getAppStrings";
try {
return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings");
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return CommandExecutionHelper.execute(this, GET_STRINGS);
return CommandExecutionHelper.execute(markExtensionAbsence(extName), GET_STRINGS);
}
}

Expand All @@ -49,14 +50,16 @@ default Map<String, String> getAppStringMap() {
* @return a map with localized strings defined in the app
*/
default Map<String, String> getAppStringMap(String language) {
final String extName = "mobile: getAppStrings";
try {
return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings", ImmutableMap.of(
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
"language", language
));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return CommandExecutionHelper.execute(
this, new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments("language", language))
markExtensionAbsence(extName),
new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments("language", language))
);
}
}
Expand All @@ -71,8 +74,9 @@ default Map<String, String> getAppStringMap(String language) {
* @return a map with localized strings defined in the app
*/
default Map<String, String> getAppStringMap(String language, String stringFile) {
final String extName = "mobile: getAppStrings";
try {
return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings", ImmutableMap.of(
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
"language", language,
"stringFile", stringFile
));
Expand All @@ -81,7 +85,8 @@ default Map<String, String> getAppStringMap(String language, String stringFile)
String[] parameters = new String[]{"language", "stringFile"};
Object[] values = new Object[]{language, stringFile};
return CommandExecutionHelper.execute(
this, new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values))
markExtensionAbsence(extName),
new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.MobileCommand.isKeyboardShownCommand;

public interface HasOnScreenKeyboard extends ExecutesMethod {
public interface HasOnScreenKeyboard extends ExecutesMethod, CanRememberExtensionPresence {

/**
* Check if the on-screen keyboard is displayed.
Expand All @@ -14,11 +14,14 @@ public interface HasOnScreenKeyboard extends ExecutesMethod {
* @return true if keyboard is displayed. False otherwise
*/
default boolean isKeyboardShown() {
final String extName = "mobile: isKeyboardShown";
try {
return checkNotNull(CommandExecutionHelper.executeScript(this, "mobile: isKeyboardShown"));
return checkNotNull(CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return checkNotNull(CommandExecutionHelper.execute(this, isKeyboardShownCommand()));
return checkNotNull(
CommandExecutionHelper.execute(markExtensionAbsence(extName), isKeyboardShownCommand())
);
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/io/appium/java_client/HidesKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD;

public interface HidesKeyboard extends ExecutesMethod {
public interface HidesKeyboard extends ExecutesMethod, CanRememberExtensionPresence {

/**
* Hides the keyboard if it is showing.
Expand All @@ -30,11 +30,12 @@ public interface HidesKeyboard extends ExecutesMethod {
* See the documentation for 'mobile: hideKeyboard' extension for more details.
*/
default void hideKeyboard() {
final String extName = "mobile: hideKeyboard";
try {
CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard");
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
CommandExecutionHelper.execute(this, HIDE_KEYBOARD);
CommandExecutionHelper.execute(markExtensionAbsence(extName), HIDE_KEYBOARD);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ public interface HidesKeyboardWithKeyName extends HidesKeyboard {
* keyboard.
*/
default void hideKeyboard(String keyName) {
final String extName = "mobile: hideKeyboard";
try {
CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard", ImmutableMap.of(
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
"keys", ImmutableList.of(keyName)
));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
CommandExecutionHelper.execute(this, hideKeyboardCommand(keyName));
CommandExecutionHelper.execute(markExtensionAbsence(extName), hideKeyboardCommand(keyName));
}
}

Expand Down
Loading