skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    This works:

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

  2. You could do something like this:

    var myTextLocator = await Page.GetByText("my text");
    await Expect(myTextLocator).ToHaveCountAsync(1);
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search