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

时间:2018-08-30 07:21:14

标签: javascript if-statement webdriver-io

当if语句中的值大于0时,我需要打断while循环和if语句,然后代码应从while循环中中断。

我该如何实现?

我的代码如下:

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++;
            }
      }

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

使用appendChild来突破

var img = document.createElement('img');
img.src = 'my_image.jpg';
document.getElementById('container').appendChild(img);

答案 1 :(得分:1)

是否将条件添加到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++;
            }
      }

此外,如果您不需要运行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++;
      }

答案 2 :(得分:0)

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
}