Pindakaas
Pindakaas

Reputation: 4439

protractor how to get text from div?

Trying to get the text inside my div:

<div class="ng-scope ng-binding">
         Hello this is it!
</div>

In my jasmine script I have (using xpath):

var helloTxt=driver.findElement(by.xpath('//*[@id="top"]/div/div/div[3]/div[2]/div/div[1]/div[1]/div/div'));
expect(helloTxt.getText()).toBe('Hello this is it!');

the code looks like this:

<div class="container">
        <div data-ng-repeat="message in getMessages() track by $index">
            {{message}}
        </div>
</div>

How can I check get the messages in {{message}}? Is there another way to access this div without using xpath? Or how can I fix this?

Upvotes: 2

Views: 12392

Answers (1)

Leo Gallucci
Leo Gallucci

Reputation: 16732

Target all messages available inside the container

var messageElms = $('.container').all(by.binding('message'));

Assuming you expect only 1 message:

expect(messageElms.count()).toBe(1);

Assuming you expect that Hello to be the first message:

expect(messageElms.get(0).getText()).toEqual('Hello this is it!');

Upvotes: 14

Related Questions