在量角器中,browser.isElementPresent vs element.isPresent vs element.isElementPresent

ale*_*cxe 25 javascript selenium end-to-end selenium-webdriver protractor

在量角器中,基本上有3种方法可以检查元素是否存在:

var elm = element(by.id("myid"));

browser.isElementPresent(elm);
elm.isPresent();
elm.isElementPresent();
Run Code Online (Sandbox Code Playgroud)

这些选项是否相同且可以互换,哪个应该是首选?

Gir*_*tur 36

所有功能都以类似的方式发挥微妙的差异.以下是我发现的一些差异 -

elm.isPresent() -

  1. 是一个扩展,ElementFinder因此在执行任何操作之前等待Angular在页面上定居.
  2. 当它elmelement(locator)ElementFinder不是时它起作用ElementArrayFinder.如果使用locator指定的方法返回多个元素,则检查第一个元素是否isEnabled()在DOM中.不接受任何参数作为输入.
  3. 最适用于Angular页面和Angular元素.
  4. 当需要查找元素是否存在时,首先要使用.

elm.isElementPresent(subLoc)- (当有一个子定位器时elm)

  1. 是一个扩展,ElementFinder因此在执行任何操作之前等待Angular在页面上定居.
  2. 用于检查是否存在作为父元素的子元素的元素.它将sub locator父项elm作为参数.(只有这个和之间的区别elm.isPresent())
  3. 最适用于Angular页面和Angular元素.
  4. 每当需要检查父元素的子元素是否存在时,首先使用.

browser.isElementPresent(element || Locator) -

  1. 是一个实现,webdriver所以不等待角度来解决.
  2. 将a locator或a element作为参数并使用第一个结果,如果使用相同的定位器定位多个元素.
  3. 最适用于非角度页面.
  4. 在非角度页面上进行测试时首选使用.

以上所有检查DOM中是否存在元素并返回boolean结果.虽然角度和非角度特征不会影响这些方法的使用,但是当方法默认等待角度稳定时有一个额外的优点,并且有助于避免在未找到角度元素或状态元素引用异常的情况下出现错误,等等...


Jef*_*ffC 5

我不能说哪个更受欢迎,但我能够找到源代码并检查它。

根据文档,elm.isPresent()elm.isElementPresent()是等效的。希望有帮助。

量角器 API 文档

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;
  });
};
Run Code Online (Sandbox Code Playgroud)

榆树.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;
    }
  });
};
Run Code Online (Sandbox Code Playgroud)

榆树.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();
};
Run Code Online (Sandbox Code Playgroud)

  • 我认为downvotes只是复制粘贴源代码而不解释任何东西。 (4认同)