|
| 1 | +package io.appium.java_client.android; |
| 2 | + |
| 3 | +import io.appium.java_client.remote.AutomationName; |
| 4 | +import io.appium.java_client.remote.MobileCapabilityType; |
| 5 | +import io.appium.java_client.service.local.AppiumDriverLocalService; |
| 6 | +import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException; |
| 7 | +import org.junit.After; |
| 8 | +import org.junit.AfterClass; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | +import org.openqa.selenium.DeviceRotation; |
| 12 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | + |
| 18 | +public class UIAutomator2Test { |
| 19 | + private static AppiumDriverLocalService service; |
| 20 | + protected static AndroidDriver<AndroidElement> driver; |
| 21 | + |
| 22 | + /** |
| 23 | + * initialization. |
| 24 | + */ |
| 25 | + @BeforeClass public static void beforeClass() throws Exception { |
| 26 | + service = AppiumDriverLocalService.buildDefaultService(); |
| 27 | + service.start(); |
| 28 | + |
| 29 | + if (service == null || !service.isRunning()) { |
| 30 | + throw new AppiumServerHasNotBeenStartedLocallyException |
| 31 | + ("An appium server node is not started!"); |
| 32 | + } |
| 33 | + |
| 34 | + File appDir = new File("src/test/java/io/appium/java_client"); |
| 35 | + File app = new File(appDir, "ApiDemos-debug.apk"); |
| 36 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 37 | + capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); |
| 38 | + capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); |
| 39 | + capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2); |
| 40 | + driver = new AndroidDriver<>(service.getUrl(), capabilities); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * finishing. |
| 45 | + */ |
| 46 | + @AfterClass public static void afterClass() { |
| 47 | + if (driver != null) { |
| 48 | + driver.quit(); |
| 49 | + } |
| 50 | + if (service != null) { |
| 51 | + service.stop(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @After public void afterMethod() { |
| 57 | + driver.rotate(new DeviceRotation(0, 0, 0)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test public void testLandscapeRightRotation() { |
| 61 | + DeviceRotation landscapeRightRotation = new DeviceRotation(0, 0, 90); |
| 62 | + driver.rotate(landscapeRightRotation); |
| 63 | + assertEquals(driver.rotation(), landscapeRightRotation); |
| 64 | + } |
| 65 | + |
| 66 | + @Test public void testLandscapeLeftRotation() { |
| 67 | + DeviceRotation landscapeLeftRotation = new DeviceRotation(0, 0, 270); |
| 68 | + driver.rotate(landscapeLeftRotation); |
| 69 | + assertEquals(driver.rotation(), landscapeLeftRotation); |
| 70 | + } |
| 71 | + |
| 72 | + @Test public void testPortraitUpsideDown() { |
| 73 | + DeviceRotation landscapeRightRotation = new DeviceRotation(0, 0, 180); |
| 74 | + driver.rotate(landscapeRightRotation); |
| 75 | + assertEquals(driver.rotation(), landscapeRightRotation); |
| 76 | + } |
| 77 | +} |
0 commit comments