Skip to content

Commit 62a25e5

Browse files
chore: Avoid sending unnecessary requests if corresponding extensions are absent (#1903)
1 parent ba63c06 commit 62a25e5

19 files changed

+282
-126
lines changed

src/main/java/io/appium/java_client/AppiumDriver.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.openqa.selenium.MutableCapabilities;
3333
import org.openqa.selenium.OutputType;
3434
import org.openqa.selenium.SessionNotCreatedException;
35+
import org.openqa.selenium.UnsupportedCommandException;
3536
import org.openqa.selenium.WebDriverException;
3637
import org.openqa.selenium.remote.CapabilityType;
3738
import org.openqa.selenium.remote.DriverCommand;
@@ -48,7 +49,9 @@
4849
import java.net.URL;
4950
import java.util.Arrays;
5051
import java.util.Collections;
52+
import java.util.HashSet;
5153
import java.util.Map;
54+
import java.util.Set;
5255

5356
import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
5457
import static io.appium.java_client.remote.MobileCapabilityType.AUTOMATION_NAME;
@@ -64,13 +67,15 @@ public class AppiumDriver extends RemoteWebDriver implements
6467
ExecutesDriverScript,
6568
LogsEvents,
6669
HasBrowserCheck,
70+
CanRememberExtensionPresence,
6771
HasSettings {
6872

6973
private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
7074
// frequently used command parameters
7175
private final URL remoteAddress;
7276
protected final RemoteLocationContext locationContext;
7377
private final ExecuteMethod executeMethod;
78+
private final Set<String> absentExtensionNames = new HashSet<>();
7479

7580
/**
7681
* Creates a new instance based on command {@code executor} and {@code capabilities}.
@@ -327,4 +332,18 @@ public X convertFromPngBytes(byte[] png) {
327332
}
328333
});
329334
}
335+
336+
@Override
337+
public AppiumDriver assertExtensionExists(String extName) {
338+
if (absentExtensionNames.contains(extName)) {
339+
throw new UnsupportedCommandException();
340+
}
341+
return this;
342+
}
343+
344+
@Override
345+
public AppiumDriver markExtensionAbsence(String extName) {
346+
absentExtensionNames.add(extName);
347+
return this;
348+
}
330349
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.appium.java_client;
2+
3+
import org.openqa.selenium.UnsupportedCommandException;
4+
5+
public interface CanRememberExtensionPresence {
6+
/**
7+
* Verifies if the given extension is not present in the list of absent extensions
8+
* for the given driver instance.
9+
* This API is designed for private usage.
10+
*
11+
* @param extName extension name.
12+
* @return self instance for chaining.
13+
* @throws UnsupportedCommandException if the extension is listed in the list of absents.
14+
*/
15+
ExecutesMethod assertExtensionExists(String extName);
16+
17+
/**
18+
* Marks the given extension as absent for the given driver instance.
19+
* This API is designed for private usage.
20+
*
21+
* @param extName extension name.
22+
* @return self instance for chaining.
23+
*/
24+
ExecutesMethod markExtensionAbsence(String extName);
25+
}

src/main/java/io/appium/java_client/HasAppStrings.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@
2525
import static io.appium.java_client.MobileCommand.GET_STRINGS;
2626
import static io.appium.java_client.MobileCommand.prepareArguments;
2727

28-
public interface HasAppStrings extends ExecutesMethod {
28+
public interface HasAppStrings extends ExecutesMethod, CanRememberExtensionPresence {
2929
/**
3030
* Get all defined Strings from an app for the default language.
3131
* See the documentation for 'mobile: getAppStrings' extension for more details.
3232
*
3333
* @return a map with localized strings defined in the app
3434
*/
3535
default Map<String, String> getAppStringMap() {
36+
final String extName = "mobile: getAppStrings";
3637
try {
37-
return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings");
38+
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName);
3839
} catch (UnsupportedCommandException e) {
3940
// TODO: Remove the fallback
40-
return CommandExecutionHelper.execute(this, GET_STRINGS);
41+
return CommandExecutionHelper.execute(markExtensionAbsence(extName), GET_STRINGS);
4142
}
4243
}
4344

@@ -49,14 +50,16 @@ default Map<String, String> getAppStringMap() {
4950
* @return a map with localized strings defined in the app
5051
*/
5152
default Map<String, String> getAppStringMap(String language) {
53+
final String extName = "mobile: getAppStrings";
5254
try {
53-
return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings", ImmutableMap.of(
55+
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
5456
"language", language
5557
));
5658
} catch (UnsupportedCommandException e) {
5759
// TODO: Remove the fallback
5860
return CommandExecutionHelper.execute(
59-
this, new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments("language", language))
61+
markExtensionAbsence(extName),
62+
new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments("language", language))
6063
);
6164
}
6265
}
@@ -71,8 +74,9 @@ default Map<String, String> getAppStringMap(String language) {
7174
* @return a map with localized strings defined in the app
7275
*/
7376
default Map<String, String> getAppStringMap(String language, String stringFile) {
77+
final String extName = "mobile: getAppStrings";
7478
try {
75-
return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings", ImmutableMap.of(
79+
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
7680
"language", language,
7781
"stringFile", stringFile
7882
));
@@ -81,7 +85,8 @@ default Map<String, String> getAppStringMap(String language, String stringFile)
8185
String[] parameters = new String[]{"language", "stringFile"};
8286
Object[] values = new Object[]{language, stringFile};
8387
return CommandExecutionHelper.execute(
84-
this, new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values))
88+
markExtensionAbsence(extName),
89+
new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values))
8590
);
8691
}
8792
}

src/main/java/io/appium/java_client/HasOnScreenKeyboard.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static com.google.common.base.Preconditions.checkNotNull;
66
import static io.appium.java_client.MobileCommand.isKeyboardShownCommand;
77

8-
public interface HasOnScreenKeyboard extends ExecutesMethod {
8+
public interface HasOnScreenKeyboard extends ExecutesMethod, CanRememberExtensionPresence {
99

1010
/**
1111
* Check if the on-screen keyboard is displayed.
@@ -14,11 +14,14 @@ public interface HasOnScreenKeyboard extends ExecutesMethod {
1414
* @return true if keyboard is displayed. False otherwise
1515
*/
1616
default boolean isKeyboardShown() {
17+
final String extName = "mobile: isKeyboardShown";
1718
try {
18-
return checkNotNull(CommandExecutionHelper.executeScript(this, "mobile: isKeyboardShown"));
19+
return checkNotNull(CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName));
1920
} catch (UnsupportedCommandException e) {
2021
// TODO: Remove the fallback
21-
return checkNotNull(CommandExecutionHelper.execute(this, isKeyboardShownCommand()));
22+
return checkNotNull(
23+
CommandExecutionHelper.execute(markExtensionAbsence(extName), isKeyboardShownCommand())
24+
);
2225
}
2326
}
2427
}

src/main/java/io/appium/java_client/HidesKeyboard.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public interface HidesKeyboard extends ExecutesMethod {
23+
public interface HidesKeyboard extends ExecutesMethod, CanRememberExtensionPresence {
2424

2525
/**
2626
* Hides the keyboard if it is showing.
@@ -30,11 +30,12 @@ public interface HidesKeyboard extends ExecutesMethod {
3030
* See the documentation for 'mobile: hideKeyboard' extension for more details.
3131
*/
3232
default void hideKeyboard() {
33+
final String extName = "mobile: hideKeyboard";
3334
try {
34-
CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard");
35+
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName);
3536
} catch (UnsupportedCommandException e) {
3637
// TODO: Remove the fallback
37-
CommandExecutionHelper.execute(this, HIDE_KEYBOARD);
38+
CommandExecutionHelper.execute(markExtensionAbsence(extName), HIDE_KEYBOARD);
3839
}
3940
}
4041
}

src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ public interface HidesKeyboardWithKeyName extends HidesKeyboard {
3535
* keyboard.
3636
*/
3737
default void hideKeyboard(String keyName) {
38+
final String extName = "mobile: hideKeyboard";
3839
try {
39-
CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard", ImmutableMap.of(
40+
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
4041
"keys", ImmutableList.of(keyName)
4142
));
4243
} catch (UnsupportedCommandException e) {
4344
// TODO: Remove the fallback
44-
CommandExecutionHelper.execute(this, hideKeyboardCommand(keyName));
45+
CommandExecutionHelper.execute(markExtensionAbsence(extName), hideKeyboardCommand(keyName));
4546
}
4647
}
4748

0 commit comments

Comments
 (0)