UIATarget.onAlert = function onAlert(alert) Issue - Script doesn't seem to go into block correctly

南楼画角 提交于 2019-12-12 12:17:02

问题


Thanks firstly for taking the time to read and hopefully offer some incite into my issue. At the moment, I'm quite simply trying automate logging in and logging out of an app that my company is making using Instruments. I ran into a few small issues (as you can see with the password entry, I roll char by char instead of using string because of a strange typing issue).

The issue is that when I get the alert pop up to the screen I wish to tap the Logout button. However, it seems that I never enter the block that should be handling the alert. I can deduce this because of the logMessages that I littered in the onAlert block, which do not appear in my logs when the test script is run. I know that the default handler just dismisses any alerts unless the alert is explicitly handled otherwise. I also believe that I am or at least trying to explicitly handle that alert so that I can tap the right button.

What am I doing wrong? I followed the Apple Instruments guide and I believe I'm using the exact same syntax as they offer as an example. Please find my code below, I included all of it, but the block of interest is at the very end.

var target = UIATarget.localTarget();
var password = "something"

UIALogger.logStart("Test1");

target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].textFields()[0].tap();

target.frontMostApp().keyboard().typeString("something");

UIATarget.localTarget().delay(2);

target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].secureTextFields()[0].tap();

UIATarget.localTarget().delay(2);

for (i = 0; i < password.length; i++){

    var strChar = password.charAt(i);
    target.frontMostApp().keyboard().typeString(strChar);

}

target.frontMostApp().mainWindow().scrollViews()[0].webViews()[0].buttons()["Log in"].tap();

target.frontMostApp().mainWindow().tableViews()[0].cells()["Wipe data after 10 attempts"].staticTexts()["Wipe data after 10 attempts"].scrollToVisible();

target.frontMostApp().mainWindow().tableViews()[0].groups()["logout default"].buttons()["logout default"].tap();
// Alert detected. Expressions for handling alerts should be moved into the               UIATarget.onAlert function definition.

UIALogger.logMessage("Just before alert");

UIATarget.onAlert = function onAlert(alert){

    UIALogger.logMessage("In Alert!");

    UIATarget.delay(1);
    var title = alert.name();
    UIALogger.logMessage("Alert with title '" + title + "' encountered!");


    UIALogger.alert.logElementTree();
    alert.buttons()["Logout"].tap();


}

回答1:


I had this issue and eventually found that the automation code was progressing onto the next line before the UI (in this case the alert view) was displayed in the simulator. To mitigate this I added a delay prior to the onAlert block to allow the alert view to be displayed.

UIATarget.localTarget().delay(3)
UIATarget.onAlert = function onAlert(alert){
    var title = alert.name();
    UIALogger.logWarning("Alert with title ’" + title + "’ encountered!");
    target.frontMostApp().alert().cancelButton().tap();
    return false; // use default handler
}



回答2:


You have a few typos.

First, you need to define the alert handling function before you execute your test code. Move it to the top of your test script.

Second, your definition and assignment of a function is slightly incorrect. Instead of this:

UIATarget.onAlert = function onAlert(alert){

You should have this:

UIATarget.onAlert = function (alert){

Finally, this alert handler is only for iOS-level alerts, like permissions popups. I'm making a guess here, but alert.buttons()["Logout"].tap(); looks like something that your own application is creating. You don't need an alert handler for this; simply access it in the element tree like any other UI element, and tap it.



来源:https://stackoverflow.com/questions/17651909/uiatarget-onalert-function-onalertalert-issue-script-doesnt-seem-to-go-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!