Skip to content

Instantly share code, notes, and snippets.

@up1
Last active June 9, 2024 05:10
Show Gist options
  • Save up1/62c17d1ea64b0a4af32722f0172a37d4 to your computer and use it in GitHub Desktop.
Save up1/62c17d1ea64b0a4af32722f0172a37d4 to your computer and use it in GitHub Desktop.
MSTest 3.4 + Playwright
$dotnet --version
9.0.100-preview.4.24267.66
$dotnet new list
These templates matched your input:
Template Name Short Name Language Tags
-------------------------------------------- -------------------------- ---------- --------------------------------
API Controller apicontroller [C#] Web/ASP.NET
ASP.NET Core Web API (native AOT) webapiaot [C#] Web/Web API/API/Service
...
MSTest Playwright Test Project mstest-playwright [C#] Test/MSTest/Playwright
MSTest Test Class mstest-class [C#],F#,VB Test/MSTest
MSTest Test Project mstest [C#],F#,VB Test/MSTest
$dotnet new mstest-playwright
The template "MSTest Playwright Test Project" was created successfully.
Processing post-creation actions...
Build succeeded in 20.9s
Restore succeeded.
$dotnet test
Restore complete (0.3s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
demo-mstest succeeded (0.2s) → bin/Debug/net9.0/demo-mstest.dll
demo-mstest test succeeded (5.6s)
Test summary: total: 1, failed: 0, succeeded: 1, skipped: 0, duration: 5.2s
Build succeeded in 6.2s
<Project Sdk="MSTest.Sdk/3.4">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>demo_mstest</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnablePlaywright>true</EnablePlaywright>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Text.RegularExpressions" />
<Using Include="System.Threading.Tasks" />
<Using Include="Microsoft.Playwright.MSTest" />
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
</Project>
namespace demo_mstest;
[TestClass]
public class UnitTest1 : PageTest
{
[TestMethod]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{
await Page.GotoAsync("https://playwright.dev");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
// create a locator
var getStarted = Page.Locator("text=Get Started");
// Expect an attribute "to be strictly equal" to the value.
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro");
// Click the get started link.
await getStarted.ClickAsync();
// Expects the URL to contain intro.
await Expect(Page).ToHaveURLAsync(new Regex(".*intro"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment