POC to E2E test with Playwright
Install Playwright for C#
- Install the Playwright tool with the next command
dotnet tool install --global Microsoft.Playwright.CLI
- Install Playwright (this includes the browsers Chromium, Firefox, Webkit). Execute this command inside the project path. Sample path c:\Projects\sauceDemo
playwright install
You can run the test from Windows, MacOS or with Azure Devops Pipeline
You can run with different browsers
dotnet test -s chromium.runsettings
dotnet test -s webkit.runsettings
dotnet test -s firefox.runsettings
For run only some test, for example run the test that contains AddItems in the name
dotnet test --filter "Name~AddItems"
For get the list of filter: https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests?pivots=nunit
- Select the .runsettings
- On Test Explorer select Run
- Open Test Panel and Right Click on SauceDemo
- Select the option Run Test Wih -> Custom Parameters
- Add the variables BASE_ADDRESS : https://www.saucedemo.com/ BROWSER_TYPE : Chromium or Firefox or Webkit
Request access to azure devops by mail with a microsoft email
- Go to pipelines
- Right click in the ... button and click in Run Pipeline
- Change the variables if is needed
- Click in Run
You can run the pipeline and manually change the variable BROWSER_TYPE to Chromium or Firefox or Webkit to test with different browsers and BASE_ADDRESS to test with different URLs
You can check the reports in Azure Devops
For run test in parallel you need to add the attribute [Parallelizable] to the Test class
You can setup the number or workers
dotnet test -- NUnit.NumberOfTestWorkers=2
For additional reports to Azure Devops
Integration with third party software like saucelabs, browserstack, applitools is only supported to NodeJS (JS/Typescript)
With NUnit you can integrate with Report Portal
To generate a new test
playwright codegen https://www.saucedemo.com/
To emulate an IPhone
playwright codegen --device="iPhone 11" wikipedia.org
The test case: Login_WithValidUser_NavigatesToProductsPageAsync includes a tracing sample
This option generates a .zip with more details about the test
More info
https://playwright.dev/dotnet/docs/trace-viewer https://applitools.com/event/playwright-four-futuristic-features/
To check the trace
playwright show-trace trace.zip
As a best practice you can add a custom snippet to Visual Studio (windows) to generate the code for the test cases with the next format:
UoW_InitialCondition_ExpectedResult
UoW is Unit of work
You can replace the [TestMethod] to [Test] in the xml file
You can clone the files from this repo https://gist.github.com/osmyn/906c917653a30864cb52dee02c36c14e
Copy the filess to %USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets
After you can write "uat" from VS and press tab to get a template for async test
[TestMethod]
public async Task UoW_InitialCondition_ExpectedResult()
{
//Arrange
//Act
//Assert
}
Or write "ut" for sync test case
[TestMethod]
public void UoW_InitialCondition_ExpectedResult()
{
//Arrange
$end$
//Act
//Assert
}]