In protractor, browser.isElementPresent vs element.isPresent vs element.isElementPresent

后端 未结 3 786
情歌与酒
情歌与酒 2020-11-27 06:06

In protractor, there are, basically, 3 ways to check if an element is present:

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

browser.isElementPresent(elm);
elm.isPres         


        
相关标签:
3条回答
  • 2020-11-27 06:17

    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.

    Protractor API Docs

    There is a link to View code just to the right of the title.

    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();
    };
    
    0 讨论(0)
  • 2020-11-27 06:19

    All function in a similar way with subtle differences. Here are few differences that i found -

    elm.isPresent() -

    1. Is an extension of ElementFinder and so waits for Angular to settle on page before executing any action.
    2. It works when elm is an element(locator) or ElementFinder and not ElementArrayFinder. If multiple elements are returned using the locator specified then first element is checked if it isEnabled() in the DOM. Doesn't take any parameter as input.
    3. Works best with Angular pages and Angular elements.
    4. First preference to use whenever there is a need to find if an element is present.

    elm.isElementPresent(subLoc) - (When there is a sub locator to elm)

    1. Is an extension of ElementFinder and so waits for Angular to settle on page before executing any action.
    2. Used to check the presence of elements that are sub elements of a parent. It takes a sub locator to the parent elm as a parameter. (only difference between this and the elm.isPresent())
    3. Works best with Angular pages and Angular elements.
    4. First preference to use whenever there is a need to check if a sub element of a parent is present.

    browser.isElementPresent(element || Locator) -

    1. Is an implementation of webdriver and so doesn't wait for angular to settle.
    2. Takes a locator or an element as a parameter and uses the first result if multiple elements are located using the same locator.
    3. Best used with Non-Angular pages.
    4. First preference to use when testing on non-angular pages.

    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...

    0 讨论(0)
  • 2020-11-27 06:31

    You can check whether the element is present or not by using the isPresent function.

    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.

    0 讨论(0)
提交回复
热议问题