diff --git a/src/main/java/io/appium/java_client/AppiumDriver.java b/src/main/java/io/appium/java_client/AppiumDriver.java index 854d76239..eab19e283 100644 --- a/src/main/java/io/appium/java_client/AppiumDriver.java +++ b/src/main/java/io/appium/java_client/AppiumDriver.java @@ -56,15 +56,15 @@ * Default Appium driver implementation. * * @param the required type of class which implement {@link WebElement}. - * Instances of the defined type will be returned via findElement* and findElements* - * Warning (!!!). Allowed types: - * {@link WebElement}, {@link org.openqa.selenium.remote.RemoteWebElement}, - * {@link MobileElement} and its subclasses that designed - * specifically for each target mobile OS (still Android and iOS) -*/ + * Instances of the defined type will be returned via findElement* and findElements* + * Warning (!!!). Allowed types: + * {@link WebElement}, {@link org.openqa.selenium.remote.RemoteWebElement}, + * {@link MobileElement} and its subclasses that designed + * specifically for each target mobile OS (still Android and iOS) + */ @SuppressWarnings("unchecked") public class AppiumDriver - extends DefaultGenericMobileDriver implements ComparesImages, FindsByImage, FindsByCustom { + extends DefaultGenericMobileDriver implements ComparesImages, FindsByImage, FindsByCustom { private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true); // frequently used command parameters @@ -75,9 +75,9 @@ public class AppiumDriver /** * Creates a new instance based on command {@code executor} and {@code capabilities}. * - * @param executor is an instance of {@link HttpCommandExecutor} - * or class that extends it. Default commands or another vendor-specific - * commands may be specified there. + * @param executor is an instance of {@link HttpCommandExecutor} + * or class that extends it. Default commands or another vendor-specific + * commands may be specified there. * @param capabilities take a look at {@link Capabilities} */ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) { @@ -91,24 +91,24 @@ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) { public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) { this(new AppiumCommandExecutor(MobileCommand.commandRepository, remoteAddress), - desiredCapabilities); + desiredCapabilities); } public AppiumDriver(URL remoteAddress, HttpClient.Factory httpClientFactory, - Capabilities desiredCapabilities) { + Capabilities desiredCapabilities) { this(new AppiumCommandExecutor(MobileCommand.commandRepository, remoteAddress, - httpClientFactory), desiredCapabilities); + httpClientFactory), desiredCapabilities); } public AppiumDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) { this(new AppiumCommandExecutor(MobileCommand.commandRepository, service), - desiredCapabilities); + desiredCapabilities); } public AppiumDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory, - Capabilities desiredCapabilities) { + Capabilities desiredCapabilities) { this(new AppiumCommandExecutor(MobileCommand.commandRepository, service, httpClientFactory), - desiredCapabilities); + desiredCapabilities); } public AppiumDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) { @@ -116,13 +116,13 @@ public AppiumDriver(AppiumServiceBuilder builder, Capabilities desiredCapabiliti } public AppiumDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory, - Capabilities desiredCapabilities) { + Capabilities desiredCapabilities) { this(builder.build(), httpClientFactory, desiredCapabilities); } public AppiumDriver(HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) { this(AppiumDriverLocalService.buildDefaultService(), httpClientFactory, - desiredCapabilities); + desiredCapabilities); } public AppiumDriver(Capabilities desiredCapabilities) { @@ -133,26 +133,29 @@ public AppiumDriver(Capabilities desiredCapabilities) { * Changes platform name and returns new capabilities. * * @param originalCapabilities the given {@link Capabilities}. - * @param newPlatform a {@link MobileCapabilityType#PLATFORM_NAME} value which has - * to be set up + * @param newPlatform a {@link MobileCapabilityType#PLATFORM_NAME} value which has + * to be set up * @return {@link Capabilities} with changed mobile platform value */ protected static Capabilities substituteMobilePlatform(Capabilities originalCapabilities, - String newPlatform) { + String newPlatform) { DesiredCapabilities dc = new DesiredCapabilities(originalCapabilities); dc.setCapability(PLATFORM_NAME, newPlatform); return dc; } - @Override public List findElements(By by) { + @Override + public List findElements(By by) { return super.findElements(by); } - @Override public List findElements(String by, String using) { + @Override + public List findElements(String by, String using) { return super.findElements(by, using); } - @Override public List findElementsById(String id) { + @Override + public List findElementsById(String id) { return super.findElementsById(id); } @@ -184,15 +187,18 @@ public List findElementsByXPath(String using) { return super.findElementsByXPath(using); } - @Override public List findElementsByAccessibilityId(String using) { + @Override + public List findElementsByAccessibilityId(String using) { return super.findElementsByAccessibilityId(using); } - @Override public ExecuteMethod getExecuteMethod() { + @Override + public ExecuteMethod getExecuteMethod() { return executeMethod; } - @Override public WebDriver context(String name) { + @Override + public WebDriver context(String name) { checkNotNull(name, "Must supply a context name"); try { execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name)); @@ -202,7 +208,8 @@ public List findElementsByXPath(String using) { } } - @Override public Set getContextHandles() { + @Override + public Set getContextHandles() { Response response = execute(DriverCommand.GET_CONTEXT_HANDLES); Object value = response.getValue(); try { @@ -210,20 +217,31 @@ public List findElementsByXPath(String using) { return new LinkedHashSet<>(returnedValues); } catch (ClassCastException ex) { throw new WebDriverException( - "Returned value cannot be converted to List: " + value, ex); + "Returned value cannot be converted to List: " + value, ex); } } - @Override public String getContext() { + @Override + public String getContext() { String contextName = - String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue()); + String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue()); if ("null".equalsIgnoreCase(contextName)) { return null; } return contextName; } - @Override public DeviceRotation rotation() { + /** + * This method is used to get build version status of running Appium server. + * + * @return map containing version details + */ + public Map getStatus() { + return (Map) execute(DriverCommand.STATUS).getValue(); + } + + @Override + public DeviceRotation rotation() { Response response = execute(DriverCommand.GET_SCREEN_ROTATION); DeviceRotation deviceRotation = new DeviceRotation((Map) response.getValue()); @@ -233,17 +251,20 @@ public List findElementsByXPath(String using) { return deviceRotation; } - @Override public void rotate(DeviceRotation rotation) { + @Override + public void rotate(DeviceRotation rotation) { execute(DriverCommand.SET_SCREEN_ROTATION, rotation.parameters()); } - @Override public void rotate(ScreenOrientation orientation) { + @Override + public void rotate(ScreenOrientation orientation) { execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", orientation.value().toUpperCase())); } - @Override public ScreenOrientation getOrientation() { + @Override + public ScreenOrientation getOrientation() { Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION); String orientation = response.getValue().toString().toLowerCase(); if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) { @@ -255,11 +276,13 @@ public List findElementsByXPath(String using) { } } - @Override public Location location() { + @Override + public Location location() { return locationContext.location(); } - @Override public void setLocation(Location location) { + @Override + public void setLocation(Location location) { locationContext.setLocation(location); } @@ -267,7 +290,8 @@ public URL getRemoteAddress() { return remoteAddress; } - @Override public boolean isBrowser() { + @Override + public boolean isBrowser() { return super.isBrowser() && !containsIgnoreCase(getContext(), "NATIVE_APP"); } diff --git a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java index f2dedab97..f4b41348a 100644 --- a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java +++ b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java @@ -29,6 +29,7 @@ import io.appium.java_client.appmanagement.ApplicationState; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; +import org.hamcrest.Matchers; import org.junit.Test; import org.openqa.selenium.ScreenOrientation; import org.openqa.selenium.html5.Location; @@ -42,7 +43,8 @@ public class AndroidDriverTest extends BaseAndroidTest { - @Test public void sendSMSTest() { + @Test + public void sendSMSTest() { try { driver.sendSMS("11111111", "call"); } catch (Exception e) { @@ -50,7 +52,14 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void gsmCallTest() { + + @Test + public void getStatusTest() { + assertThat(driver.getStatus().get("build").toString(), Matchers.containsString(".")); + } + + @Test + public void gsmCallTest() { try { driver.makeGsmCall("11111111", GsmCallActions.CALL); driver.makeGsmCall("11111111", GsmCallActions.ACCEPT); @@ -59,7 +68,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void toggleWiFi() { + @Test + public void toggleWiFi() { try { driver.toggleWifi(); } catch (Exception e) { @@ -67,7 +77,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void toggleAirplane() { + @Test + public void toggleAirplane() { try { driver.toggleAirplaneMode(); } catch (Exception e) { @@ -75,7 +86,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void toggleData() { + @Test + public void toggleData() { try { driver.toggleData(); } catch (Exception e) { @@ -83,7 +95,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void gsmSignalStrengthTest() { + @Test + public void gsmSignalStrengthTest() { try { driver.setGsmSignalStrength(GsmSignalStrength.GREAT); } catch (Exception e) { @@ -91,7 +104,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void gsmVoiceTest() { + @Test + public void gsmVoiceTest() { try { driver.setGsmVoice(GsmVoiceState.OFF); } catch (Exception e) { @@ -99,7 +113,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void networkSpeedTest() { + @Test + public void networkSpeedTest() { try { driver.setNetworkSpeed(NetworkSpeed.EDGE); } catch (Exception e) { @@ -107,7 +122,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void powerTest() { + @Test + public void powerTest() { try { driver.setPowerCapacity(100); driver.setPowerAC(PowerACState.OFF); @@ -116,71 +132,81 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void getDeviceTimeTest() { + @Test + public void getDeviceTimeTest() { String time = driver.getDeviceTime(); assertFalse(time.isEmpty()); } - @Test public void isAppInstalledTest() { + @Test + public void isAppInstalledTest() { assertTrue(driver.isAppInstalled("com.example.android.apis")); } - @Test public void isAppNotInstalledTest() { + @Test + public void isAppNotInstalledTest() { assertFalse(driver.isAppInstalled("foo")); } - @Test public void closeAppTest() { + @Test + public void closeAppTest() { driver.closeApp(); driver.launchApp(); assertEquals(".ApiDemos", driver.currentActivity()); } - @Test public void pushFileTest() { + @Test + public void pushFileTest() { byte[] data = Base64.encodeBase64( - "The eventual code is no more than the deposit of your understanding. ~E. W. Dijkstra" - .getBytes()); + "The eventual code is no more than the deposit of your understanding. ~E. W. Dijkstra" + .getBytes()); driver.pushFile("/data/local/tmp/remote.txt", data); byte[] returnData = driver.pullFile("/data/local/tmp/remote.txt"); String returnDataDecoded = new String(returnData); assertEquals( - "The eventual code is no more than the deposit of your understanding. ~E. W. Dijkstra", - returnDataDecoded); + "The eventual code is no more than the deposit of your understanding. ~E. W. Dijkstra", + returnDataDecoded); } - @Test public void pushTempFileTest() throws Exception { + @Test + public void pushTempFileTest() throws Exception { File temp = File.createTempFile("Temp_", "_test"); try { FileUtils.writeStringToFile(temp, "The eventual code is no " - + "more than the deposit of your understanding. ~E. W. Dijkstra", "UTF-8", true); + + "more than the deposit of your understanding. ~E. W. Dijkstra", "UTF-8", true); driver.pushFile("/data/local/tmp/remote2.txt", temp); byte[] returnData = driver.pullFile("/data/local/tmp/remote2.txt"); String returnDataDecoded = new String(returnData); assertEquals( - "The eventual code is no more than the deposit of " - + "your understanding. ~E. W. Dijkstra", - returnDataDecoded); + "The eventual code is no more than the deposit of " + + "your understanding. ~E. W. Dijkstra", + returnDataDecoded); } finally { FileUtils.forceDelete(temp); } } - @Test public void toggleLocationServicesTest() { + @Test + public void toggleLocationServicesTest() { driver.toggleLocationServices(); } - @Test public void geolocationTest() { + @Test + public void geolocationTest() { Location location = new Location(45, 45, 100); driver.setLocation(location); } - @Test public void orientationTest() { + @Test + public void orientationTest() { assertEquals(ScreenOrientation.PORTRAIT, driver.getOrientation()); driver.rotate(ScreenOrientation.LANDSCAPE); assertEquals(ScreenOrientation.LANDSCAPE, driver.getOrientation()); driver.rotate(ScreenOrientation.PORTRAIT); } - @Test public void lockTest() { + @Test + public void lockTest() { try { driver.lockDevice(); assertTrue(driver.isDeviceLocked()); @@ -190,14 +216,16 @@ public class AndroidDriverTest extends BaseAndroidTest { } } - @Test public void runAppInBackgroundTest() { + @Test + public void runAppInBackgroundTest() { long time = System.currentTimeMillis(); driver.runAppInBackground(Duration.ofSeconds(4)); long timeAfter = System.currentTimeMillis(); assert (timeAfter - time > 3000); } - @Test public void testApplicationsManagement() throws InterruptedException { + @Test + public void testApplicationsManagement() throws InterruptedException { String appId = driver.getCurrentPackage(); assertThat(driver.queryAppState(appId), equalTo(ApplicationState.RUNNING_IN_FOREGROUND)); Thread.sleep(500); @@ -208,37 +236,44 @@ public class AndroidDriverTest extends BaseAndroidTest { assertThat(driver.queryAppState(appId), equalTo(ApplicationState.RUNNING_IN_FOREGROUND)); } - @Test public void pullFileTest() { + @Test + public void pullFileTest() { byte[] data = - driver.pullFile("/data/system/users/userlist.xml"); + driver.pullFile("/data/system/users/userlist.xml"); assert (data.length > 0); } - @Test public void resetTest() { + @Test + public void resetTest() { driver.resetApp(); } - @Test public void endTestCoverage() { + @Test + public void endTestCoverage() { driver.endTestCoverage("android.intent.action.MAIN", ""); } - @Test public void getDeviceUDIDTest() { + @Test + public void getDeviceUDIDTest() { String deviceSerial = driver.getSessionDetail("deviceUDID").toString(); assertNotNull(deviceSerial); } - @Test public void getSessionMapData() { - Map map = (Map) driver.getSessionDetail("desired"); + @Test + public void getSessionMapData() { + Map map = (Map) driver.getSessionDetail("desired"); assertNotEquals(map.size(), 0); } - - @Test public void deviceDetailsAndKeyboardTest() { + + @Test + public void deviceDetailsAndKeyboardTest() { assertFalse(driver.isKeyboardShown()); assertNotNull(driver.getDisplayDensity()); assertNotEquals(0, driver.getSystemBars().size()); } - @Test public void getSupportedPerformanceDataTypesTest() { + @Test + public void getSupportedPerformanceDataTypesTest() { driver.startActivity(new Activity(APP_ID, ".ApiDemos")); List dataTypes = new ArrayList<>(); @@ -251,14 +286,15 @@ public class AndroidDriverTest extends BaseAndroidTest { assertEquals(4, supportedPerformanceDataTypes.size()); - for ( int i = 0 ; i < supportedPerformanceDataTypes.size() ; ++i) { + for (int i = 0; i < supportedPerformanceDataTypes.size(); ++i) { assertEquals(dataTypes.get(i), supportedPerformanceDataTypes.get(i)); } } - @Test public void getPerformanceDataTest() { + @Test + public void getPerformanceDataTest() { driver.startActivity(new Activity(APP_ID, ".ApiDemos")); List supportedPerformanceDataTypes = driver.getSupportedPerformanceDataTypes(); @@ -274,7 +310,8 @@ public class AndroidDriverTest extends BaseAndroidTest { } - @Test public void getCurrentPackageTest() { + @Test + public void getCurrentPackageTest() { assertEquals(APP_ID, driver.getCurrentPackage()); }