Verifying the existence of a button object using Javascript, in Xcode UIAutomation and Instruments
For the quality testing of our project on iOS 4.2, we are working with UIAutomation though Instruments in Xcode 3.x. We are writing our scripts in Javascript. I am new to Javascript, and have found the UIAutomation documentation to be (how shall I put this?), "sparse".
I am hoping that some genius in the Ether may be able to enlighten me as to how to verify the existence of a button named 'beep sound' when it is displayed on the main window of our iOS application?
Also has anyone out there foun开发者_StackOverflowd any good references for writing test scripts (as opposed to dynamic web pages) in JavaScript?
Thanks for any and all assistance!
Regards,
Steve O'Sullivan
Hey.
Actually documentation (this and this) from apple is the only thing I could find.
As for your question try
if(UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].name() === "beep sound")) {
UIALogger.logPass("Buton Present");
} else {
UIALogger.logFail("Buton Not Present");
};
Of course, that assumes (elements()[0]) that your button is first in object tree under main window. If it is not, you may need to call other element ((elements()3), or you may need to call deeper into hierarchy (elements()[0].elements()3).
Keep in mind that code above will fail if one of the objects in chain won't be present. You may need to check every object in chain. Additionally you may need to check if given button is not only present but if it visible on the screen. In this case code above may need to look like this:
if(UAITarget.localTarget().frontMostApplication().mainWindow() && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0] && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate("name matches 'beep sound'")) {
if(UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].isVisible()) {
UIALogger.logPass("Buton Present");
} else {
UIALogger.logFail("Buton Present, but Not Visible");
}
} else {
UIALogger.logFail("Buton Not Present");
};
But now readability, maintainability, and over -ity attributes of code suffer. So I would refactor it to:
function isButtonWithPredicate (predicate) {
if(UAITarget.localTarget().frontMostApplication().mainWindow() && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0] && UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate(predicate)) {
return true;
} else {
throw new Error("button not found, predicate: " + predicate);
}
function getButtonWithPredicate (predicate) {
try {
if(isButtonWithPredicate(predicate)) {
return UAITarget.localTarget().frontMostApplication().mainWindow().elements()[0].withPredicate(predicate);
}
} catch (error) {
throw new Error("getButtonWithPredicateError: " + error.message);
};
}
var strpredicate = "name matches 'beep sound'";
var objButton = null;
try{
objButton = getButtonWithPredicate(strPredicate);
if(objButton.isVisible) {
UIALogger.logPass("Buton Present");
};
} catch(error) {
UIALogger.logFail(error.message);
}
Of course you can still improve it... but you should get the idea.
btw apple guide to predicates
P.S. Code was written in notepad, and wasn't checked so it may contain some parse errors.
精彩评论