简体   繁体   English

如何打破while循环和if语句?

[英]How to break out of a while loop and if statement?

I need to break out both the while loop and if statement when the the value inside the if statement is great than 0, in turn the code should then break from the while loop. 当if语句中的值大于0时,我需要同时打破while循环和if语句,然后代码应再从while循环中打破。

How can I achieve this? 我该如何实现?

My code is as follows: 我的代码如下:

it('x Accordion description should appear after 5 seconds upon being clicked', function(done) {
      //this.timeout(15000);
      //setTimeout(done, 6000);
      var i = 0;
      while (i < 15000) {
          browser.click('#accordion');
          //var timeoutText = browser.getText('#timeout').length;
          var timeoutText = browser.getText('#timeout').length;
          console.log(timeoutText);

            if (timeoutText > 0) {
                  browser.pause(5000);
                  console.log("Pass");
                  break;
                  i++;
            }
      }

Thanks for your help. 谢谢你的帮助。

Use a label to break out 使用label突围

 outer: while (i < 15000) {
    browser.click('#accordion');
    //var timeoutText = browser.getText('#timeout').length;
    var timeoutText = browser.getText('#timeout').length;
    console.log(timeoutText);

    if (timeoutText > 0) {
        browser.pause(5000);
        console.log("Pass");
        break outer;
        i++;
    }
}

Would adding the condition to the while statement work? 将条件添加到while语句是否可行?

it('x Accordion description should appear after 5 seconds upon being clicked', function(done) {
      //this.timeout(15000);
      //setTimeout(done, 6000);
      var i = 0;
      var timeoutText = 1;
      while (i < 15000 && timeoutText <= 0) {
          browser.click('#accordion');              
          timeoutText = browser.getText('#timeout').length;
          console.log(timeoutText);

            if (timeoutText > 0) {
                  browser.pause(5000);
                  console.log("Pass");
                  i++;
            }
      }

Also, if you don't need to run the while loop, this could be written more simply, for ex. 另外,如果您不需要运行while循环,则可以将其编写得更简单,例如。

it('x Accordion description should appear after 5 seconds upon being clicked', function(done) {
      //this.timeout(15000);
      //setTimeout(done, 6000);
      var i = 0;
      var timeoutText = browser.getText('#timeout').length;
      while (i < 15000 && timeoutText <= 0) {
          browser.click('#accordion');              
          timeoutText = browser.getText('#timeout').length;
          console.log(timeoutText);
          browser.pause(5000);
          console.log("Pass");
          i++;
      }
it('x Accordion description should appear after 5 seconds upon being clicked', function() {
    // http://webdriver.io/api/utility/waitUntil.html
    browser.waitUntil(function () {
      // Your condition to wait:
      browser.click('#accordion');
      return browser.getText('#timeout').length > 0
    }, 
    15200, // Waiting timeout 15 secs, but actually only 3 clicks will be done
    'expected accordion description should appear after 5 seconds upon being clicked', 
    5000) // Pooling interval - each 5 secs
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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