Skip to content

Commit e387bec

Browse files
Mykola MokhnachSrinivasanTarget
Mykola Mokhnach
authored andcommitted
Update runAppInBackground method implementation for XCUITest (#593)
1 parent 34dd16f commit e387bec

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MobileCommand {
3737
protected static final String SET_VALUE;
3838
protected static final String PULL_FILE;
3939
protected static final String PULL_FOLDER;
40-
protected static final String RUN_APP_IN_BACKGROUND;
40+
public static final String RUN_APP_IN_BACKGROUND;
4141
protected static final String PERFORM_TOUCH_ACTION;
4242
protected static final String PERFORM_MULTI_TOUCH;
4343
protected static final String IS_APP_INSTALLED;

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.appium.java_client.ios;
1818

19+
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
1920
import static io.appium.java_client.MobileCommand.prepareArguments;
2021

2122
import io.appium.java_client.AppiumDriver;
@@ -164,6 +165,24 @@ public IOSDriver(Capabilities desiredCapabilities) {
164165
new TouchAction(this).press(startx, starty).waitAction(duration).moveTo(xOffset, yOffset).release().perform();
165166
}
166167

168+
/**
169+
* Runs the current app as a background app for the number of seconds
170+
* or minimizes the app
171+
*
172+
* @param seconds if seconds >= 0: Number of seconds to run App in background.
173+
* This method call will block main thread and restore the application under
174+
* test after the timeout expires.
175+
* if seconds < 0: any negative number of seconds will put the application
176+
* under test into background and return immediately, so iOS dashboard
177+
* will remain on-screen (this, actually, simulates click on Home button)
178+
*/
179+
@Override public void runAppInBackground(int seconds) {
180+
// timeout parameter is expected to be in milliseconds
181+
// float values are allowed
182+
execute(RUN_APP_IN_BACKGROUND,
183+
prepareArguments("seconds", prepareArguments("timeout", seconds * 1000)));
184+
}
185+
167186
@Override public TargetLocator switchTo() {
168187
return new InnerTargetLocator();
169188
}

src/test/java/io/appium/java_client/ios/XCUIAutomationTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package io.appium.java_client.ios;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotNull;
21-
2219
import org.junit.After;
2320
import org.junit.Test;
2421
import org.openqa.selenium.DeviceRotation;
2522

23+
import static org.hamcrest.Matchers.greaterThan;
24+
import static org.hamcrest.Matchers.is;
25+
import static org.hamcrest.Matchers.not;
26+
import static org.hamcrest.Matchers.empty;
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertThat;
29+
import static org.junit.Assert.assertNotNull;
30+
2631
public class XCUIAutomationTest extends AppXCUITTest {
2732

2833
@After public void afterMethod() {
@@ -51,6 +56,18 @@ public class XCUIAutomationTest extends AppXCUITTest {
5156
}
5257
}
5358

59+
@Test public void testPutIntoBackgroundAndRestore() {
60+
final long msStarted = System.currentTimeMillis();
61+
driver.runAppInBackground(4);
62+
assertThat(System.currentTimeMillis() - msStarted, greaterThan(3000L));
63+
}
64+
65+
@Test public void testPutIntoBackgroundWithoutRestore() {
66+
assertThat(driver.findElementsById("IntegerA"), is(not(empty())));
67+
driver.runAppInBackground(-1);
68+
assertThat(driver.findElementsById("IntegerA"), is(empty()));
69+
}
70+
5471
@Test public void doubleTapTest() {
5572
IOSElement firstField = driver.findElementById("IntegerA");
5673
firstField.sendKeys("2");

0 commit comments

Comments
 (0)