constructor()

in lib/element.ts [786:838]


  constructor(public browser_: ProtractorBrowser, elementArrayFinder: ElementArrayFinder) {
    super();
    if (!elementArrayFinder) {
      throw new Error('BUG: elementArrayFinder cannot be empty');
    }
    this.parentElementArrayFinder = elementArrayFinder;

    // Only have a `then` method if the parent element array finder
    // has action results.
    if (this.parentElementArrayFinder.actionResults_) {
      // Access the underlying actionResult of ElementFinder.
      this.then = (fn: (value: any) => any | Promise<any>, errorFn?: (error: any) => any) => {
        return this.elementArrayFinder_.then((actionResults: any) => {
          if (!fn) {
            return actionResults[0];
          }
          return fn(actionResults[0]);
        }, errorFn);
      };
    }

    // This filter verifies that there is only 1 element returned by the
    // elementArrayFinder. It will warn if there are more than 1 element and
    // throw an error if there are no elements.
    const getWebElements = async(): Promise<WebElement[]> => {
      const webElements = await elementArrayFinder.getWebElements();
      if (webElements.length === 0) {
        throw new wderror.NoSuchElementError(
            'No element found using locator: ' + elementArrayFinder.locator().toString());
      } else {
        if (webElements.length > 1) {
          logger.warn(
              'more than one element found for locator ' + elementArrayFinder.locator().toString() +
              ' - the first result will be used');
        }
        return [webElements[0]];
      }
    };

    // Store a copy of the underlying elementArrayFinder, but with the more
    // restrictive getWebElements (which checks that there is only 1 element).
    this.elementArrayFinder_ = new ElementArrayFinder(
        this.browser_, getWebElements, elementArrayFinder.locator(),
        elementArrayFinder.actionResults_);

    WEB_ELEMENT_FUNCTIONS.forEach((fnName: string) => {
      (this)[fnName] = (...args: any[]) => {
        return (this.elementArrayFinder_)[fnName]
            .apply(this.elementArrayFinder_, args)
            .toElementFinder_();
      };
    });
  }