量角器返回一个对象但是期望 - element.getText()的值

时间:2015-03-02 14:01:10

标签: javascript selenium-webdriver promise protractor end-to-end

无法理解为什么它返回一个对象而不是文本值,一些测试代码:

describe('columns swap', function () {

    describe('location column ', function () {
        it('should swap right', function () {
            browser.sleep(10000);
            var fColumn = element(by.xpath('(//*[@class="k-link"])[2]')).getText();
            console.log(fColumn); 

控制台输出:

>   columns swap
>     location column { ptor_:    { controlFlow: [Function],
>      schedule: [Function],
>      getSession: [Function],
>      getCapabilities: [Function],
>      quit: [Function],
>      actions: [Function],
>      executeScript: [Function],
>      executeAsyncScript: [Function],
>      call: [Function],
>      wait: [Function],
>      sleep: [Function],
>      getWindowHandle: [Function],
>      getAllWindowHandles: [Function],
>      getPageSource: [Function],
>      close: [Function],
>      getCurrentUrl: [Function], ...

此外,如果我将此部分用于expect():

    expect(columnSorting.acpColumn.getText()).to.eventually.equal(fColumn);

我明白了:

  1) columns swap location column  should swap right:
     AssertionError: expected 'Location' to equal { Object (ptor_, parentElement
ArrayFinder, ...) }

因此,出于某种原因,我可以从期望中得到文本并且它正确 - “位置”

我做错了什么?

1 个答案:

答案 0 :(得分:13)

getText() 返回承诺。如果要记录实际值,则需要解决它:

element(by.xpath('(//*[@class="k-link"])[2]')).getText().then(function (value) {
    console.log(value);

    expect(columnSorting.acpColumn.getText()).to.eventually.equal(value);
});

请注意,expect()已修补"在量角器/ jasminewd隐含地解决承诺。换句话说,您可以断言getText()等于所需的文本:

expect(element(by.xpath('(//*[@class="k-link"])[2]')).getText()).toEqual('My Text');