Search code examples
javascriptselenium-webdriverpromiseprotractorend-to-end

Protractor returns an object but expected - value of element.getText()


Unable to understand why it return an object not a value of text, some test code:

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); 

Console output:

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

Also if I use this part with expect():

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

I see:

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

So for some reason I can get text from expect and it correct - 'Location'

What im doing wrong?


Solution

  • getText() returns a promise. If you want to log an actual value, you need to resolve it:

    element(by.xpath('(//*[@class="k-link"])[2]')).getText().then(function (value) {
        console.log(value);
    
        expect(columnSorting.acpColumn.getText()).to.eventually.equal(value);
    });
    

    Note that expect() is "patched" in protractor/jasminewd to resolve promises implicitly. In other words, you can assert getText() being equal to the desired text:

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