Skip to content

Commit

Permalink
Update runAppInBackground method implementation for XCUITest
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Mokhnach committed Mar 17, 2017
1 parent 48f3a96 commit 7cdaa59
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
23 changes: 20 additions & 3 deletions src/test/java/io/appium/java_client/ios/XCUIAutomationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 7cdaa59

Please sign in to comment.