From 7cdaa59a2d6d25bede1ca4f2977f0bc9a94804e7 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sat, 18 Mar 2017 00:50:55 +0100 Subject: [PATCH] Update runAppInBackground method implementation for XCUITest --- .../io/appium/java_client/MobileCommand.java | 2 +- .../io/appium/java_client/ios/IOSDriver.java | 19 +++++++++++++++ .../java_client/ios/XCUIAutomationTest.java | 23 ++++++++++++++++--- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index ed0a530fd..e0c7dafe7 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -37,7 +37,7 @@ public class MobileCommand { protected static final String SET_VALUE; protected static final String PULL_FILE; protected static final String PULL_FOLDER; - protected static final String RUN_APP_IN_BACKGROUND; + public static final String RUN_APP_IN_BACKGROUND; protected static final String PERFORM_TOUCH_ACTION; protected static final String PERFORM_MULTI_TOUCH; protected static final String IS_APP_INSTALLED; diff --git a/src/main/java/io/appium/java_client/ios/IOSDriver.java b/src/main/java/io/appium/java_client/ios/IOSDriver.java index ce71c5591..b972bd97f 100644 --- a/src/main/java/io/appium/java_client/ios/IOSDriver.java +++ b/src/main/java/io/appium/java_client/ios/IOSDriver.java @@ -16,6 +16,7 @@ package io.appium.java_client.ios; +import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND; import static io.appium.java_client.MobileCommand.prepareArguments; import io.appium.java_client.AppiumDriver; @@ -164,6 +165,24 @@ public IOSDriver(Capabilities desiredCapabilities) { new TouchAction(this).press(startx, starty).waitAction(duration).moveTo(xOffset, yOffset).release().perform(); } + /** + * Runs the current app as a background app for the number of seconds + * or minimizes the app + * + * @param seconds if seconds >= 0: Number of seconds to run App in background. + * This method call will block main thread and restore the application under + * test after the timeout expires. + * if seconds < 0: any negative number of seconds will put the application + * under test into background and return immediately, so iOS dashboard + * will remain on-screen (this, actually, simulates click on Home button) + */ + @Override public void runAppInBackground(int seconds) { + // timeout parameter is expected to be in milliseconds + // float values are allowed + execute(RUN_APP_IN_BACKGROUND, + prepareArguments("seconds", prepareArguments("timeout", seconds * 1000))); + } + @Override public TargetLocator switchTo() { return new InnerTargetLocator(); } diff --git a/src/test/java/io/appium/java_client/ios/XCUIAutomationTest.java b/src/test/java/io/appium/java_client/ios/XCUIAutomationTest.java index e0be4ebea..b8e20b636 100644 --- a/src/test/java/io/appium/java_client/ios/XCUIAutomationTest.java +++ b/src/test/java/io/appium/java_client/ios/XCUIAutomationTest.java @@ -16,13 +16,18 @@ package io.appium.java_client.ios; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import org.junit.After; import org.junit.Test; import org.openqa.selenium.DeviceRotation; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.empty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertNotNull; + public class XCUIAutomationTest extends AppXCUITTest { @After public void afterMethod() { @@ -51,6 +56,18 @@ public class XCUIAutomationTest extends AppXCUITTest { } } + @Test public void testPutIntoBackgroundAndRestore() { + final long msStarted = System.currentTimeMillis(); + driver.runAppInBackground(4); + assertThat(System.currentTimeMillis() - msStarted, greaterThan(3000L)); + } + + @Test public void testPutIntoBackgroundWithoutRestore() { + assertThat(driver.findElementsById("IntegerA"), is(not(empty()))); + driver.runAppInBackground(-1); + assertThat(driver.findElementsById("IntegerA"), is(empty())); + } + @Test public void doubleTapTest() { IOSElement firstField = driver.findElementById("IntegerA"); firstField.sendKeys("2");