Skip to content

Instantly share code, notes, and snippets.

@xslim
Created February 22, 2011 22:00
Show Gist options
  • Save xslim/839512 to your computer and use it in GitHub Desktop.
Save xslim/839512 to your computer and use it in GitHub Desktop.
UI Automation sample test file
#import "UIAutomationAddon.js"
/*
* Predefined vars
*/
var userLogin = "U5";
var userPassword = "1111";
/*
* Tests
*/
//var target = UIATarget.localTarget();
//var app = target.frontMostApp();
test("Login screen", function(target, app) {
var window = app.mainWindow();
var view = window.elements()[0];
if (view.elements()[0] instanceof UIAActivityIndicator) {
UIALogger.logMessage("Loading users indicator");
do {
delay(0.2);
} while (window.elements()[0] instanceof UIAPopover);
}
waitVisible(window.elements()[0]);
if (window.elements()[0] instanceof UIAPopover) {
UIALogger.logMessage("UIAPopover found!");
var loginTable;
for (var i in window.elements()[0].elements()) {
if (window.elements()[0].elements()[i] instanceof UIATableView) {
UIALogger.logMessage("Found login table");
loginTable = window.elements()[0].elements()[i];
break;
}
}
assertTrue((loginTable instanceof UIATableView), "Expected UIATableView");
waitVisible(loginTable);
scrollToElementWithNameAndTap(loginTable, userLogin);
}
});
/*
for (var i in window.elements()) {
UIALogger.logMessage(i);
window.elements()[i].logElementTree();
}
*/
test("Logging to app", function(target, app) {
var window = app.mainWindow();
assertTrue(app.keyboard().isVisible(), "Keyboard is open");
// hack !
var passwordField = window.elements()[3].secureTextFields()[0];
assertTrue((passwordField instanceof UIASecureTextField), "Expected UIASecureTextField");
assertTrue(passwordField.hasKeyboardFocus(), "Password field has focus");
passwordField.setValue(userPassword);
var loginButton = window.elements()[3].buttons().firstWithName("Login");
assertTrue((loginButton instanceof UIAButton), "Login button expected");
assertTrue(loginButton.isEnabled(), "Login button should be enabled");
//UIALogger.logDebug(loginButton);
//UIALogger.logDebug(loginButton.name());
//UIALogger.logDebug(loginButton.value());
loginButton.tap();
});
/*
window = UIATarget.localTarget().frontMostApp().mainWindow();
UIALogger.logMessage("dumping window");
UIALogger.logMessage(window.elements().length);
for (var i=0;i< window.elements().length; i++) {
UIALogger.logDebug(window.elements()[i].toString());
}
UIALogger.logDebug(window.elements());
window.logElementTree();
for (var i in UIATarget.localTarget().frontMostApp().elements()) {
UIALogger.logDebug(UIATarget.localTarget().frontMostApp().elements()[i].toString());
}
*/
/*
view = window.elements()[0];
UIALogger.logMessage("dumping view");
view.logElementTree();
*/
//var passwordfields = window.elements().firstWithName("Password");
/*
* Test
*/
function test(title, f, options) {
if (options == null) {
options = {
logTree: true
};
}
target = UIATarget.localTarget();
application = target.frontMostApp();
UIALogger.logStart(title);
try {
f(target, application);
UIALogger.logPass(title);
}
catch (e) {
UIALogger.logError(e);
if (options.logTree) target.logElementTree();
UIALogger.logFail(title);
}
};
function assertEquals(expected, received, message) {
if (received != expected) {
if (! message) message = "Expected " + expected + " but received " + received;
throw message;
}
}
function assertTrue(expression, message) {
if (! expression) {
if (! message) message = "Assertion failed";
throw message;
}
}
function assertFalse(expression, message) {
assertTrue(! expression, message);
}
function assertNotNull(thingie, message) {
if (thingie == null || thingie.toString() == "[object UIAElementNil]") {
if (message == null) message = "Expected not null object";
throw message;
}
}
/*
* Utils
*/
function delay(seconds) {
UIATarget.localTarget().delay(seconds);
}
function tapTab(name) {
var window = UIATarget.localTarget().frontMostApp().mainWindow();
window.tabBar().buttons()[name].tap();
}
// Poll till the item becomes visible, up to a specified timeout
function waitVisible(element) {
waitForVisible(element, 5, 0.25);
}
function waitForVisible(element, timeout, step) {
if (step == null) {
step = 0.5;
}
var stop = timeout/step;
for (var i = 0; i < stop; i++) {
delay(step); // for the animation
if (element.isVisible()) {
return;
}
}
element.logElement();
throw("Not visible");
}
// Allows you to scroll to an element with a particular name and tap it.
function scrollToElementWithNameAndTap(scrollView, name) {
var elementArray = scrollView.elements();
if (scrollView instanceof UIATableView) {
scrollToCellWithNameAndTap(scrollView, name);
return;
} else if (! (scrollView instanceof UIAScrollView)) {
throw ("Expected a UIAScrollView");
}
var e = elementArray.scrollToElementWithName(name);
waitForVisible(e, 5, 0.25);
e.tap();
}
// Allows you to scroll to an cell with a particular name and tap it.
function scrollToCellWithNameAndTap(tableView, name) {
var cellArray = tableView.cells();
if (! (tableView instanceof UIATableView)) {
throw ("Expected a UIAScrollView");
}
var e = cellArray.firstWithName(name);
waitForVisible(e, 5, 0.25);
e.tap();
}
UIAScrollView.prototype.scrollToElementWithNameAndTap = function(name){
scrollToElementWithNameAndTap(this,name)
};
function dumpElements(elements) {
for (var i in elements) {
UIALogger.logDebug(elements[i].toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment