在量角器中,browser.isElementPresent vs element.isPresent vs element.isElementPresent
In protractor, browser.isElementPresent vs element.isPresent vs element.isElementPresent
在量角器中,基本上有 3 种方法来检查元素是否存在:
var elm = element(by.id("myid"));
browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
这些选项是否等效且可以互换,通常应该首选哪一个?
我不能说哪个是首选,但我能够找到源代码并检查它。
根据文档,elm.isPresent()
和 elm.isElementPresent()
是等效的。希望对您有所帮助。
标题右边有一个link到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();
};
您可以使用 isPresent 函数检查元素是否存在。
因此,您的代码将类似于:
var myElement = element(by.css('.elementClass'));
expect(myElement.isPresent()).toBeFalsy();
Here 是 isPresent 函数的量角器文档。
所有功能都以类似的方式运行,但存在细微差别。以下是我发现的一些差异 -
- 是
ElementFinder
的扩展,因此 waits for Angular 在执行任何操作之前先停留在页面上。
- 当
elm
是 element(locator)
或 ElementFinder
而不是 ElementArrayFinder
时有效。如果使用指定的 locator
对多个元素进行 return 编辑,则检查第一个元素是否在 DOM 中 isEnabled()
。不接受任何参数作为输入。
- 最适用于 Angular 个页面和 Angular 个元素。
- 当需要查找元素是否存在时优先使用。
elm.isElementPresent(subLoc)
- (当有子定位器到elm
时)
- 是
ElementFinder
的扩展,因此在执行任何操作之前等待 Angular 进入页面。
- 用于检查父元素的子元素是否存在。它需要一个
sub locator
到父 elm
作为参数。 (这和 elm.isPresent()
的唯一区别)
- 最适用于 Angular 个页面和 Angular 个元素。
- 当需要检查父元素的子元素是否存在时优先使用。
browser.isElementPresent(element || Locator)
-
- 是
webdriver
的实现,因此不等待 angular 解决。
- 将
locator
或 element
作为参数,如果使用同一定位器定位多个元素,则使用第一个结果。
- 最好与非 Angular 页面一起使用。
- 在非 angular 页面上测试时的首选。
以上所有检查 DOM 和 return 中是否存在元素 boolean
结果。虽然 angular 和非 angular 特性不会影响这些方法的使用,但是当方法默认等待 angular 稳定时还有一个额外的优势,有助于避免错误,以防万一angular 类似元素未找到或声明元素引用异常等...
在量角器中,基本上有 3 种方法来检查元素是否存在:
var elm = element(by.id("myid"));
browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
这些选项是否等效且可以互换,通常应该首选哪一个?
我不能说哪个是首选,但我能够找到源代码并检查它。
根据文档,elm.isPresent()
和 elm.isElementPresent()
是等效的。希望对您有所帮助。
标题右边有一个link到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();
};
您可以使用 isPresent 函数检查元素是否存在。
因此,您的代码将类似于:
var myElement = element(by.css('.elementClass'));
expect(myElement.isPresent()).toBeFalsy();
Here 是 isPresent 函数的量角器文档。
所有功能都以类似的方式运行,但存在细微差别。以下是我发现的一些差异 -
- 是
ElementFinder
的扩展,因此 waits for Angular 在执行任何操作之前先停留在页面上。 - 当
elm
是element(locator)
或ElementFinder
而不是ElementArrayFinder
时有效。如果使用指定的locator
对多个元素进行 return 编辑,则检查第一个元素是否在 DOM 中isEnabled()
。不接受任何参数作为输入。 - 最适用于 Angular 个页面和 Angular 个元素。
- 当需要查找元素是否存在时优先使用。
elm.isElementPresent(subLoc)
- (当有子定位器到elm
时)
- 是
ElementFinder
的扩展,因此在执行任何操作之前等待 Angular 进入页面。 - 用于检查父元素的子元素是否存在。它需要一个
sub locator
到父elm
作为参数。 (这和elm.isPresent()
的唯一区别) - 最适用于 Angular 个页面和 Angular 个元素。
- 当需要检查父元素的子元素是否存在时优先使用。
browser.isElementPresent(element || Locator)
-
- 是
webdriver
的实现,因此不等待 angular 解决。 - 将
locator
或element
作为参数,如果使用同一定位器定位多个元素,则使用第一个结果。 - 最好与非 Angular 页面一起使用。
- 在非 angular 页面上测试时的首选。
以上所有检查 DOM 和 return 中是否存在元素 boolean
结果。虽然 angular 和非 angular 特性不会影响这些方法的使用,但是当方法默认等待 angular 稳定时还有一个额外的优势,有助于避免错误,以防万一angular 类似元素未找到或声明元素引用异常等...