Merge pull request #2118 from bootstraponline/leak

Prevent leaking state across executions
This commit is contained in:
Jonathan Lipps
2014-03-21 09:35:47 -07:00
10 changed files with 210 additions and 189 deletions
@@ -1,18 +1,18 @@
// https://android.googlesource.com/platform/frameworks/testing/+/master/uiautomator_test_libraries/src/com/android/uiautomator/common/UiWatchers.java
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.uiautomator.common;
@@ -30,135 +30,136 @@ import java.util.Collections;
import java.util.List;
public class UiWatchers {
private static final String LOG_TAG = UiWatchers.class.getSimpleName();
private final List<String> mErrors = new ArrayList<String>();
private static final String LOG_TAG = UiWatchers.class.getSimpleName();
private final List<String> mErrors = new ArrayList<String>();
/**
* We can use the UiDevice registerWatcher to register a small script to be executed when the
* framework is waiting for a control to appear. Waiting may be the cause of an unexpected
* dialog on the screen and it is the time when the framework runs the registered watchers.
* This is a sample watcher looking for ANR and crashes. it closes it and moves on. You should
* create your own watchers and handle error logging properly for your type of tests.
*/
public void registerAnrAndCrashWatchers() {
/**
* We can use the UiDevice registerWatcher to register a small script to be
* executed when the framework is waiting for a control to appear. Waiting may
* be the cause of an unexpected dialog on the screen and it is the time when
* the framework runs the registered watchers. This is a sample watcher
* looking for ANR and crashes. it closes it and moves on. You should create
* your own watchers and handle error logging properly for your type of tests.
*/
public void registerAnrAndCrashWatchers() {
UiDevice.getInstance().registerWatcher("ANR", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector().className(
"com.android.server.am.AppNotRespondingDialog"));
String errorText = null;
if (window.exists()) {
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onAnrDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
// class names may have changed
UiDevice.getInstance().registerWatcher("ANR2", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector().packageName("android")
.textContains("isn't responding."));
if (window.exists()) {
String errorText = null;
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onAnrDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
UiDevice.getInstance().registerWatcher("CRASH", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector().className(
"com.android.server.am.AppErrorDialog"));
if (window.exists()) {
String errorText = null;
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onCrashDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
UiDevice.getInstance().registerWatcher("CRASH2", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector().packageName("android")
.textContains("has stopped"));
if (window.exists()) {
String errorText = null;
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onCrashDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
Log.i(LOG_TAG, "Registed GUI Exception watchers");
}
public void onAnrDetected(String errorText) {
mErrors.add(errorText);
}
public void onCrashDetected(String errorText) {
mErrors.add(errorText);
}
public void reset() {
mErrors.clear();
}
public List<String> getErrors() {
return Collections.unmodifiableList(mErrors);
}
/**
* Current implementation ignores the exception and continues.
*/
public void postHandler() {
// TODO: Add custom error logging here
String formatedOutput = String.format("UI Exception Message: %-20s\n", UiDevice
.getInstance().getCurrentPackageName());
Log.e(LOG_TAG, formatedOutput);
UiObject buttonOK = new UiObject(new UiSelector().text("OK").enabled(true));
// sometimes it takes a while for the OK button to become enabled
buttonOK.waitForExists(5000);
try {
buttonOK.click();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "Exception", e);
UiDevice.getInstance().registerWatcher("ANR", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector()
.className("com.android.server.am.AppNotRespondingDialog"));
String errorText = null;
if (window.exists()) {
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onAnrDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
// class names may have changed
UiDevice.getInstance().registerWatcher("ANR2", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector().packageName("android")
.textContains("isn't responding."));
if (window.exists()) {
String errorText = null;
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onAnrDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
UiDevice.getInstance().registerWatcher("CRASH", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector()
.className("com.android.server.am.AppErrorDialog"));
if (window.exists()) {
String errorText = null;
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onCrashDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
UiDevice.getInstance().registerWatcher("CRASH2", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector().packageName("android")
.textContains("has stopped"));
if (window.exists()) {
String errorText = null;
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onCrashDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
Log.i(LOG_TAG, "Registed GUI Exception watchers");
}
public void onAnrDetected(String errorText) {
mErrors.add(errorText);
}
public void onCrashDetected(String errorText) {
mErrors.add(errorText);
}
public void reset() {
mErrors.clear();
}
public List<String> getErrors() {
return Collections.unmodifiableList(mErrors);
}
/**
* Current implementation ignores the exception and continues.
*/
public void postHandler() {
// TODO: Add custom error logging here
String formatedOutput = String.format("UI Exception Message: %-20s\n",
UiDevice.getInstance().getCurrentPackageName());
Log.e(LOG_TAG, formatedOutput);
UiObject buttonOK = new UiObject(new UiSelector().text("OK").enabled(true));
// sometimes it takes a while for the OK button to become enabled
buttonOK.waitForExists(5000);
try {
buttonOK.click();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "Exception", e);
}
}
}
@@ -139,10 +139,10 @@ public class Dynamic {
case SELECTOR_CLASS:
String androidClass = (String) param;
try {
androidClass = AndroidElementClassMap.match((String) param);
androidClass = AndroidElementClassMap.match((String) param);
} catch (final AndroidCommandException e) {
// Couldn't find it in the classmap.
// Let it fall through and error out.
// Couldn't find it in the classmap.
// Let it fall through and error out.
} catch (final UnallowedTagNameException e) {
}
@@ -149,7 +149,7 @@ class SocketServer {
try {
new UiWatchers().registerAnrAndCrashWatchers();
Logger.info("Registered crash watchers.");
} catch(Exception e) {
} catch (Exception e) {
Logger.info("Unable to register crash watchers.");
}
}
@@ -140,6 +140,8 @@ public class Drag extends CommandHandler {
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
// DragArguments is created on each execute which prevents leaking state
// across executions.
final DragArguments dragArgs = new DragArguments(command);
if (command.isElementCommand()) {
@@ -12,17 +12,17 @@ import com.android.uiautomator.core.UiDevice;
/**
* This handler is used to EnableCompressedLayoutHeirarchy.
*
*
*/
public class EnableCompressedLayoutHeirarchy extends CommandHandler {
/*
* @param command The {@link AndroidCommand} used for this handler.
*
*
* @return {@link AndroidCommandResult}
*
*
* @throws JSONException
*
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@@ -33,12 +33,13 @@ import com.android.uiautomator.core.UiSelector;
/**
* This handler is used to find elements in the Android UI.
*
*
* Based on which {@link Strategy}, {@link UiSelector}, and optionally the
* contextId, the element Id or Ids are returned to the user.
*
*
*/
public class Find extends CommandHandler {
// These variables are expected to persist across executions.
AndroidElementsHash elements = AndroidElementsHash.getInstance();
Dynamic dynamic = new Dynamic();
public static JSONObject apkStrings = null;
@@ -60,11 +61,11 @@ public class Find extends CommandHandler {
/*
* @param command The {@link AndroidCommand} used for this handler.
*
*
* @return {@link AndroidCommandResult}
*
*
* @throws JSONException
*
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@@ -213,13 +214,16 @@ public class Find extends CommandHandler {
} catch (final AndroidCommandException e) {
return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
} catch (final ElementNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final UnallowedTagNameException e) {
return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
} catch (final ElementNotInHashException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
}
} else {
try {
@@ -264,9 +268,11 @@ public class Find extends CommandHandler {
} catch (final AndroidCommandException e) {
return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final ElementNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
}
}
}
@@ -274,12 +280,12 @@ public class Find extends CommandHandler {
/**
* Get the element from the {@link AndroidElementsHash} and return the element
* id using JSON.
*
*
* @param sel
* A UiSelector that targets the element to fetch.
* @param contextId
* The Id of the element used for the context.
*
*
* @return JSONObject
* @throws JSONException
* @throws ElementNotFoundException
@@ -295,12 +301,12 @@ public class Find extends CommandHandler {
/**
* Get an array of elements from the {@link AndroidElementsHash} and return
* the element's ids using JSON.
*
*
* @param sel
* A UiSelector that targets the element to fetch.
* @param contextId
* The Id of the element used for the context.
*
*
* @return JSONObject
* @throws JSONException
* @throws UiObjectNotFoundException
@@ -320,7 +326,7 @@ public class Find extends CommandHandler {
/**
* Create and return a UiSelector based on the strategy, text, and how many
* you want returned.
*
*
* @param strategy
* The {@link Strategy} used to search for the element.
* @param text
@@ -400,7 +406,7 @@ public class Find extends CommandHandler {
/**
* Create and return a UiSelector based on Xpath attributes.
*
*
* @param path
* The Xpath path.
* @param attr
@@ -409,7 +415,7 @@ public class Find extends CommandHandler {
* Any constraint.
* @param substr
* Any substr.
*
*
* @return UiSelector
* @throws AndroidCommandException
*/
@@ -16,17 +16,17 @@ import com.android.uiautomator.core.UiDevice;
/**
* This handler is used to get or set the orientation of the device.
*
*
*/
public class Orientation extends CommandHandler {
/*
* @param command The {@link AndroidCommand} used for this handler.
*
*
* @return {@link AndroidCommandResult}
*
*
* @throws JSONException
*
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@@ -53,7 +53,7 @@ public class Orientation extends CommandHandler {
/**
* Returns the current rotation
*
*
* @return {@link AndroidCommandResult}
*/
private AndroidCommandResult getRotation() {
@@ -82,7 +82,7 @@ public class Orientation extends CommandHandler {
/**
* Set the desired rotation
*
*
* @param orientation
* The rotation desired (LANDSCAPE or PORTRAIT)
* @return {@link AndroidCommandResult}
@@ -19,7 +19,8 @@ import io.appium.android.bootstrap.exceptions.ElementNotInHashException;
/**
* This handler is used to scroll to elements in the Android UI.
*
* Based on the element Id of the scrollable, scroll to the object with the text.
* Based on the element Id of the scrollable, scroll to the object with the
* text.
*
*/
public class ScrollTo extends CommandHandler {
@@ -66,26 +67,22 @@ public class ScrollTo extends CommandHandler {
view.waitForExists(5000);
// make sure we can get to the item
UiObject listViewItem = view.getChildByInstance(new UiSelector()
.text(text), 0);
UiObject listViewItem = view.getChildByInstance(
new UiSelector().text(text), 0);
// We need to make sure that the item exists (visible)
if(!(result && listViewItem.exists())) {
return getErrorResult("Could not scroll element into view: "+text);
if (!(result && listViewItem.exists())) {
return getErrorResult("Could not scroll element into view: " + text);
}
return getSuccessResult(result);
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
} catch (final ElementNotInHashException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
} catch (final NullPointerException e) { // el is null
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
} catch (final Exception e) {
return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR,
e.getMessage());
return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
}
}
}
@@ -35,15 +35,15 @@ public abstract class TouchEvent extends CommandHandler {
return fieldObject;
}
protected AndroidElement el = null;
protected AndroidElement el;
protected int clickX = -1;
protected int clickX;
protected int clickY = -1;
protected int clickY;
protected Hashtable<String, Object> params;
protected boolean isElement = false;
protected boolean isElement;
/**
*
@@ -56,6 +56,7 @@ public abstract class TouchEvent extends CommandHandler {
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
initalize();
try {
params = command.params();
@@ -138,6 +139,18 @@ public abstract class TouchEvent extends CommandHandler {
return method;
}
/**
* Variables persist across executions. initialize must be called at the start
* of execute.
**/
private void initalize() {
el = null;
clickX = -1;
clickY = -1;
params = null;
isElement = false;
}
protected void printEventDebugLine(final String methodName,
final Integer... duration) {
String extra = "";
@@ -7,13 +7,14 @@ import io.appium.android.bootstrap.Logger;
public class TheWatchers {
private static TheWatchers ourInstance = new TheWatchers();
private boolean alerted = false;
private boolean alerted = false;
public static TheWatchers getInstance() {
return ourInstance;
}
private TheWatchers() { }
private TheWatchers() {
}
public boolean check() {
// Send only one alert message...
@@ -30,7 +31,8 @@ public class TheWatchers {
}
public boolean isDialogPresent() {
UiObject alertDialog = new UiObject(new UiSelector().packageName("com.android.systemui"));
UiObject alertDialog = new UiObject(
new UiSelector().packageName("com.android.systemui"));
return alertDialog.exists();
}
}