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 singleelement
. Is there a similar way to check if multiple elements are displayed?
Thanks in advance for your answers!
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 singleelement
. Is there a similar way to check if multiple elements are displayed?
Thanks in advance for your answers!
Share Improve this question edited Oct 23, 2015 at 9:38 giri-sh 6,9622 gold badges27 silver badges50 bronze badges asked Sep 24, 2015 at 9:50 d_z90d_z90 1,2632 gold badges20 silver badges49 bronze badges4 Answers
Reset to default 3isDisplayed() 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);
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.
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);
});
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)
})
};
`
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744247929a4565028.html
评论列表(0条)