Pergunta

In my project, to test if a column is well sorted, I used Selenium Webdriver in Javascript. I wanted to get the first column in a table and see if two by two my strings were sorted in order. Here is my code :

driver.findElements(webdriver.By.xpath("//table[@id='consentsTable']/tbody//td[1]")).then(function(id){
        assert(id[0].getText()).equalTo("26");
        for (var i = 0, l=id.length; i < l-1; i++) {
            console.log(id[i].getText());
            assert(id[i].getText()<id[i+1].getText()).isTrue();
        }
    });

The problem that I have is that my comparison between doesn't work. To make sure about what I get from findElements, I put a console.log. The result was the following one :

{ then: [Function: then], cancel: [Function: cancel], isPending: [Function: isPending] }

As far as I know it seems to be a promise which has not be resolved. But I was a little surprised knowing that normally a function after a "then" should be executed only if the promise has been resolved.

To be sure I put an assert to the first element which in my case should be equal to 26.. and the assert passed ! I changed the value 26 by something else to verify is my assert was considered or not and in fact it failed for others values. I've also done that for others elements in my array.

So at this point, if the assert passed, it means that the promise was actually resolved, which is why I don't understand what the log tells me in my loop.

I've been stuck with that problem for quite a long time without knowing why a promise is displayed, which I also think explains why my comparison fails.

If someone have an idea it would be a huge help for me.

Foi útil?

Solução

Change your code at line getText() to

element.getText().then(function(textValue) {
    assert(textValue).equalTo("foo");
});

OR, you can change assert to expect

expect(element.getText()).toEqual("foo");// write using Protractor/Jasmine
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top