UIATarget.onAlert = onAlert function (warning) Problem - Script doesn't seem to work correctly

Thank you, first, for taking the time to read and hopefully suggesting some incitement to my problem. At the moment, I'm just trying to automate logging in and out of an application that my company makes with tools. I ran into a few small problems (as you can see with password writing, I swing char to char instead of using a string due to a strange type problem).

The problem is that when I exit the screen, I want to click the "Exit" button. However, it seems that I never enter the block that should handle the warning. I can do this because of logMessages, which I got clogged in the onAlert block, which do not appear in my logs when running the test script. I know that the default handler simply rejects any warnings unless the warning is explicitly addressed. I also believe that I, or at least am trying to explicitly handle this warning, so that I can press the right button.

What am I doing wrong? I followed the guidance of Apple Instruments, and I believe that I use the same syntax as an example. Please find my code below, I have included it all, 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(); } 
+4
source share
2 answers

I had this problem, and in the end I found that the automation code was advancing to the next line before the user interface (in this case, the warning view) was displayed in the simulator. To mitigate this, I added a delay before the onAlert block to allow the warning to appear.

 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 } 
+3
source

You have a few typos.

First you need to define an alert handling function before executing your test code. Move it to the beginning of the test script.

Secondly, your definition and purpose of a function is somewhat incorrect. Instead of this:

 UIATarget.onAlert = function onAlert(alert){ 

You should have the following:

 UIATarget.onAlert = function (alert){ 

Finally, this alert handler is only for iOS level alerts, such as pop-ups. I guess here, but alert.buttons()["Logout"].tap(); Looks like something created by your own application. You do not need an alert handler for this; just access it in the element tree, like any other user interface element, and touch it.

+2
source

Source: https://habr.com/ru/post/1491408/


All Articles