Skip to content

Commit f1cc103

Browse files
refactor!: Remove selenium package override (#1555)
1 parent 3c4bbe9 commit f1cc103

File tree

62 files changed

+526
-2863
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+526
-2863
lines changed

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

Lines changed: 26 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,18 @@
1616

1717
package io.appium.java_client;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
2019
import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME;
21-
import static org.apache.commons.lang3.StringUtils.containsIgnoreCase;
22-
import static org.apache.commons.lang3.StringUtils.isBlank;
2320

24-
import com.google.common.collect.ImmutableMap;
25-
26-
import io.appium.java_client.internal.CapabilityHelpers;
27-
import io.appium.java_client.internal.JsonToMobileElementConverter;
2821
import io.appium.java_client.remote.AppiumCommandExecutor;
2922
import io.appium.java_client.remote.AppiumNewSessionCommandPayload;
3023
import io.appium.java_client.remote.MobileCapabilityType;
3124
import io.appium.java_client.service.local.AppiumDriverLocalService;
3225
import io.appium.java_client.service.local.AppiumServiceBuilder;
33-
import org.openqa.selenium.By;
3426
import org.openqa.selenium.Capabilities;
35-
import org.openqa.selenium.DeviceRotation;
27+
import org.openqa.selenium.ImmutableCapabilities;
3628
import org.openqa.selenium.MutableCapabilities;
37-
import org.openqa.selenium.ScreenOrientation;
3829
import org.openqa.selenium.SessionNotCreatedException;
39-
import org.openqa.selenium.WebDriver;
4030
import org.openqa.selenium.WebDriverException;
41-
import org.openqa.selenium.WebElement;
42-
import org.openqa.selenium.html5.Location;
43-
import org.openqa.selenium.remote.CapabilityType;
44-
import org.openqa.selenium.remote.DesiredCapabilities;
4531
import org.openqa.selenium.remote.DriverCommand;
4632
import org.openqa.selenium.remote.ErrorHandler;
4733
import org.openqa.selenium.remote.ExecuteMethod;
@@ -55,29 +41,25 @@
5541
import java.lang.reflect.Field;
5642
import java.net.URL;
5743
import java.util.Arrays;
58-
import java.util.LinkedHashSet;
59-
import java.util.List;
44+
import java.util.Collections;
6045
import java.util.Map;
61-
import java.util.Set;
6246

6347
/**
6448
* Default Appium driver implementation.
65-
*
66-
* @param <T> the required type of class which implement {@link WebElement}.
67-
* Instances of the defined type will be returned via findElement* and findElements*
68-
* Warning (!!!). Allowed types:
69-
* {@link WebElement}, {@link org.openqa.selenium.remote.RemoteWebElement},
70-
* {@link MobileElement} and its subclasses that designed
71-
* specifically for each target mobile OS (still Android and iOS)
7249
*/
73-
@SuppressWarnings("unchecked")
74-
public class AppiumDriver<T extends WebElement>
75-
extends DefaultGenericMobileDriver<T> implements ComparesImages, ExecutesDriverScript, LogsEvents, HasSettings {
50+
public class AppiumDriver extends RemoteWebDriver implements
51+
ExecutesMethod,
52+
ComparesImages,
53+
ExecutesDriverScript,
54+
LogsEvents,
55+
CanSetElementValue,
56+
HasBrowserCheck,
57+
HasSettings {
7658

7759
private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
7860
// frequently used command parameters
7961
private final URL remoteAddress;
80-
private final RemoteLocationContext locationContext;
62+
protected final RemoteLocationContext locationContext;
8163
private final ExecuteMethod executeMethod;
8264

8365
/**
@@ -94,7 +76,6 @@ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) {
9476
locationContext = new RemoteLocationContext(executeMethod);
9577
super.setErrorHandler(errorHandler);
9678
this.remoteAddress = executor.getAddressOfRemoteServer();
97-
this.setElementConverter(new JsonToMobileElementConverter(this));
9879
}
9980

10081
public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
@@ -147,95 +128,31 @@ public AppiumDriver(Capabilities desiredCapabilities) {
147128
*/
148129
protected static Capabilities updateDefaultPlatformName(Capabilities originalCapabilities,
149130
String defaultName) {
150-
if (originalCapabilities.getCapability(PLATFORM_NAME) == null) {
151-
DesiredCapabilities dc = new DesiredCapabilities(originalCapabilities);
152-
dc.setCapability(PLATFORM_NAME, defaultName);
153-
return dc;
154-
}
155-
return originalCapabilities;
156-
}
157-
158-
@Override
159-
public List<T> findElements(By by) {
160-
return super.findElements(by);
131+
return originalCapabilities.getCapability(PLATFORM_NAME) == null
132+
? originalCapabilities.merge(new ImmutableCapabilities(PLATFORM_NAME, defaultName))
133+
: originalCapabilities;
161134
}
162135

163136
@Override
164137
public ExecuteMethod getExecuteMethod() {
165138
return executeMethod;
166139
}
167140

168-
@Override
169-
public WebDriver context(String name) {
170-
checkNotNull(name, "Must supply a context name");
171-
try {
172-
execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));
173-
return this;
174-
} catch (WebDriverException e) {
175-
throw new NoSuchContextException(e.getMessage(), e);
176-
}
177-
}
178-
179-
@Override
180-
public Set<String> getContextHandles() {
181-
Response response = execute(DriverCommand.GET_CONTEXT_HANDLES);
182-
Object value = response.getValue();
183-
try {
184-
List<String> returnedValues = (List<String>) value;
185-
return new LinkedHashSet<>(returnedValues);
186-
} catch (ClassCastException ex) {
187-
throw new WebDriverException(
188-
"Returned value cannot be converted to List<String>: " + value, ex);
189-
}
190-
}
191-
192-
@Override
193-
public String getContext() {
194-
String contextName =
195-
String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());
196-
if ("null".equalsIgnoreCase(contextName)) {
197-
return null;
198-
}
199-
return contextName;
200-
}
201-
202141
/**
203142
* This method is used to get build version status of running Appium server.
204143
*
205144
* @return map containing version details
206145
*/
207146
public Map<String, Object> getStatus() {
147+
//noinspection unchecked
208148
return (Map<String, Object>) execute(DriverCommand.STATUS).getValue();
209149
}
210150

211-
@Override
212-
public DeviceRotation rotation() {
213-
Response response = execute(DriverCommand.GET_SCREEN_ROTATION);
214-
DeviceRotation deviceRotation =
215-
new DeviceRotation((Map<String, Number>) response.getValue());
216-
if (deviceRotation.getX() < 0 || deviceRotation.getY() < 0 || deviceRotation.getZ() < 0) {
217-
throw new WebDriverException("Unexpected orientation returned: " + deviceRotation);
218-
}
219-
return deviceRotation;
220-
}
221-
222-
@Override
223-
public void rotate(DeviceRotation rotation) {
224-
execute(DriverCommand.SET_SCREEN_ROTATION, rotation.parameters());
225-
}
226-
227-
228-
@Override
229-
public void rotate(ScreenOrientation orientation) {
230-
execute(DriverCommand.SET_SCREEN_ORIENTATION,
231-
ImmutableMap.of("orientation", orientation.value().toUpperCase()));
232-
}
233-
234151
/**
235152
* This method is used to add custom appium commands in Appium 2.0.
236153
*
237154
* @param httpMethod the available {@link HttpMethod}.
238-
* @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
155+
* @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
239156
* @param methodName The name of custom appium command.
240157
*/
241158
public void addCommand(HttpMethod httpMethod, String url, String methodName) {
@@ -257,51 +174,10 @@ public void addCommand(HttpMethod httpMethod, String url, String methodName) {
257174
((AppiumCommandExecutor) getCommandExecutor()).refreshAdditionalCommands();
258175
}
259176

260-
@Override
261-
public ScreenOrientation getOrientation() {
262-
Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION);
263-
String orientation = response.getValue().toString().toLowerCase();
264-
if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) {
265-
return ScreenOrientation.LANDSCAPE;
266-
} else if (orientation.equals(ScreenOrientation.PORTRAIT.value())) {
267-
return ScreenOrientation.PORTRAIT;
268-
} else {
269-
throw new WebDriverException("Unexpected orientation returned: " + orientation);
270-
}
271-
}
272-
273-
@Override
274-
public Location location() {
275-
return locationContext.location();
276-
}
277-
278-
@Override
279-
public void setLocation(Location location) {
280-
locationContext.setLocation(location);
281-
}
282-
283177
public URL getRemoteAddress() {
284178
return remoteAddress;
285179
}
286180

287-
@Override
288-
public boolean isBrowser() {
289-
String browserName = CapabilityHelpers.getCapability(getCapabilities(),
290-
CapabilityType.BROWSER_NAME, String.class);
291-
if (!isBlank(browserName)) {
292-
try {
293-
return (boolean) executeScript("return !!window.navigator;");
294-
} catch (WebDriverException ign) {
295-
// ignore
296-
}
297-
}
298-
try {
299-
return !containsIgnoreCase(getContext(), "NATIVE_APP");
300-
} catch (WebDriverException e) {
301-
return false;
302-
}
303-
}
304-
305181
@Override
306182
protected void startSession(Capabilities capabilities) {
307183
Response response = execute(new AppiumNewSessionCommandPayload(capabilities));
@@ -333,4 +209,14 @@ protected void startSession(Capabilities capabilities) {
333209
}
334210
setSessionId(response.getSessionId());
335211
}
212+
213+
@Override
214+
public Response execute(String driverCommand, Map<String, ?> parameters) {
215+
return super.execute(driverCommand, parameters);
216+
}
217+
218+
@Override
219+
public Response execute(String command) {
220+
return super.execute(command, Collections.emptyMap());
221+
}
336222
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.util.Map;
2525

2626
public class AppiumExecutionMethod implements ExecuteMethod {
27-
private final AppiumDriver<?> driver;
27+
private final AppiumDriver driver;
2828

29-
public AppiumExecutionMethod(AppiumDriver<?> driver) {
29+
public AppiumExecutionMethod(AppiumDriver driver) {
3030
this.driver = driver;
3131
}
3232

src/main/java/io/appium/java_client/android/AndroidElement.java renamed to src/main/java/io/appium/java_client/CanSetElementValue.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.appium.java_client.android;
17+
package io.appium.java_client;
1818

19-
import static io.appium.java_client.android.AndroidMobileCommandHelper.replaceElementValueCommand;
19+
import com.google.common.collect.ImmutableMap;
20+
import org.openqa.selenium.remote.RemoteWebElement;
2021

21-
import io.appium.java_client.CommandExecutionHelper;
22-
import io.appium.java_client.MobileElement;
23-
24-
public class AndroidElement extends MobileElement {
22+
public interface CanSetElementValue extends ExecutesMethod {
2523
/**
26-
* This method replace current text value.
27-
* @param value a new value
24+
* Set a value to an element.
25+
*
26+
* @param webElement Web element instance.
27+
* @param value Value to set.
2828
*/
29-
public void replaceValue(String value) {
30-
CommandExecutionHelper.execute(this, replaceElementValueCommand(this, value));
29+
default void setElementValue(RemoteWebElement webElement, String value) {
30+
this.execute(MobileCommand.SET_VALUE, ImmutableMap.of(
31+
"id", webElement.getId(),
32+
"value", value
33+
));
3134
}
3235
}

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)