2

Using selenium and webdriverJS I want to create a test to check if an element is not displayed on the page. To check if one or more elements are present, I decided to use the isDisplayed() function:

driver.findElement(webdriver.By.css('.element')).isDisplayed();
  • Do you know a similar function that returns true if the elements if the element is NOT displayed?
  • isDisplayed() works well when used on a single element. Is there a similar way to check if multiple elements are displayed?

Thanks in advance for your answers!

giri-sh
  • 6,934
  • 2
  • 25
  • 50
d_z90
  • 1,213
  • 2
  • 19
  • 48

4 Answers4

3

isDisplayed() works well when used on a single element. Is there a similar way to check if multiple elements are displayed?

This can be solved by using map(). Example:

element.all(by.css(".element")).map(function (elm) {
    return elm.isDisplayed();
});

This would return a promise resolving into an array of booleans.

If you want a single boolean value, you can use reduce():

element.all(by.css(".element")).reduce(function (acc, elm) {
    return elm.isDisplayed().then(function (isDisplayed) {
        return isDisplayed && acc;
    });
}, false);
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Hey man, nice to see you again :) I will check it out, but a question first: is it also possible to use `Protractor` terminology with `Jasmine`? – d_z90 Sep 25 '15 at 09:04
  • 1
    @d_z90 well, if your web-site is non-angular, you can still use protractor and it's syntactic sugar. Thanks. – alecxe Sep 25 '15 at 13:09
2

To answer your questions -

  • No, there is no function which returns true if element is absent. You can check for negative(false) condition on the existing isDisplayed() method. You can use various test frameworks with webdriverjs and perform the false assertions like jasmine, mocha, etc... How to use webdriverjs with Mocha and testing with jasmine
  • If you want to check if multiple elements are displayed then you can either use inbuilt loops like .each() or .map() or .reduce()(applicable only if you have multiple elements that you can get using a single locator) or you have to check them individually.

Hope this helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • Sounds reasonable. I just have one further question: is there a syntax similar to `jasmine`'s `.not` to check for the negative condition of a function or method? – d_z90 Sep 24 '15 at 10:20
  • There is no such thing in webdriverjs. However you can still use Jasmine with webdriverjs and use such shortcuts. Or you can also use Mocha test framework too. – giri-sh Sep 24 '15 at 10:32
  • They force me to use `jasmine` so I have to stick with it :) Thanks for your help! – d_z90 Sep 24 '15 at 10:57
1

I show how I solved the issue, using jasmine, in case that someone may need it in the future:

driver.findElement(webdriver.By.css('.element')).isDisplayed()
    .then(function(displayed) {
        expect(displayed).not.toBe(true);
    });
d_z90
  • 1,213
  • 2
  • 19
  • 48
1

I found this solution working. Code evaluates presense of element and return true or false. Then I could evaluate this value and perform other stuff on a page. Still have some small error : Failed to create shader cache entry: -2 `

const exit =await driver.findElement(By.css('input[id^="Login"] ')).then(
()=>{return true} ). catch((err=> { if(err.name=== "NoSuchElementError") return false;
throw new Error (" No such element")}));

const check= (elem) => {
    return new Promise( (resolve,reject)=> {
        setTimeout(elem=> { 
            if( elem ===true) {resolve('HA-HA-HA') }
            else {reject(new Error('Not ha-ha-ha'))}
        },500,elem)

       })
   };
`
Artem N
  • 71
  • 4