Skip to content

Commit

Permalink
Merge pull request #54 from sravanmedarapu/master
Browse files Browse the repository at this point in the history
Added waitForIdle utility method
  • Loading branch information
sravanmedarapu authored Apr 7, 2017
2 parents b06fdfa + 7a1be50 commit 84dfac1
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.appium.uiautomator2.server.ServerConfig;
import io.appium.uiautomator2.server.ServerInstrumentation;
import io.appium.uiautomator2.server.WDStatus;
import io.appium.uiautomator2.utils.Device;
import io.appium.uiautomator2.utils.Logger;

import static io.appium.uiautomator2.unittest.test.TestHelper.getJsonObjectCountInJsonArray;
Expand Down Expand Up @@ -348,7 +349,7 @@ public void getTextTest() throws JSONException {

@Test
public void sendKeysTest() throws JSONException, InterruptedException {
getUiDevice().waitForIdle();
Device.waitForIdle();
scrollTo("Views"); // Due to 'Views' option not visible on small screen

waitForElement(By.accessibilityId("Views"), 10 * SECOND);
Expand All @@ -373,7 +374,7 @@ public void sendKeysTest() throws JSONException, InterruptedException {
*/
@Test
public void getNameTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();
waitForElement(By.id("android:id/text1"), 5 * SECOND);
String response = getName(findElement(By.id("android:id/text1")));
assertEquals("Accessibility", getStringValueInJsonObject(response, "value"));
Expand All @@ -386,7 +387,7 @@ public void getNameTest() throws JSONException {
*/
@Test
public void getElementSizeTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();
waitForElement(By.id("android:id/text1"), 5 * SECOND);
response = getSize(findElement(By.id("android:id/text1")));
Integer height = JsonPath.compile("$.value.height").read(response);
Expand All @@ -402,7 +403,7 @@ public void getElementSizeTest() throws JSONException {
*/
@Test
public void getDeviceSizeTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();
response = getDeviceSize();
Integer height = JsonPath.compile("$.value.height").read(response);
Integer width = JsonPath.compile("$.value.width").read(response);
Expand All @@ -417,10 +418,10 @@ public void getDeviceSizeTest() throws JSONException {
*/
@Test
public void flickOnElementTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();
waitForElement(By.id("android:id/text1"), 5 * SECOND);
response = flickOnElement(findElement(By.id("android:id/text1")));
getUiDevice().waitForIdle();
Device.waitForIdle();
waitForElement(By.accessibilityId("Custom View"), 10 * SECOND);
assertTrue(JsonPath.compile("$.value").<Boolean>read(response));
}
Expand All @@ -432,7 +433,7 @@ public void flickOnElementTest() throws JSONException {
*/
@Test
public void flickTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();
response = flickOnPosition();
assertTrue(JsonPath.compile("$.value").<Boolean>read(response));
}
Expand Down Expand Up @@ -522,7 +523,7 @@ public void multiPointerGestureTest() throws InterruptedException, JSONException
*/
@Test
public void swipeTest() throws JSONException, InterruptedException {
getUiDevice().waitForIdle();
Device.waitForIdle();
scrollTo("Views"); // Due to 'Views' option not visible on small screen
waitForElement(By.accessibilityId("Views"), 10 * SECOND);
click(findElement(By.accessibilityId("Views")));
Expand Down Expand Up @@ -563,7 +564,7 @@ public void touchLongClickTest() throws JSONException {
Logger.info("[AppiumUiAutomator2Server]", "long click element:" + element);
assertTrue(By.accessibilityId("Accessibility") + " not found", isElementPresent(element));
longClick(element);
getUiDevice().waitForIdle();
Device.waitForIdle();
waitForElementInvisible(By.accessibilityId("Accessibility"), 5 * SECOND);
element = findElement(By.accessibilityId("Accessibility"));
assertFalse(By.accessibilityId("Accessibility") + " found", isElementPresent(element));
Expand All @@ -577,7 +578,7 @@ public void touchLongClickTest() throws JSONException {
*/
@Test
public void scrollTest() throws JSONException, InterruptedException {
getUiDevice().waitForIdle();
Device.waitForIdle();
scrollTo("Views"); // Due to 'Views' option not visible on small screen
waitForElement(By.accessibilityId("Views"), 10 * SECOND);
click(findElement(By.accessibilityId("Views")));
Expand Down Expand Up @@ -608,7 +609,7 @@ public void appStringsTest() throws JSONException {
*/
@Test
public void screenRotationTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();

rotateScreen("LANDSCAPE");
assertEquals("LANDSCAPE", getScreenOrientation());
Expand Down Expand Up @@ -678,7 +679,7 @@ public void verify500HTTPStatusCode() throws JSONException, IOException {

@Test
public void touchActionsTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();

scrollTo("Views"); // Due to 'Views' option not visible on small screen
waitForElement(By.accessibilityId("Views"), 10 * SECOND);
Expand Down Expand Up @@ -754,7 +755,7 @@ public void findElementWithContextId() throws JSONException {
assertEquals("Animation", elementTxt);

click(findElement(By.accessibilityId("Animation")));
getUiDevice().waitForIdle();
Device.waitForIdle();
waitForElement(By.accessibilityId("Events"), 5 * SECOND);
click(findElement(By.accessibilityId("Events")));
waitForElement(By.xpath("//*[@class='android.widget.LinearLayout'][3]"), 5 * SECOND);
Expand All @@ -774,7 +775,7 @@ public void findElementWithAttributes() throws JSONException {
scrollTo("Views");
click(findElement(By.accessibilityId("Views")));
waitForElement(By.accessibilityId("Focus"), 10 * SECOND);
getUiDevice().waitForIdle();
Device.waitForIdle();

element = findElement(By.accessibilityId("Focus"));
Logger.info("[AppiumUiAutomator2Server]", " findElement By.accessibilityId: " + element);
Expand Down Expand Up @@ -828,7 +829,7 @@ public void findElementsWithAttribute() throws JSONException {

@Test
public void toastVerificationTest() throws JSONException {
getUiDevice().waitForIdle();
Device.waitForIdle();
scrollTo("Views"); // Due to 'Views' option not visible on small screen
waitForElement(By.accessibilityId("Views"), 10 * SECOND);
click(findElement(By.accessibilityId("Views")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;

import io.appium.uiautomator2.server.ServerConfig;
import io.appium.uiautomator2.utils.Device;
import io.appium.uiautomator2.utils.Logger;

import static android.os.SystemClock.elapsedRealtime;
Expand Down Expand Up @@ -126,7 +127,7 @@ public static void waitForAppToLaunch(String testAppPkg, int LAUNCH_TIMEOUT) thr
long start = elapsedRealtime();
boolean waitStatus;
do {
getUiDevice().waitForIdle();
Device.waitForIdle();
waitStatus = getUiDevice().wait(Until.hasObject(By.pkg(testAppPkg).depth(0)), LAUNCH_TIMEOUT);
if (waitStatus) break;
} while ((elapsedRealtime() - start < LAUNCH_TIMEOUT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.appium.uiautomator2.model.By;
import io.appium.uiautomator2.server.WDStatus;
import io.appium.uiautomator2.utils.Device;
import io.appium.uiautomator2.utils.Logger;

import static android.os.SystemClock.elapsedRealtime;
Expand Down Expand Up @@ -383,7 +384,7 @@ public static void startActivity(Context ctx, String packg, String activity) thr
ctx.startActivity(intent);
Logger.info("[AppiumUiAutomator2Server]", " waiting for activity to launch ");
TestHelper.waitForAppToLaunch(packg, 15 * SECOND);
getUiDevice().waitForIdle();
Device.waitForIdle();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/appium/uiautomator2/handler/Click.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.appium.uiautomator2.model.AndroidElement;
import io.appium.uiautomator2.model.KnownElements;
import io.appium.uiautomator2.server.WDStatus;
import io.appium.uiautomator2.utils.Device;
import io.appium.uiautomator2.utils.Logger;
import io.appium.uiautomator2.utils.Point;
import io.appium.uiautomator2.utils.PositionHelper;
Expand Down Expand Up @@ -45,7 +46,7 @@ public AppiumResponse safeHandle(IHttpRequest request) {
final boolean res = getUiDevice().click(coords.x.intValue(), coords.y.intValue());
return new AppiumResponse(getSessionId(request), res);
}
getUiDevice().waitForIdle();
Device.waitForIdle();
} catch (UiObjectNotFoundException e) {
Logger.error("Element not found: ", e);
return new AppiumResponse(getSessionId(request), WDStatus.NO_SUCH_ELEMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.appium.uiautomator2.model.XPathFinder;
import io.appium.uiautomator2.model.internal.NativeAndroidBySelector;
import io.appium.uiautomator2.server.WDStatus;
import io.appium.uiautomator2.utils.Device;
import io.appium.uiautomator2.utils.Logger;
import io.appium.uiautomator2.utils.NodeInfoList;
import io.appium.uiautomator2.utils.UiAutomatorParser;
Expand Down Expand Up @@ -93,7 +94,7 @@ public AppiumResponse safeHandle(IHttpRequest request) {
Logger.info(String.format("find element command using '%s' with selector '%s'.", method, selector));
final By by = new NativeAndroidBySelector().pickFrom(method, selector);

getUiDevice().waitForIdle();
Device.waitForIdle();
Object element;
if(contextId.length() > 0) {
element = this.findElement(by, contextId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public AppiumResponse safeHandle(IHttpRequest request) {
final String contextId = payload.getString("context");
Logger.info(String.format("find element command using '%s' with selector '%s'.", method, selector));
By by = new NativeAndroidBySelector().pickFrom(method, selector);
getUiDevice().waitForIdle();
Device.waitForIdle();
List<Object> elements ;
if(contextId.length() > 0) {
elements = this.findElements(by, contextId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AppiumResponse safeHandle(IHttpRequest request) {
String json = getPayload(request).toString();
String selector = "$.params.selector", uiSelectorString, scrollToString = "";
uiSelectorString = JsonPath.compile(selector).read(json);
Device.getUiDevice().waitForIdle();
Device.waitForIdle();
// Extracting (\"Radio Group\") text from the String
// TODO This logic needs to be changed according to the request body from the Driver
Matcher m = Pattern.compile("\\(\"([^)]+)\"\\)").matcher(uiSelectorString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AppiumResponse safeHandle(IHttpRequest request) {
if (payload.has("timeout")) {
timeout = Integer.parseInt(payload.getString("timeout"));
}
Device.getUiDevice().waitForIdle(timeout);
Device.waitForIdle(timeout);
} catch (JSONException e) {
Logger.error("Unable to get timeout value from the json payload", e);
return new AppiumResponse(getSessionId(request), WDStatus.UNKNOWN_ERROR, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static void refreshUiElementTree(AccessibilityNodeInfo nodeInfo) {

public static AccessibilityNodeInfo getRootAccessibilityNode() throws UiAutomator2Exception {
final long timeoutMillis = 10000;
Device.getUiDevice().waitForIdle(timeoutMillis);
Device.waitForIdle(timeoutMillis);

long end = SystemClock.uptimeMillis() + timeoutMillis;
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Instrumentation getInstrumentation(){
public Object findObject(Object selector) throws ClassNotFoundException, ElementNotFoundException, InvalidSelectorException, UiAutomator2Exception {

AccessibilityNodeInfo node ;
getUiDevice().waitForIdle();
Device.waitForIdle();
if (selector instanceof BySelector) {
node = (AccessibilityNodeInfo) invoke(METHOD_FIND_MATCH, ByMatcher, Device.getUiDevice(), selector, getWindowRoots());
} else if (selector instanceof NodeInfoList) {
Expand Down Expand Up @@ -187,7 +187,7 @@ public List<Object> findObjects(Object selector) throws ClassNotFoundException,
* Returns a list containing the root {@link AccessibilityNodeInfo}s for each active window
*/
AccessibilityNodeInfo[] getWindowRoots() throws UiAutomator2Exception {
getUiDevice().waitForIdle();
Device.waitForIdle();
ArrayList<AccessibilityNodeInfo> ret = new ArrayList<>();
/**
* TODO: MULTI_WINDOW is disabled, UIAutomatorViewer captures active window properties and
Expand Down Expand Up @@ -222,7 +222,7 @@ AccessibilityNodeInfo[] getWindowRoots() throws UiAutomator2Exception {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
getUiDevice().waitForIdle();
Device.waitForIdle();
Logger.debug(" ERROR: null root node returned by UiTestAutomationBridge, retrying: " + retryCount);
node = mInstrumentation.getUiAutomation().getRootInActiveWindow();
retryCount++;
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/io/appium/uiautomator2/utils/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,29 @@ public static void scrollTo(String scrollToString) throws UiObjectNotFoundExcept
public static boolean back() {
return getUiDevice().pressBack();
}

/**
* reason for explicit method, in some cases google UiAutomator2 throwing exception
* while calling waitForIdle() which is causing appium UiAutomator2 server to fall in
* unexpected behaviour.
* for more info please refer
* https://code.google.com/p/android/issues/detail?id=73297
*/
public static void waitForIdle() {
try {
getUiDevice().waitForIdle();
}catch (Exception e) {
Logger.error("Unable wait for AUT to idle");
}
}

public static void waitForIdle(long timeInMS) {
try {
getUiDevice().waitForIdle(timeInMS);
}catch (Exception e) {
Logger.error(String.format("Unable wait %d for AUT to idle", timeInMS));
}
}


}

0 comments on commit 84dfac1

Please sign in to comment.