admin管理员组文章数量:1130349
In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:
NoSuchElementError: No element found using locator: By.id("userForm")
My code is:
describe('closeModal', function() {
it('should close the alert that appears after registration.', function(){
element(by.id('closeAlertModalButton')).click();
expect(element(by.id('userForm')).isDisplayed()).toBeFalsy();
});
});
I understand that I get that error because element is not longer on the page (is what I want to confirm), but shouldn't I get a false and not a error?
In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:
NoSuchElementError: No element found using locator: By.id("userForm")
My code is:
describe('closeModal', function() {
it('should close the alert that appears after registration.', function(){
element(by.id('closeAlertModalButton')).click();
expect(element(by.id('userForm')).isDisplayed()).toBeFalsy();
});
});
I understand that I get that error because element is not longer on the page (is what I want to confirm), but shouldn't I get a false and not a error?
Share Improve this question asked May 7, 2015 at 11:35 MikelMikel 6,2025 gold badges36 silver badges50 bronze badges 1- You should better use isElementPresent or isPresent - error you've got is part of WebDriver logic – Vasiliy vvscode Vanchuk Commented May 7, 2015 at 11:39
4 Answers
Reset to default 36isDisplayed() would check if an element is visible or not, but you need to check whether an element is present in DOM or not, use isElementPresent() or isPresent():
expect(browser.isElementPresent(element(by.id('userForm')))).toBe(false);
expect(element(by.id('userForm')).isPresent()).toBe(false);
See also:
- How do I test if an img tag exists?
- Use element by css to check if element exists in Protractor
This error is part of WebDriver behavior. For such cases you should better use isPresent or isElementPresent
If element visible do A if not visible do B, disregard exception if element not found:
element.isDisplayed().then(function(visible){
if (visible) {
// do A when element visible
}else{
// do B when element not visible
}
}, function () {
//suppress exception if element is not found on page
});
.isDisplayed() assumes the element is present (exists in the DOM)
so if you do
expect($('[ng-show=saving]').isDisplayed()).toBe(true);
but the element is not present, then instead of graceful failed expectation, $('[ng-show=saving]').isDisplayed() will throw an error causing the rest of it block not executed
Solution
If you assume, the element you're checking may not be present for any reason on the page, then go with a safe way below
/**
* element is Present and is Displayed
* @param {ElementFinder} $element Locator of element
* @return {boolean}
*/
let isDisplayed = function ($element) {
return (await $element.isPresent()) && (await $element.isDisplayed())
}
and use
expect(await isDisplayed( $('[ng-show=saving]') )).toBe(true);
In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:
NoSuchElementError: No element found using locator: By.id("userForm")
My code is:
describe('closeModal', function() {
it('should close the alert that appears after registration.', function(){
element(by.id('closeAlertModalButton')).click();
expect(element(by.id('userForm')).isDisplayed()).toBeFalsy();
});
});
I understand that I get that error because element is not longer on the page (is what I want to confirm), but shouldn't I get a false and not a error?
In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:
NoSuchElementError: No element found using locator: By.id("userForm")
My code is:
describe('closeModal', function() {
it('should close the alert that appears after registration.', function(){
element(by.id('closeAlertModalButton')).click();
expect(element(by.id('userForm')).isDisplayed()).toBeFalsy();
});
});
I understand that I get that error because element is not longer on the page (is what I want to confirm), but shouldn't I get a false and not a error?
Share Improve this question asked May 7, 2015 at 11:35 MikelMikel 6,2025 gold badges36 silver badges50 bronze badges 1- You should better use isElementPresent or isPresent - error you've got is part of WebDriver logic – Vasiliy vvscode Vanchuk Commented May 7, 2015 at 11:39
4 Answers
Reset to default 36isDisplayed() would check if an element is visible or not, but you need to check whether an element is present in DOM or not, use isElementPresent() or isPresent():
expect(browser.isElementPresent(element(by.id('userForm')))).toBe(false);
expect(element(by.id('userForm')).isPresent()).toBe(false);
See also:
- How do I test if an img tag exists?
- Use element by css to check if element exists in Protractor
This error is part of WebDriver behavior. For such cases you should better use isPresent or isElementPresent
If element visible do A if not visible do B, disregard exception if element not found:
element.isDisplayed().then(function(visible){
if (visible) {
// do A when element visible
}else{
// do B when element not visible
}
}, function () {
//suppress exception if element is not found on page
});
.isDisplayed() assumes the element is present (exists in the DOM)
so if you do
expect($('[ng-show=saving]').isDisplayed()).toBe(true);
but the element is not present, then instead of graceful failed expectation, $('[ng-show=saving]').isDisplayed() will throw an error causing the rest of it block not executed
Solution
If you assume, the element you're checking may not be present for any reason on the page, then go with a safe way below
/**
* element is Present and is Displayed
* @param {ElementFinder} $element Locator of element
* @return {boolean}
*/
let isDisplayed = function ($element) {
return (await $element.isPresent()) && (await $element.isDisplayed())
}
and use
expect(await isDisplayed( $('[ng-show=saving]') )).toBe(true);
本文标签:
版权声明:本文标题:javascript - Protractor, with isDisplayed() I get NoSuchElementError: No element found using locator - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1737519344a1477693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论