在 Protractor 中,基本上有三种方法来检查一个元素是否存在:
var elm = element(by.id("myid"));
browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
这些选项是否等效和可互换,通常应该选择哪个?
在 Protractor 中,基本上有三种方法来检查一个元素是否存在:
var elm = element(by.id("myid"));
browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
这些选项是否等效和可互换,通常应该选择哪个?
所有的函数都有类似的工作方式,但存在微小的差异。以下是我发现的几个差异 -
ElementFinder
的扩展,因此在执行任何操作之前等待Angular在页面上安定下来。 elm
是element(locator)
或ElementFinder
而不是ElementArrayFinder
时才有效。如果使用指定的locator
返回多个元素,则首先检查第一个元素是否在DOM中isEnabled()
。不接受任何参数作为输入。elm.isElementPresent(subLoc)
- (当有子定位器subLoc
时)
ElementFinder
的扩展,因此在执行任何操作之前等待Angular在页面上稳定。elm
的sub locator
作为参数。(与elm.isPresent()
之间的唯一区别)browser.isElementPresent(element || Locator)
-
定位器
或元素
作为参数,并使用第一个结果,如果使用相同的定位器定位到多个元素,则只使用第一个。以上所有内容都检查DOM中元素的存在并返回一个布尔结果。虽然angular和非angular特性不影响这些方法的使用,但默认情况下该方法等待angular稳定,可以帮助避免出现类似于未找到元素或状态元素引用异常等angular问题而导致的错误。
我不确定哪个更受欢迎,但我能够找到源代码并检查它。
根据文档,elm.isPresent()
和 elm.isElementPresent()
是等效的。希望这能够帮助你。
在标题右侧有一个名为 View code
的链接。
browser.isElementPresent(elm);
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();
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();
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();
};
elm.isPresent()
中的错误代码。 耸肩 - JeffCvar myElement = element(by.css('.elementClass'));
expect(myElement.isPresent()).toBeFalsy();
这里是关于isPresent函数的protractor文档。