1

ページに ID を持つ要素があります。<p id="blabla">The Text</p>

分度器の elementExplorer を実行すると、つまりprotractor ./config/protractor-conf.js --elementExplorer http://localhost:9001と入力element(by.id('blabla'))すると、要素の結果が得られます (以下にコピー)。

ただし、愚かな id (つまりfoo) を指定すると、一見同じ結果が得られることに気付きました。エラーまたは「見つかりません」というメッセージが表示されることを期待していました。要素が実際に存在するかどうかを確認するにはどうすればよいですか?

すべて element(by.id())の呼び出しの結果:

> element(by.id('foo'))
ElementFinder {
  ptor_:
   Protractor {
     getProcessedConfig: [Function],
     forkNewDriverInstance: [Function],
     restart: [Function],
     controlFlow: [Function],
     schedule: [Function],
     setFileDetector: [Function],
     getSession: [Function],
     getCapabilities: [Function],
     quit: [Function],
     actions: [Function],
     touchActions: [Function],
     executeScript: [Function],
     executeAsyncScript: [Function],
     call: [Function],
     wait: [Function],
     sleep: [Function],
     getWindowHandle: [Function],
     getAllWindowHandles: [Function],
     getPageSource: [Function],
     close: [Function],
     getCurrentUrl: [Function],
     getTitle: [Function],
     findElementInternal_: [Function],
     findElementsInternal_: [Function],
     takeScreenshot: [Function],
     manage: [Function],
     switchTo: [Function],
     driver:
      WebDriver {
        session_: [Object],
        executor_: [Object],
        flow_: [Object],
        fileDetector_: null },
     element: { [Function] all: [Function] },
     '$': [Function],
     '$$': [Function],
     baseUrl: 'http://localhost:9001',
     rootEl: 'body',
     ignoreSynchronization: false,
     getPageTimeout: 10000,
     params: {},
     ready:
      Promise {
        flow_: [Object],
        stack_: null,
        parent_: null,
        callbacks_: null,
        state_: 'fulfilled',
        handled_: true,
        value_: null,
        queue_: null },
     plugins_:
      Plugins {
        setup: [Function],
        teardown: [Function],
        postResults: [Function],
        postTest: [Function],
        onPageLoad: [Function],
        onPageStable: [Function],
        waitForPromise: [Function],
        waitForCondition: [Function],
        pluginObjs: [],
        assertions: {},
        resultsReported: false },
     resetUrl: 'data:text/html,<html></html>',
     trackOutstandingTimeouts_: true,
     mockModules_: [ [Object] ],
     allScriptsTimeout: 11000,
     dbgCodeExecutor_:
      { execPromise_: [Object],
        execPromiseResult_: undefined,
        execPromiseError_: undefined,
        replServer_: [Object],
        execute_: [Function],
        execute: [Function],
        complete: [Function],
        resultReady: [Function],
        getResult: [Function] } },
  then: null,
  parentElementArrayFinder:
   ElementArrayFinder {
     ptor_:
      Protractor {
        getProcessedConfig: [Function],
        forkNewDriverInstance: [Function],
        restart: [Function],
        controlFlow: [Function],
        schedule: [Function],
        setFileDetector: [Function],
        getSession: [Function],
        getCapabilities: [Function],
        quit: [Function],
        actions: [Function],
        touchActions: [Function],
        executeScript: [Function],
        executeAsyncScript: [Function],
        call: [Function],
        wait: [Function],
        sleep: [Function],
        getWindowHandle: [Function],
        getAllWindowHandles: [Function],
        getPageSource: [Function],
        close: [Function],
        getCurrentUrl: [Function],
        getTitle: [Function],
        findElementInternal_: [Function],
        findElementsInternal_: [Function],
        takeScreenshot: [Function],
        manage: [Function],
        switchTo: [Function],
        driver: [Object],
        element: [Object],
        '$': [Function],
        '$$': [Function],
        baseUrl: 'http://localhost:9001',
        rootEl: 'body',
        ignoreSynchronization: false,
        getPageTimeout: 10000,
        params: {},
        ready: [Object],
        plugins_: [Object],
        resetUrl: 'data:text/html,<html></html>',
        trackOutstandingTimeouts_: true,
        mockModules_: [Object],
        allScriptsTimeout: 11000,
        dbgCodeExecutor_: [Object] },
     locator_:
      name(name) {
          return By.css('*[name="' + escapeCss(name) + '"]');
        } { using: 'css selector', value: '*[id="blabla"]' },
     actionResults_: null,
     getWebElements: [Function],
     click: [Function],
     sendKeys: [Function],
     getTagName: [Function],
     getCssValue: [Function],
     getAttribute: [Function],
     getText: [Function],
     getSize: [Function],
     getLocation: [Function],
     isEnabled: [Function],
     isSelected: [Function],
     submit: [Function],
     clear: [Function],
     isDisplayed: [Function],
     getOuterHtml: [Function],
     getInnerHtml: [Function],
     getId: [Function],
     getRawId: [Function],
     serialize: [Function],
     takeScreenshot: [Function] },
  elementArrayFinder_:
   ElementArrayFinder {
     ptor_:
      Protractor {
        getProcessedConfig: [Function],
        forkNewDriverInstance: [Function],
        restart: [Function],
        controlFlow: [Function],
        schedule: [Function],
        setFileDetector: [Function],
        getSession: [Function],
        getCapabilities: [Function],
        quit: [Function],
        actions: [Function],
        touchActions: [Function],
        executeScript: [Function],
        executeAsyncScript: [Function],
        call: [Function],
        wait: [Function],
        sleep: [Function],
        getWindowHandle: [Function],
        getAllWindowHandles: [Function],
        getPageSource: [Function],
        close: [Function],
        getCurrentUrl: [Function],
        getTitle: [Function],
        findElementInternal_: [Function],
        findElementsInternal_: [Function],
        takeScreenshot: [Function],
        manage: [Function],
        switchTo: [Function],
        driver: [Object],
        element: [Object],
        '$': [Function],
        '$$': [Function],
        baseUrl: 'http://localhost:9001',
        rootEl: 'body',
        ignoreSynchronization: false,
        getPageTimeout: 10000,
        params: {},
        ready: [Object],
        plugins_: [Object],
        resetUrl: 'data:text/html,<html></html>',
        trackOutstandingTimeouts_: true,
        mockModules_: [Object],
        allScriptsTimeout: 11000,
        dbgCodeExecutor_: [Object] },
     locator_:
      name(name) {
          return By.css('*[name="' + escapeCss(name) + '"]');
        } { using: 'css selector', value: '*[id="blabla"]' },
     actionResults_: null,
     getWebElements: [Function],
     click: [Function],
     sendKeys: [Function],
     getTagName: [Function],
     getCssValue: [Function],
     getAttribute: [Function],
     getText: [Function],
     getSize: [Function],
     getLocation: [Function],
     isEnabled: [Function],
     isSelected: [Function],
     submit: [Function],
     clear: [Function],
     isDisplayed: [Function],
     getOuterHtml: [Function],
     getInnerHtml: [Function],
     getId: [Function],
     getRawId: [Function],
     serialize: [Function],
     takeScreenshot: [Function] },
  click: [Function],
  sendKeys: [Function],
  getTagName: [Function],
  getCssValue: [Function],
  getAttribute: [Function],
  getText: [Function],
  getSize: [Function],
  getLocation: [Function],
  isEnabled: [Function],
  isSelected: [Function],
  submit: [Function],
  clear: [Function],
  isDisplayed: [Function],
  getOuterHtml: [Function],
  getInnerHtml: [Function],
  getId: [Function],
  getRawId: [Function],
  serialize: [Function],
  takeScreenshot: [Function] }
4

1 に答える 1

1

これは、分度器が設計上どのように機能するかです。要素の使用を開始するまで、「要素が見つかりません」というエラーは表示されません。たとえば、次のように呼び出してみてください。

> element(by.id('something_that_does_not_exist')).getText()

これをサポートするドキュメントの参照は次のとおりです。

element() 関数は ElementFinder オブジェクトを返します。ElementFinder は、パラメーターとして渡されたロケーターを使用して DOM 要素を見つける方法を知っていますが、実際にはまだ行っていません。アクション メソッドが呼び出されるまで、ブラウザに接続しません

于 2016-05-13T20:53:12.260 に答える