1

Here is what I am trying to do.

1) I have one alertView that asks "where are you shopping?". This alert has two buttons skip/cancel and continue. 2) By clicking the skip button it pops up another alertView with title "Nearby listings:" and shows a tableView list of nearby stores from which user can select any store. This one has only one Cancel button. (cancel button dismisses the alert and takes back to the home page)

My problem is whenever I am trying to bypass the default handler like so

    UIATarget.onAlert = function onAlert(alert) {
        var title = alert.name();
        UIALogger.logWarning("Alert with title ’" + title + "’ encountered!");
        if (title == "Where are you shopping?") {
            alert.buttons()["Skip"].tap();
            return true; // bypass default handler
        }
        return false; 
        }

This taps the skip button and the second alert pops up and the default button is tapped on the second alert even if I have not written any code for it to do so.

I want to tap the skip button of the first alert and tap on one of the cells of the second alert. So I tried the code below but it still dismisses the second alert without tapping the cell. Not sure how to do it. I am a beginner so would really appreciate any help.

    var target = UIATarget.localTarget();
    var app = target.frontMostApp();
    var window = app.mainWindow();
    var testName = "Test 1";

    UIALogger.logStart(testName);


    var buttonScan = target.frontMostApp().windows()[0].buttons()["scan btn"];
    //UIATarget.localTarget().pushTimeout(1);

    target.delay(1);

    //app.logElementTree();

    if (buttonScan.isValid()) {
  buttonScan.tap();

  // first alert box "Where are you shopping"
  UIATarget.onAlert = function onAlert(alert) { // this is never called
      var title = alert.name();
      UIALogger.logWarning("Alert with title ’" + title + "’ encountered!");
      if (title == "Where are you shopping?") {
          alert.buttons()["Skip"].tap();
      return true; // bypass default handler
      }
  return false; 
}

// second alert box "Nearby listings:"
UIATarget.onAlert = function onAlert(alert) {
    var title = alert.name();
UIALogger.logMessage( "Dismiss the keyboard" + title );
    UIALogger.logWarning("Alert2 with title ’" + title + "’ encountered!");
    if (title == "Nearby listings:") {

        return true; // bypass default handler
    }
    return false; // use default handler that is dismissing the alert
   }


      UIALogger.logPass(testName);


   } else {

        UIALogger.logFail(testName);

    }
Lisa Satpathy
  • 23
  • 1
  • 5
  • Have you verified that your `onAlert` handler is being called? Citing the Apple documentation it attempts to click `cancel` if no alert is specified, or if the handler returns false. In your case you would click cancel if both the conditional statement was true or false. So make sure that you can in fact click the `Continue` button on the first alert. – brightintro Jun 20 '13 at 15:52
  • @ekims Thanks for the response. For me the expected behavior is, when I click skip (which is the cancel button of first alert) the second alert should show up with a tableView inside it. Now in this second alert I want to select one of the cells (list of products) that will take me to another screen. – Lisa Satpathy Jun 20 '13 at 18:48
  • I was just suggesting you try and click the `Continue` button on the first alert to make sure that your alert handler is working in the first scenario. Because if no alert handler is specified it defaults to the Cancel button. This will help us narrow down where the problem may be occurring. – brightintro Jun 21 '13 at 01:24
  • @ekim you are right. Thanks for suggesting that. Now I know its not working for the first alert also. Could you tell me whats wrong with the first one? – Lisa Satpathy Jun 21 '13 at 03:28
  • Also the second alert will appear no mater which button (skip or continue)is tapped. I figured out the first alert isn't working from trace log, as it doesn't show the logWarning message. help!!! – Lisa Satpathy Jun 21 '13 at 03:30
  • Can you post your test? As it would be helpful to see where you are setting the onAlert handler. – brightintro Jun 21 '13 at 04:11

1 Answers1

2

Try setting your onAlert handler before you take the instance of UITarget.

For example:

// first alert box "Where are you shopping"
UIATarget.onAlert = function onAlert(alert) { // this is never called
      var title = alert.name();
      UIALogger.logWarning("Alert with title ’" + title + "’ encountered!");
      if (title == "Where are you shopping?") {
          alert.buttons()["Skip"].tap();
          return true; // bypass default handler
      }
  return false; 
}
    var target = UIATarget.localTarget();
    var app = target.frontMostApp();
    var window = app.mainWindow();
    var testName = "Test 1";

    UIALogger.logStart(testName);

    var buttonScan = target.frontMostApp().windows()[0].buttons()["scan btn"];
    //UIATarget.localTarget().pushTimeout(1);

    target.delay(1);
    //app.logElementTree();

    if (buttonScan.isValid()) {
  buttonScan.tap();

   //alert should happen here
brightintro
  • 1,016
  • 1
  • 11
  • 16
  • Hey its working now (by moving the onAlert to the top). I was missing a quote in my last try. Thanks so much man. You have saved me a lot more hours of pain. I would definitely like to be in contact with you for my next UIAutomation ventures. – Lisa Satpathy Jun 21 '13 at 19:37
  • No problem. I have used it on a few work projects and am glad to offer assistance when I can. – brightintro Jun 21 '13 at 19:58