javascript 在量角器中,browser.isElementPresent vs element.isPresent vs element.isElementPresent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33019429/
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
In protractor, browser.isElementPresent vs element.isPresent vs element.isElementPresent
提问by alecxe
In protractor, there are, basically, 3 ways to check if an element is present:
在量角器中,基本上有 3 种方法可以检查元素是否存在:
var elm = element(by.id("myid"));
browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
Are these options equivalent and interchangeable, and which one should be generally preferred?
这些选项是否等效和可互换,通常应该首选哪一个?
回答by Girish Sortur
All function in a similar way with subtle differences. Here are few differences that i found -
所有功能都以类似的方式运行,但有细微的差别。以下是我发现的一些差异 -
- Is an extension of
ElementFinder
and so waits for Angularto settle on page before executing any action. - It works when
elm
is anelement(locator)
orElementFinder
and notElementArrayFinder
. If multiple elements are returned using thelocator
specified then first element is checked if itisEnabled()
in the DOM. Doesn't take any parameter as input. - Works best with Angular pages and Angular elements.
- First preference to use whenever there is a need to find if an element is present.
- 是等的扩展,
ElementFinder
因此在执行任何操作之前等待 Angular在页面上稳定下来。 - 它在
elm
是element(locator)
orElementFinder
和 not时起作用ElementArrayFinder
。如果使用locator
指定的返回多个元素,则检查第一个元素是否isEnabled()
在 DOM 中。不接受任何参数作为输入。 - 最适用于 Angular 页面和 Angular 元素。
- 每当需要查找元素是否存在时使用的首选。
elm.isElementPresent(subLoc)
- (When there is a sub locator to elm
)
elm.isElementPresent(subLoc)
- (当有一个子定位器时elm
)
- Is an extension of
ElementFinder
and so waits for Angular to settle on page before executing any action. - Used to check the presence of elements that are sub elements of a parent. It takes a
sub locator
to the parentelm
as a parameter. (only difference between this and theelm.isPresent()
) - Works best with Angular pages and Angular elements.
- First preference to use whenever there is a need to check if a sub element of a parent is present.
- 是等的扩展,
ElementFinder
因此在执行任何操作之前等待 Angular 在页面上稳定下来。 - 用于检查作为父元素的子元素的元素是否存在。它以
sub locator
父代的elm
a 作为参数。(只有这个和那个的区别elm.isPresent()
) - 最适用于 Angular 页面和 Angular 元素。
- 每当需要检查父元素的子元素是否存在时使用的首选。
browser.isElementPresent(element || Locator)
-
browser.isElementPresent(element || Locator)
——
- Is an implementation of
webdriver
and so doesn't wait for angular to settle. - Takes a
locator
or anelement
as a parameter and uses the first result if multiple elements are located using the same locator. - Best used with Non-Angular pages.
- First preference to use when testing on non-angular pages.
- 是等的实现,
webdriver
所以不等待 angular 稳定下来。 - 如果使用相同的定位器定位多个元素,则将a
locator
或 anelement
作为参数并使用第一个结果。 - 最好与非 Angular 页面一起使用。
- 在非角度页面上测试时使用的首选。
All of the above checks for the presence of an element in DOM and return a boolean
result. Though angular and non-angular features doesn't affect the usage of these methods, but there's an added advantage when the method waits for angular to settle by default and helps avoid errors in case of angular like element not found or state element reference exceptions, etc...
以上所有检查元素在 DOM 中的存在并返回boolean
结果。虽然角度和非角度特征不会影响这些方法的使用,但是当该方法默认等待 angular 稳定时,还有一个额外的优势,有助于避免在找不到 angular 元素或状态元素引用异常的情况下出现错误,等等...
回答by JeffC
I can't speak to which one is preferred but I was able to find the source code and examine it.
我不能说哪个更受欢迎,但我能够找到源代码并检查它。
According to the docs, elm.isPresent()
and elm.isElementPresent()
are equivalent. Hope that helps.
根据文档,elm.isPresent()
和elm.isElementPresent()
是等效的。希望有帮助。
There is a link to View code
just to the right of the title.
View code
标题右侧有一个链接。
browser.isElementPresent(elm);
browser.isElementPresent(elm);
https://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.isElementPresent
https://angular.github.io/protractor/#/api?view=webdriver.WebElement.prototype.isElementPresent
/**
* Schedules a command to test if there is at least one descendant of this
* element that matches the given search criteria.
*
* @param {!(webdriver.Locator|webdriver.By.Hash|Function)} locator The
* locator strategy to use when searching for the element.
* @return {!webdriver.promise.Promise.<boolean>} A promise that will be
* resolved with whether an element could be located on the page.
*/
webdriver.WebElement.prototype.isElementPresent = function(locator) {
return this.findElements(locator).then(function(result) {
return !!result.length;
});
};
elm.isPresent();
榆树.isPresent();
https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isPresent
https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isPresent
/**
* Determine whether the element is present on the page.
*
* @view
* <span>{{person.name}}</span>
*
* @example
* // Element exists.
* expect(element(by.binding('person.name')).isPresent()).toBe(true);
*
* // Element not present.
* expect(element(by.binding('notPresent')).isPresent()).toBe(false);
*
* @return {ElementFinder} which resolves to whether
* the element is present on the page.
*/
ElementFinder.prototype.isPresent = function() {
return this.parentElementArrayFinder.getWebElements().then(function(arr) {
if (arr.length === 0) {
return false;
}
return arr[0].isEnabled().then(function() {
return true; // is present, whether it is enabled or not
}, function(err) {
if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) {
return false;
} else {
throw err;
}
});
}, function(err) {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
return false;
} else {
throw err;
}
});
};
elm.isElementPresent();
榆树.isElementPresent();
https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isElementPresent
https://angular.github.io/protractor/#/api?view=ElementFinder.prototype.isElementPresent
/**
* Same as ElementFinder.isPresent(), except this checks whether the element
* identified by the subLocator is present, rather than the current element
* finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()` is
* identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`.
*
* @see ElementFinder.isPresent
*
* @param {webdriver.Locator} subLocator Locator for element to look for.
* @return {ElementFinder} which resolves to whether
* the subelement is present on the page.
*/
ElementFinder.prototype.isElementPresent = function(subLocator) {
if (!subLocator) {
throw new Error('SubLocator is not supplied as a parameter to ' +
'`isElementPresent(subLocator)`. You are probably looking for the ' +
'function `isPresent()`.');
}
return this.element(subLocator).isPresent();
};
回答by JeffC
You can check whether the element is present or not by using the isPresent function.
您可以使用 isPresent 函数检查元素是否存在。
So, your code would be something like:
因此,您的代码将类似于:
var myElement = element(by.css('.elementClass'));
expect(myElement.isPresent()).toBeFalsy();
Here is the protractor docs for the isPresent function.
这是 isPresent 函数的量角器文档。