Walter Levy
Walter Levy

Reputation: 31

How can I check to see if specific text is on a page using Playwright test automation

I am working on my first test automation project using Playwright for .NET. For one of the tests I need to check for a specific bit of text on a page. I have been able to do a number of other tests but I can't seem to figure this one out. The JavaScript implementation includes "has-text" but I don't see that for .NET.

I used page.Locator("my text") but I don't know how to check if "my text" in fact exists on the page.

Upvotes: 1

Views: 2411

Answers (3)

Walter Levy
Walter Levy

Reputation: 31

This works:

var foo = await page.Locator("my text").TextContentAsync();

Upvotes: 0

Basti
Basti

Reputation: 917

You can use ToContainTextAsync:

await Expect(locator).ToContainTextAsync("myText");

So just pick the locator in which you want to check. And if you want to check for visibility you could use the UseInnerText option.

Upvotes: 0

AutomationAndy
AutomationAndy

Reputation: 1022

You could do something like this:

var myTextLocator = await Page.GetByText("my text");
await Expect(myTextLocator).ToHaveCountAsync(1);

Upvotes: 1

Related Questions