How to create a condition in protractor for when an element exists or not

前端 未结 4 1410
孤独总比滥情好
孤独总比滥情好 2020-12-05 07:20

I\'m using Protractor JS. And the site is written in Angular JS.

So I have a toggle switch. And I noticed the value in the toggle switch goes from true to false and

4条回答
  •  醉梦人生
    2020-12-05 07:35

    Or try this solution implemented from the top of my head, Schedules a command to test if an element is present on the page. If any errors occur while evaluating the wait, they will be allowed to propagate.

    function alwaysSwitchOn(element) {
       browser.driver.isElementPresent(element).then(function(isPresent) {
          if (isPresent) {
            isPresent = true;
          } 
          else {
            browser.driver.wait(function () {
              return browser.driver.isElementPresent(element);
            }, 5000);
          }
          // to fail the test, then uncomment this line
          //expect(isPresent).toBeTruthy();
       }).then(function () {
          if (element.getAttribute('value') === 'OFF') {
             element.click();
          }
          else {
             // turn it OFF
             element.click();
             // turn it back ON
             element.click();
          }
       });
    }
    

    fn usage is to keep trying again and again for 5 seconds till it's true. if the element cannot be found within 5 sec then it'll result in an error code; No such an element is found.Note, If the condition is fulfilled before wait (5s) it'll quickly move to then(...).

提交回复
热议问题