bobe_b
bobe_b

Reputation: 435

How to use 'expect' in playwright with C# - Xunit

Im porting some playwright tests over from visual studio code in js, to visual studio C# using xuint.

I cant seem to get the expect command to work in visual studio.

in the javascript project i would do the following to check an element contains some text:

await expect(page.locator('elementtofind')).toContainText('myText');

In C# if i write the same thing i get 'The name 'identifier' does not exist in the current context' error on the expect command

In the JS project my import statement is

import { test, expect } from '@playwright/test';

In the C# project im using:

using Microsoft.Playwright;

which does not appear to contain the expect

As a temporary workaround im doing the following in C# to achieve the same result

var item1 = page.Locator("elementtofind");
        Assert.Equal("myText", await item1.InnerTextAsync());

but would like to make use of the expect functionality in my tests.

Does anyone know how I can make this work in my project?

Upvotes: 10

Views: 6575

Answers (1)

dariom
dariom

Reputation: 4578

If you're using xUnit, unfortunately, you can't use Expect directly. For NUnit and MSTest there's a base class PageTest that you can derive your tests from that provide this method. A future version of xUnit might make it possible to create a similar base class, but for now I haven't seen a good way to do the equivalent.

As a work around in xUnit, I use the Playwright Assertions class directly in my tests, for example:

await Assertions.Expect(Page.Locator("h1")).ToHaveTextAsync("Foo");

Upvotes: 22

Related Questions