Question

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);

    }
Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top