Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mengge.android; import static com.google.common.base.Preconditions.checkNotNull; import static com.mengge.android.AndroidMobileCommandHelper.longPressKeyCodeCommand; import static com.mengge.android.AndroidMobileCommandHelper.pressKeyCodeCommand; import com.mengge.*; import com.mengge.android.internal.JsonToAndroidElementConverter; import com.mengge.android.*; import com.mengge.remote.MobilePlatform; import com.mengge.service.local.AppiumDriverLocalService; import com.mengge.service.local.AppiumServiceBuilder; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.remote.http.HttpClient; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.List; /** * @param <T> the required type of class which implement {@link org.openqa.selenium.WebElement}. * Instances of the defined type will be returned via findElement* and findElements*. * Warning (!!!). Allowed types: * {@link org.openqa.selenium.WebElement} * {@link TouchableElement} * {@link org.openqa.selenium.remote.RemoteWebElement} * {@link MobileElement} * {@link AndroidElement} */ public class AndroidDriver<T extends WebElement> extends AppiumDriver<T> implements AndroidDeviceActionShortcuts, HasNetworkConnection, PushesFiles, StartsActivity, FindsByAndroidUIAutomator<T> { private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID; /** * @param executor is an instance of {@link org.openqa.selenium.remote.HttpCommandExecutor} * or class that extends it. Default commands or another vendor-specific * commands may be specified there. * @param capabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) { super(executor, capabilities, JsonToAndroidElementConverter.class); } /** * @param remoteAddress is the address of remotely/locally * started Appium server * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) { super(remoteAddress, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param remoteAddress is the address of remotely/locally * started Appium server * @param httpClientFactory take a look * at {@link org.openqa.selenium.remote.http.HttpClient.Factory} * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(URL remoteAddress, HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) { super(remoteAddress, httpClientFactory, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param service take a look * at {@link AppiumDriverLocalService} * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) { super(service, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param service take a look * at {@link AppiumDriverLocalService} * @param httpClientFactory take a look * at {@link org.openqa.selenium.remote.http.HttpClient.Factory} * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) { super(service, httpClientFactory, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param builder take a look * at {@link AppiumServiceBuilder} * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) { super(builder, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param builder take a look * at {@link AppiumServiceBuilder} * @param httpClientFactory take a look * at {@link org.openqa.selenium.remote.http.HttpClient.Factory} * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) { super(builder, httpClientFactory, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param httpClientFactory take a look * at {@link org.openqa.selenium.remote.http.HttpClient.Factory} * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) { super(httpClientFactory, substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @param desiredCapabilities take a look * at {@link org.openqa.selenium.Capabilities} */ public AndroidDriver(Capabilities desiredCapabilities) { super(substituteMobilePlatform(desiredCapabilities, ANDROID_PLATFORM), JsonToAndroidElementConverter.class); } /** * @see TouchShortcuts#swipe(int, int, int, int, int) */ @Override public void swipe(int startx, int starty, int endx, int endy, int duration) { doSwipe(startx, starty, endx, endy, duration); } /** * Send a key event to the device. * * @param key code for the key pressed on the device. */ @Override public void pressKeyCode(int key) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.pressKeyCodeCommand(key)); } /** * @param key code for the key pressed on the Android device. * @param metastate metastate for the keypress. * @see AndroidKeyCode * @see AndroidKeyMetastate * @see AndroidDeviceActionShortcuts#pressKeyCode(int, Integer). */ @Override public void pressKeyCode(int key, Integer metastate) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.pressKeyCodeCommand(key, metastate)); } /** * Send a long key event to the device. * * @param key code for the long key pressed on the device. */ @Override public void longPressKeyCode(int key) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.longPressKeyCodeCommand(key)); } /** * @param key code for the long key pressed on the Android device. * @param metastate metastate for the long key press. * @see AndroidKeyCode * @see AndroidKeyMetastate * @see AndroidDeviceActionShortcuts#pressKeyCode(int, Integer) */ @Override public void longPressKeyCode(int key, Integer metastate) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.longPressKeyCodeCommand(key, metastate)); } @Override public void setConnection(Connection connection) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.setConnectionCommand(connection)); } @Override public Connection getConnection() { long bitMask = CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.getNetworkConnectionCommand()); Connection[] types = Connection.values(); for (Connection connection : types) { if (connection.bitMask == bitMask) { return connection; } } throw new WebDriverException( "The unknown network connection " + "type has been returned. The bitmask is " + bitMask); } @Override public void pushFile(String remotePath, byte[] base64Data) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.pushFileCommandCommand(remotePath, base64Data)); } @Override public void pushFile(String remotePath, File file) throws IOException { checkNotNull(file, "A reference to file should not be NULL"); if (!file.exists()) { throw new IOException("The given file " + file.getAbsolutePath() + " doesn't exist"); } pushFile(remotePath, Base64.encodeBase64(FileUtils.readFileToByteArray(file))); } @Override public void startActivity(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity, String intentAction, String intentCategory, String intentFlags, String optionalIntentArguments, boolean stopApp) throws IllegalArgumentException { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.startActivityCommand(appPackage, appActivity, appWaitPackage, appWaitActivity, intentAction, intentCategory, intentFlags, optionalIntentArguments, stopApp)); } @Override public void startActivity(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity, boolean stopApp) throws IllegalArgumentException { this.startActivity(appPackage, appActivity, appWaitPackage, appWaitActivity, null, null, null, null, stopApp); } @Override public void startActivity(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity) throws IllegalArgumentException { this.startActivity(appPackage, appActivity, appWaitPackage, appWaitActivity, null, null, null, null, true); } @Override public void startActivity(String appPackage, String appActivity) throws IllegalArgumentException { this.startActivity(appPackage, appActivity, null, null, null, null, null, null, true); } @Override public void startActivity(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity, String intentAction, String intentCategory, String intentFlags, String intentOptionalArgs) throws IllegalArgumentException { this.startActivity(appPackage, appActivity, appWaitPackage, appWaitActivity, intentAction, intentCategory, intentFlags, intentOptionalArgs, true); } /** * Get test-coverage data. * * @param intent intent to broadcast. * @param path path to .ec file. */ public void endTestCoverage(String intent, String path) { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.endTestCoverageCommand(intent, path)); } /** * Get the current activity being run on the mobile device. * * @return a current activity being run on the mobile device. */ public String currentActivity() { return CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.currentActivityCommand()); } /** * Open the notification shade, on Android devices. */ public void openNotifications() { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.openNotificationsCommand()); } /** * Check if the device is locked. * * @return true if device is locked. False otherwise */ public boolean isLocked() { return CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.isLockedCommand()); } public void toggleLocationServices() { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.toggleLocationServicesCommand()); } /** * Set the `ignoreUnimportantViews` setting. *Android-only method*. * Sets whether Android devices should use `setCompressedLayoutHeirarchy()` * which ignores all views which are marked IMPORTANT_FOR_ACCESSIBILITY_NO * or IMPORTANT_FOR_ACCESSIBILITY_AUTO (and have been deemed not important * by the system), in an attempt to make things less confusing or faster. * * @param compress ignores unimportant views if true, doesn't ignore otherwise. */ // Should be moved to the subclass public void ignoreUnimportantViews(Boolean compress) { setSetting(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS, compress); } /** * @throws WebDriverException This method is not * applicable with browser/webview UI. */ @Override public T findElementByAndroidUIAutomator(String using) throws WebDriverException { return findElement("-android uiautomator", using); } /** * @throws WebDriverException This method is not * applicable with browser/webview UI. */ @Override public List<T> findElementsByAndroidUIAutomator(String using) throws WebDriverException { return findElements("-android uiautomator", using); } /** * This method locks a device. */ public void lockDevice() { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.lockDeviceCommand()); } /** * This method unlocks a device. */ public void unlockDevice() { CommandExecutionHelper.execute(this, AndroidMobileCommandHelper.unlockCommand()); } }