David
David

Reputation: 41

Using 'Expect' to check partial text content

I want to use 'Expect' to verify presence of a certain text string in an element.

The element I'm looking at will contain the following text:

"Your booking reference is: DBM038763."

I want to confirm that "DBM" appears in the text

var conf1 = element(by.xpath(".//*[@id='root']/div[2]/div/div[2]/div/section/div[1]/div[2]/div/h4")).getText();
    expect(conf1.textToBePresentInElement("DBM"));

This will return:

    - Failed: conf1.textToBePresentInElement is not a function

I'm sure I'm missing something very obvious! Thanks

Upvotes: 0

Views: 777

Answers (2)

DublinDev
DublinDev

Reputation: 2348

textToBePresentInElement is an expectedCondition but you are attempting to call it as a method on conf1 which is a promise eventually returning a string via getText(). You need to change you syntax slightly to use expected conditions as below:

var EC = protractor.ExpectedConditions;
var conf1 = element(by.xpath(".//*[@id='root']/div[2]/div/div[2]/div/section/div[1]/div[2]/div/h4")).getText();
expect(EC.textToBePresentInElement(conf1, "DBM")).toBe(true); //added .toBe(true)

Default example provided in link above

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to contain the text 'foo'.
browser.wait(EC.textToBePresentInElement($('#abc'), 'foo'), 5000);

Upvotes: 0

supputuri
supputuri

Reputation: 14135

If value is present in conf1 then you can use the below.

 expect(conf1).toContain("DBM")

Upvotes: 1

Related Questions