Simple protractor test for isElementPresent failing with unsupported locator strategy

后端 未结 4 2047
深忆病人
深忆病人 2021-02-09 22:30

My test:

it(\'should allow login\', function() {
  browser.get(\'index.html\');

  $(\'#username\').sendKeys(\'administrator\');
  $(\'#password\').sendKeys(\'pa         


        
相关标签:
4条回答
  • 2021-02-09 23:09

    $('#logout') is a WebElement. isElementPresent takes a locator, like by.css

    $('#username').sendKeys('administrator');
    $('#password').sendKeys('password');
    $('#login').click();
    
    var logout = by.css('#logout');
    browser.wait(function() { return $p.isElementPresent(logout); }, 8000);
    expect($p.isElementPresent(logout)).toBeTruthy();
    
    0 讨论(0)
  • 2021-02-09 23:09

    The safest approach I would take is depicted in the following code snippet:

    it('should return true when element is present', function () {
     var logout;
     logout = $('#logout');
    
      browser.driver.isElementPresent(logout).then(function (isPresent) {
        isPresent = (isPresent) ? true : browser.wait(function () {
          return browser.driver.isElementPresent(logout );
        }, 15000); //timeout after 15s 
        expect(isPresent).toBeTruthy();
      });
    });
    

    Above code starts of with a promise to check if an element exists, and if true then assign it true, otherwise wait and keep pooling for the next 15sec to see if element is present, and in both cases we expect it to be true.

    0 讨论(0)
  • 2021-02-09 23:21

    Using the latest Protractor build, you can shorten the above answer to the following:

    expect(element(by.css('#logout')).isPresent()).toBeTruthy();
    

    This way you do not have to perform the browser.wait and you reduce the number of calls to isElementPresent.

    0 讨论(0)
  • 2021-02-09 23:29

    This should work :

    var logout = $('#logout');
    expect(logout.isPresent()).to.eventually.be.true;
    
    0 讨论(0)
提交回复
热议问题