How do I test if an img tag exists?

后端 未结 2 1186
无人及你
无人及你 2021-02-13 02:38

if I do expect(img).not.toBe(null) then I get an error: Error: expect called with WebElement argment, expected a Promise. Did you mean to use .getText()?

相关标签:
2条回答
  • 2021-02-13 02:52

    Using the latest Protractor build you can shorten it down to the following:

    expect(element(by.id('recaptcha_image')).isPresent()).toBe(true);
    
    0 讨论(0)
  • 2021-02-13 03:02

    Edit 2:

    Per Coding Smackdown below, an even shorter answer is now available in protractor:

    expect(element(by.id('recaptcha_image')).isPresent()).toBe(true);


    Edit 1:

    I discovered isElementPresent() today which is just a more readable shortcut for what I described below. See: http://www.protractortest.org/#/api

    Usage for you would be:

    driver.isElementPresent(by.id('recaptcha_image')).then(function(present){
        expect(present).toBe(false);
    })
    

    Old answer (this works but the above is more reader friendly)

    In general you should use findElements (or $$ which is an alias for findElements by css) if you're not sure a tag will be there. Then test for the array length. FindElement (and $) will just throw an error if it cant find the element.

    Therefore instead of

    var img =driver.findElement(by.id('recaptcha_image'));
    
    expect(img.getText()).not.toBe(null);
    

    use:

    driver.findElements(by.id('recaptcha_image')).then(function(array){
        expect(array.length).not.toBe(0);
    })
    

    Also, getText() returns a promise which is why you're getting that error.

    0 讨论(0)
提交回复
热议问题