8

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?

kyxap
  • 516
  • 6
  • 20
  • you can also use [async / await](https://javascript.info/async-await) to manage promises – Wari Jun 06 '19 at 23:44

1 Answers1

13

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');
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Yeah I know that we can use expected text- but in feature it would not work for me, because we will have different language translation. thank you for the example. basically I need to store this 'value' as var and use it for my expect(). but I can understand how to do it - my poor knowledge of js. can you please write simple example for it? – kyxap Mar 02 '15 at 16:04
  • @kyxap I've updated the answer including the expect call inside the `then()` function. Is this what you are trying to say? Thanks. – alecxe Mar 02 '15 at 18:27
  • sorry, but don't have required 15 of reputation. btw how we can store the value or create the function with return state? – kyxap Mar 02 '15 at 19:12
  • @kyxap nono, you are mixing the "accepting the answer" and "upvoting" functionality. To accept, just mark the checkbox. To answer your question: you'll have to dive into promises, see http://stackoverflow.com/questions/24289431/creating-and-resolving-promises-in-protractor and http://stackoverflow.com/a/21736757/771848. If you'll have difficulties, consider asking a separate question so that more people could help. – alecxe Mar 02 '15 at 19:21