Javascript isPresent 和 isDisplayed 方法有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28119084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between the isPresent and isDisplayed methods
提问by u4314124
I just started using Protractor to write tests. I am wondering what the difference is between the isPresent()and isDisplayed()methods.
我刚开始使用 Protractor 编写测试。我想知道isPresent()和isDisplayed()方法之间有什么区别。
The API definitions
API 定义
So... in what cases are they different?
那么……在什么情况下它们是不同的?
回答by sap1ens
isPresentis true if element exists in a page (in DOM), but can be hidden (display: none in css) isDisplayedis true only if isPresentis true andelement is visible
isPresent如果在一个网页(DOM)存在元素是真实的,但可以隐藏(显示:无在CSS) isDisplayed是真实的,只有当isPresent是真实的和元素可见
回答by abhishek89m
isDisplayedresolves to whether the element is visible or not, but throws an exception if it is not in the DOM.
isDisplayed解析元素是否可见,但如果它不在 DOM 中则抛出异常。
isPresentresolves to whether it is there in the DOM or not, regardless of whether it is actually visible or not. It doesn't throw an exception.
isPresent解析它是否存在于 DOM 中,无论它是否实际可见。它不会抛出异常。
The following code can be used to avoid the exception that isDisplayed throws if the element is not found in the DOM :
以下代码可用于避免在 DOM 中找不到元素时 isDisplayed 抛出的异常:
function isVisible(e) {
var deferred = protractor.promise.defer();
if (e) {
e.isDisplayed().then(
// isDisplayed Promise resolved
function(isDisplayed) {
deferred.fulfill(isDisplayed);
},
// Silencing the error thrown by isDisplayed.
function(error) {
deferred.fulfill(false);
}
);
}
else {
deferred.reject(new Error('No element passed'));
}
return deferred.promise;
}
Even an object with both the visibility and presence can be passed while resolving, for example :
即使是同时具有可见性和存在性的对象也可以在解析时传递,例如:
deferred.fulfill({
visible: isDisplayed,
present: true
});
However, this won't work well with expect statements.
但是,这不适用于expect 语句。
回答by bsk
IsPresent(): Returns TRUE if element exists in DOM else returns false
IsPresent():如果元素存在于 DOM 中,则返回 TRUE,否则返回 false
IsDisplayed():
被陈列():
- Returns TRUE if element exists in DOM AND is visible.
- Returns FALSE if element exists in DOM and is hidden.
- Throws exception if element does not exist in the DOM
- 如果元素存在于 DOM 并且可见,则返回 TRUE。
- 如果元素存在于 DOM 中并且被隐藏,则返回 FALSE。
- 如果元素在 DOM 中不存在,则抛出异常
回答by pelican
If you get an error when calling isDisplayed()because the element is not on the page, i.e you get NoSuchElementError: No element found using locator, then do this:
如果您在调用时isDisplayed()因为元素不在页面上而出错,即您得到NoSuchElementError: No element found using locator,请执行以下操作:
Simply wrap .isDisplayed()in your own method and handle the unresolved/rejected promiselike below:
简单地包装.isDisplayed()在你自己的方法中并处理unresolved/rejected promise如下:
function isTrulyDisplayed (elementToCheckVisibilityOf) {
return elementToCheckVisibilityOf.isDisplayed().then(function (isDisplayedd) {
return isDisplayedd;
}).then(null, function (error) {
console.log('A NoSuchElement exception was throw because the element is NOT displayed so we return false');
return false;
}); };
Hope this helps someone out there !
希望这可以帮助那里的人!
回答by mashkurm
There is a major difference between isDisplayed() and isPresent().
isDisplayed() 和 isPresent() 之间有一个主要区别。
isDisplayed() - Your element is present on the page but it is displayed.
isDisplayed() - 您的元素存在于页面上,但已显示。
isPresent() - Your element is present in entire DOM of the page. Probably it can be hidden or not disabled, but present.
isPresent() - 您的元素存在于页面的整个 DOM 中。可能它可以隐藏或不禁用,但存在。
You should not use isPresent() when you need to validate on specific element you are searching, rather you can use it to validate some other checks based on that element's presence.
当您需要对正在搜索的特定元素进行验证时,不应使用 isPresent(),而是可以使用它来根据该元素的存在来验证其他一些检查。

