简体   繁体   中英

Using a loop for multiple buttons in Protractor

Currently I am working on a Protractor project that has multiple or just one, recurring, non-problematic, error messages that must be cleared (clicked OK) before continuing with the rest of the tests. The error messages, which show at the beginning of the page load, can at times be more numerous than one, and I wish to "click out" of all of them. The code for one was simple:

alertMessages['alertMessage'].isPresent().then(function (isHere) {
                        if (!isHere) {
                           alertMessages['alertMessageOKButton'].click();
                        }
                    });

I attempted a simple loop around it which, for some reason beyond my knowledge, worked for a time and has now ceased to work appropriately.

I think you would like to test an alert boxes. If I am right, use browser.switchTo().alert().accept();

For more information please visit this link: https://github.com/angular/protractor/blob/3d60689b423749686973227f4b26cbb494f60b66/spec/basic/navigation_spec.js#L8

And see browse for more information in here: https://angular.github.io/protractor/#/api?view=webdriver.WebDriver.TargetLocator.prototype.alert

You can use async.whilst . Basically keep on clicking until all alerts are dismissed. This is somewhat modification of your code (btw I don't know what alertMessages is). But I hope you get the idea.

async.whilst(
    function () { 
        return alertMessages['alertMessage'].isPresent().then(function(isPresent){
            return isPresent;
    }) },
    function (callback) {
      alertMessages['alertMessageOKButton'].click();
    },
    function (err, n) {
      console.log('Total number of alerts were '+ n);
      if(err) {
         done(err);
      } else {
        done();
      }
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM