量角器无法获取文本值

Protractor cannot get the text value

我正在尝试获取元素的值。但是我的console.log给出了这样的东西

[launcher] Running 1 instances of WebDriver
Started
{ ptor_:
   { 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],
     findDomElement_: [Function],
     findElementsInternal_: [Function],
     takeScreenshot: [Function],
     manage: [Function],
     switchTo: [Function],
     driver:
      { session_: [Object],
        executor_: [Object],
        flow_: [Object],
        fileDetector_: null },
     element: { [Function] all: [Function] },
     '$': [Function],
     '$$': [Function],
     baseUrl: '',
     rootEl: 'body',
     ignoreSynchronization: true,
     getPageTimeout: 10000,
     params: {},
     ready:
      { closure_uid_676969452: 12,
        flow_: [Object],
        stack_: null,
        parent_: null,
        callbacks_: null,
        state_: 'fulfilled',
        handled_: true,
        value_: null,
        queue_: null },
     plugins_:
      { pluginConfs: [],
        pluginObjs: [],
        assertions: {},
        resultsReported: false },
     resetUrl: 'data:text/html,<html></html>',
     trackOutstandingTimeouts_: true,
     mockModules_: [ [Object] ],
     allScriptsTimeout: 11000,
     getProcessedConfig: [Function],
     forkNewDriverInstance: [Function],
     restart: [Function] },
  parentElementArrayFinder:
   { ptor_:
      { 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],
        findDomElement_: [Function],
        findElementsInternal_: [Function],
        takeScreenshot: [Function],
        manage: [Function],
        switchTo: [Function],
        driver: [Object],
        element: [Object],
        '$': [Function],
        '$$': [Function],
        baseUrl: '',
        rootEl: 'body',
        ignoreSynchronization: true,
        getPageTimeout: 10000,
        params: {},
        ready: [Object],
        plugins_: [Object],
        resetUrl: 'data:text/html,<html></html>',
        trackOutstandingTimeouts_: true,
        mockModules_: [Object],
        allScriptsTimeout: 11000,
        getProcessedConfig: [Function],
        forkNewDriverInstance: [Function],
        restart: [Function] },
     getWebElements: [Function],
     actionResults_:
      { closure_uid_676969452: 253,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: null,
        state_: 'pending',
        handled_: false,
        value_: undefined,
        queue_: null },
     locator_: { using: 'css selector', value: '.top-bar li' } },
  then: [Function],
  elementArrayFinder_:
   { ptor_:
      { 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],
        findDomElement_: [Function],
        findElementsInternal_: [Function],
        takeScreenshot: [Function],
        manage: [Function],
        switchTo: [Function],
        driver: [Object],
        element: [Object],
        '$': [Function],
        '$$': [Function],
        baseUrl: '',
        rootEl: 'body',
        ignoreSynchronization: true,
        getPageTimeout: 10000,
        params: {},
        ready: [Object],
        plugins_: [Object],
        resetUrl: 'data:text/html,<html></html>',
        trackOutstandingTimeouts_: true,
        mockModules_: [Object],
        allScriptsTimeout: 11000,
        getProcessedConfig: [Function],
        forkNewDriverInstance: [Function],
        restart: [Function] },
     getWebElements: [Function],
     actionResults_:
      { closure_uid_676969452: 253,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: null,
        state_: 'pending',
        handled_: false,
        value_: undefined,
        queue_: null },
     locator_: { using: 'css selector', value: '.top-bar li' } } }

我有这个代码

it('it should place order', function () {
   browser.get('https://site.loc/index.php?route=account/login');

   element.all(by.css('.top-bar li')).then(function(items) {
         console.log(items[0].getText());
   });
});

这是我的 html

<ul class="menu">
    <li><a href="https://site.loc/index.php?route=account/logout">Logout</a></li>
    <li><a href="https://site.loc/index.php?route=account/account">My Account</a></li>
    <li><a href="https://site.loc/index.php?route=account/wishlist" id="wishlist-total">Wish List (1)</a></li>
    <li><a href="http://site.loc/index.php?route=checkout/cart">Shopping Cart</a></li>
    <li><a href="https://site.loc/index.php?route=checkout/checkout">Checkout</a></li>
</ul>

我想要的原因在于我想根据我得到的东西做出某些决定。我知道我可以做这样的事情

expect(items.length).toBe(3)

但这对我没有帮助,因为我正在寻找特定的东西,然后根据它做出更多决定

谢谢

webdriver.WebElement.prototype.getTextreturns一个承诺。你需要像

这样的东西
items[0].getText().then(function(text) {
    console.log(text);
});

或者,如果您喜欢简洁

items[0].getText().then(console.log.bind(console));

如果您对记录每个元素及其文本感兴趣,请参阅ElementArrayFinder.prototype.each

element.all(by.css('.top-bar li')).each(function(element, index) {
    element.getText().then(function(text) {
        console.log(index, text);
    });
});

您在控制台上看到的是由 getText() 返回的 promise。如果你想看到真实的文本值,你需要显式解决这个承诺:

element.all(by.css('.top-bar li')).then(function(items) {
    items[0].getText().then(function (text) {
        console.log(text);
    });
});

但是,如果您需要断言元素的文本,您可以将 getText() 直接传递到 expect() - 在 Protractor 中,它被修补以理解承诺:

expect(elm.getText()).toEqual('Logout');

如果需要从元素数组中获取第一个元素,可以使用.first():

var items = element.all(by.css('.top-bar li'));
expect(items.first().getText()).toEqual('Logout');

另外,请注意,您可以对元素数组调用 getText()

expect(items.getText()).toEqual(["Logout", "My Account", ... ]);

要检查项目的数量 - 使用 .count():

expect(items.count()).toEqual(3);