Best Playwright-dotnet code snippet using Microsoft.Playwright.NUnit.BrowserTest
Tests.cs
Source: Tests.cs
1using System;2using System.Threading;3using System.Threading.Tasks;4using Microsoft.Playwright;5using Microsoft.Playwright.NUnit;6using NUnit.Framework;7using PlaywrightTests.Browsers;8using PlaywrightTests.Logger;9using PlaywrightTests.Model;10using PlaywrightTests.Pages;11using PlaywrightTests.Service;12namespace PlaywrightTests13{14 //need to use browserTest or contextTest15 public class Tests : Logging16 {17 private IPage _page;18 private User _user;19 [OneTimeSetUp]20 public async Task Setup()21 {22 _user = UserCreator.userWithDefaultCookie();23 //TestContext.Parameters[];24 _page = await PageSingleton.GetPageAsync("chrome",25 "false",26 _user.CookiePath);27 }28 [Test]29 [TestCase(1)]30 public async Task BuyMarketStocks_ReturnsExpectedAmount(int amount)31 {32 MainPage mainPage = new MainPage(_page);33 Stock stock = new Stock();34 stock.NumberBeforeOperation = await mainPage.GetStocksAmount();35 await mainPage.BuyMarketStockAsync(amount);36 await mainPage.WaitTimeOutAsync(5000.0f);37 stock.NumberAfterOperation = await mainPage.GetStocksAmount();38 Assert.AreEqual(stock.NumberBeforeOperation, stock.NumberAfterOperation - amount);39 }40 [Test]41 [TestCase(1)]42 public async Task SellMarketStocks_ReturnsExpectedAmount(int amount)43 {44 MainPage mainPage = new MainPage(_page);45 Stock stock = new Stock();46 stock.NumberBeforeOperation = await mainPage.GetStocksAmount();47 await mainPage.SellMarketStockAsync(amount);48 await mainPage.WaitTimeOutAsync(5000.0f);49 stock.NumberAfterOperation = await mainPage.GetStocksAmount();50 Assert.AreEqual(stock.NumberBeforeOperation, stock.NumberAfterOperation + amount);51 }52 [Test]53 [TestCase(1)]54 public async Task BuyLimitStocks_ReturnsExpectedAmount(int amount)55 {56 MainPage mainPage = new MainPage(_page);57 Stock stock = new Stock();58 stock.NumberBeforeOperation = await mainPage.GetStocksAmount();59 await mainPage.BuyLimitStockAsync(amount);60 await mainPage.WaitTimeOutAsync(5000.0f);61 stock.NumberAfterOperation = await mainPage.GetStocksAmount();62 Assert.AreEqual(stock.NumberBeforeOperation, stock.NumberAfterOperation - amount);63 }64 [Test]65 [TestCase(1)]66 public async Task SellLimitStocks_ReturnsExpectedAmount(int amount)67 {68 MainPage mainPage = new MainPage(_page);69 Stock stock = new Stock();70 stock.NumberBeforeOperation = await mainPage.GetStocksAmount();71 await mainPage.SellLimitStockAsync(amount);72 await mainPage.WaitTimeOutAsync(5000.0f);73 stock.NumberAfterOperation = await mainPage.GetStocksAmount();74 Assert.AreEqual(stock.NumberBeforeOperation, stock.NumberAfterOperation + amount);75 }76 [Test]77 [TestCase(1)]78 public async Task BuyStopStocks_ReturnsExpectedAmount(int amount)79 {80 MainPage mainPage = new MainPage(_page);81 Stock stock = new Stock();82 stock.NumberBeforeOperation = await mainPage.GetStocksAmount();83 await mainPage.BuyStopStockAsync(amount);84 await mainPage.WaitTimeOutAsync(5000.0f);85 stock.NumberAfterOperation = await mainPage.GetStocksAmount();86 Assert.AreEqual(stock.NumberBeforeOperation, stock.NumberAfterOperation - amount);87 }88 [Test]89 [TestCase(1)]90 public async Task SellStopStocks_ReturnsExpectedAmount(int amount)91 {92 MainPage mainPage = new MainPage(_page);93 Stock stock = new Stock();94 stock.NumberBeforeOperation = await mainPage.GetStocksAmount();95 await mainPage.SellStopStockAsync(amount);96 await mainPage.WaitTimeOutAsync(5000.0f);97 stock.NumberAfterOperation = await mainPage.GetStocksAmount();98 Assert.AreEqual(stock.NumberBeforeOperation, stock.NumberAfterOperation + amount);99 }100 [Test]101 [TestCase(10)]102 public async Task MoneyTransfer_ReturnsExpectedValue(double amount)103 {104 FundingPage fundingPage = await new MainPage(_page)105 .GoToFundingPageAsync().Result106 .MakeTransferAsync(10);107 Transfer transfer = new Transfer(amount, await fundingPage.GetTransferResultAsync(),108 await fundingPage.GetConversionRateAsync());109 Assert.AreEqual(transfer.ExpectedTransferResult, transfer.CurrentTransferResult);110 }111 [Test]112 [TestCase(10)]113 public async Task MoneyWithdrawal_ReturnsExpectedValue(double amount)114 {115 FundingPage fundingPage = await new MainPage(_page)116 .GoToFundingPageAsync().Result117 .MakeWithdrawalAsync(10);118 Withdraw withdraw = new Withdraw(amount, await fundingPage.GetResultAsync());119 Assert.AreEqual(withdraw.BalanceBeforeWithdrawal, withdraw.BalanceAfterWithdrawal + withdraw.Amount);120 }121 [Test]122 [TestCase(10)]123 public async Task MoneyDeposit_ReturnsExpectedValue(double amount)124 {125 FundingPage fundingPage = await new MainPage(_page)126 .GoToFundingPageAsync().Result127 .MakeDepositAsync(10);128 Deposit deposit = new Deposit(amount, await fundingPage.GetResultAsync());129 Assert.AreEqual(deposit.BalanceBeforeDeposit, deposit.BalanceAfterDeposit - deposit.Amount);130 }131 [OneTimeTearDown]132 public async Task Teardown()133 {134 _page = await PageSingleton.DisposeAsync();135 }136 }137}...
BrowserTest.cs
Source: BrowserTest.cs
...25using System.Threading.Tasks;26using NUnit.Framework;27namespace Microsoft.Playwright.NUnit28{29 public class BrowserTest : PlaywrightTest30 {31 public IBrowser Browser { get; internal set; }32 private readonly List<IBrowserContext> _contexts = new();33 public async Task<IBrowserContext> NewContext(BrowserNewContextOptions options)34 {35 var context = await Browser.NewContextAsync(options).ConfigureAwait(false);36 _contexts.Add(context);37 return context;38 }39 [SetUp]40 public async Task BrowserSetup()41 {42 var service = await BrowserService.Register(this, BrowserType).ConfigureAwait(false);43 Browser = service.Browser;...
BrowserTestEx.cs
Source: BrowserTestEx.cs
...26using Microsoft.Playwright.Tests.TestServer;27using NUnit.Framework;28namespace Microsoft.Playwright.Tests29{30 public class BrowserTestEx : BrowserTest31 {32 public SimpleServer Server { get; internal set; }33 public SimpleServer HttpsServer { get; internal set; }34 [SetUp]35 public async Task HttpSetup()36 {37 var http = await HttpService.Register(this);38 Server = http.Server;39 HttpsServer = http.HttpsServer;40 }41 }42}...
ContextTest.cs
Source: ContextTest.cs
...24using System.Threading.Tasks;25using NUnit.Framework;26namespace Microsoft.Playwright.NUnit27{28 public class ContextTest : BrowserTest29 {30 public IBrowserContext Context { get; private set; }31 public virtual BrowserNewContextOptions ContextOptions()32 {33 return null;34 }35 [SetUp]36 public async Task ContextSetup()37 {38 Context = await NewContext(ContextOptions()).ConfigureAwait(false);39 }40 }41}...
BrowserTest
Using AI Code Generation
1using Microsoft.Playwright.NUnit;2using NUnit.Framework;3{4 {5 public async Task Test1()6 {7 await Page.ScreenshotAsync("screenshot.png");8 }9 }10}11using Microsoft.Playwright;12using NUnit.Framework;13using System.Threading.Tasks;14{15 {16 public async Task Test1()17 {18 await Page.ScreenshotAsync("screenshot.png");19 }20 }21}22using Microsoft.Playwright;23using NUnit.Framework;24using System.Threading.Tasks;25{26 {27 public async Task Test1()28 {29 using var playwright = await Playwright.CreateAsync();30 await using var browser = await playwright.Chromium.LaunchAsync();31 var page = await browser.NewPageAsync();32 await page.ScreenshotAsync("screenshot.png");33 }34 }35}36using Microsoft.Playwright;37using Microsoft.Playwright.NUnit;38using NUnit.Framework;39using System.Threading.Tasks;40{41 {42 public async Task Test1()43 {44 await Page.ScreenshotAsync("screenshot.png");45 }46 }47}48using Microsoft.Playwright;49using Microsoft.Playwright.Testing;50using NUnit.Framework;51using System.Threading.Tasks;52{53 {54 public async Task Test1()55 {56 await Page.ScreenshotAsync("screenshot.png");57 }58 }59}
BrowserTest
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.NUnit;3using NUnit.Framework;4{5 [Parallelizable(ParallelScope.Self)]6 {7 public async Task Test1()8 {9 Assert.AreEqual(await Page.TitleAsync(), "Google");10 }11 }12}13dotnet test --logger "trx;LogFileName=TestResults.trx"
BrowserTest
Using AI Code Generation
1using NUnit.Framework;2using Microsoft.Playwright;3{4 {5 private IBrowser browser;6 private IPage page;7 public async Task SetUp()8 {9 browser = await Playwright.CreateAsync().Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 page = await browser.NewPageAsync();13 }14 public async Task TearDown()15 {16 await browser.CloseAsync();17 }18 public async Task Test1()19 {20 await page.ScreenshotAsync(new PageScreenshotOptions { Path = "google.png" });21 }22 }23}24Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "2", "2.csproj", "{B1E4F3F4-0B0B-4B6A-8B8F-6F4B6A2A2C9A}"25 GlobalSection(SolutionConfigurationPlatforms) = preSolution
BrowserTest
Using AI Code Generation
1using NUnit.Framework;2using Microsoft.Playwright;3{4 {5 public async Task Test1()6 {7 using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync();9 var page = await browser.NewPageAsync();10 Assert.AreEqual("Playwright", page.Title);11 }12 }13}14using NUnit.Framework;15using Microsoft.Playwright;16{17 {18 public async Task Test1()19 {20 using var playwright = await Playwright.CreateAsync();21 await using var browser = await playwright.Chromium.LaunchAsync();22 var page = await browser.NewPageAsync();23 Assert.AreEqual("Playwright", page.Title);24 }25 }26}27using NUnit.Framework;28using Microsoft.Playwright;29{30 {31 public async Task Test1()32 {33 using var playwright = await Playwright.CreateAsync();34 await using var browser = await playwright.Chromium.LaunchAsync();35 var page = await browser.NewPageAsync();36 Assert.AreEqual("Playwright", page.Title);37 }38 }39}40using NUnit.Framework;41using Microsoft.Playwright;42{43 {44 public async Task Test1()45 {46 using var playwright = await Playwright.CreateAsync();47 await using var browser = await playwright.Chromium.LaunchAsync();48 var page = await browser.NewPageAsync();49 Assert.AreEqual("Playwright", page.Title);50 }51 }52}53using NUnit.Framework;54using Microsoft.Playwright;
BrowserTest
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.NUnit;3using NUnit.Framework;4{5 {6 public async Task Test()7 {8 var page = await Context.NewPageAsync();9 }10 }11}12using Microsoft.Playwright;13using Microsoft.Playwright.Xunit;14using Xunit;15{16 {17 public async Task Test()18 {19 var page = await Context.NewPageAsync();20 }21 }22}23using Microsoft.Playwright;24using Microsoft.Playwright.MSTest;25using Microsoft.VisualStudio.TestTools.UnitTesting;26{27 {28 public async Task Test()29 {30 var page = await Context.NewPageAsync();31 }32 }33}34using Microsoft.Playwright;35using Microsoft.Playwright.NUnit;36using NUnit.Framework;37{38 {39 public async Task Test()40 {41 var page = await Context.NewPageAsync();42 }43 }44}45using Microsoft.Playwright;46using Microsoft.Playwright.Xunit;47using Xunit;48{49 {50 public async Task Test()51 {52 var page = await Context.NewPageAsync();53 }54 }55}56using Microsoft.Playwright;57using Microsoft.Playwright.MSTest;
BrowserTest
Using AI Code Generation
1using NUnit.Framework;2using Microsoft.Playwright;3using System.Threading.Tasks;4using System;5{6 {7 private IBrowser browser;8 private IBrowserContext context;9 private IPage page;10 public async Task Setup()11 {12 browser = await Playwright.CreateAsync().Chromium.LaunchAsync(headless: false);13 context = await browser.NewContextAsync();14 page = await context.NewPageAsync();15 }16 public async Task Test1()17 {18 await page.SetViewportSizeAsync(1920, 1080);19 await page.ScreenshotAsync("screenshot.png");20 await browser.CloseAsync();21 }22 public void TearDown()23 {24 browser.CloseAsync();25 }26 }27}
BrowserTest
Using AI Code Generation
1using Microsoft.Playwright.NUnit;2using NUnit.Framework;3using System.Threading.Tasks;4{5 {6 public async Task Test1()7 {8 var page = await Browser.NewPageAsync();9 await page.ScreenshotAsync("google.png");10 }11 }12}
BrowserTest
Using AI Code Generation
1using NUnit.Framework;2using System.Threading.Tasks;3using Microsoft.Playwright;4using Microsoft.Playwright.NUnit;5{6 {7 public async Task Test1()8 {9 await Page.ScreenshotAsync("C:\\Users\\Admin\\Desktop\\Screenshots\\screenshot.png");10 }11 }12}13using NUnit.Framework;14using System.Threading.Tasks;15using Microsoft.Playwright;16using Microsoft.Playwright.NUnit;17{18 {19 public async Task Test1()20 {21 await Page.ScreenshotAsync("C:\\Users\\Admin\\Desktop\\Screenshots\\screenshot.png");22 }23 }24}25using NUnit.Framework;26using System.Threading.Tasks;27using Microsoft.Playwright;28using Microsoft.Playwright.NUnit;29{30 {31 public async Task Test1()32 {33 await Page.ScreenshotAsync("C:\\Users\\Admin\\Desktop\\Screenshots\\screenshot.png");34 }35 }36}37using NUnit.Framework;38using System.Threading.Tasks;39using Microsoft.Playwright;40using Microsoft.Playwright.NUnit;41{42 {43 public async Task Test1()44 {45 await Page.ScreenshotAsync("C:\\Users\\Admin\\Desktop\\Screenshots\\screenshot.png");46 }47 }48}49using NUnit.Framework;50using System.Threading.Tasks;51using Microsoft.Playwright;52using Microsoft.Playwright.NUnit;53{
BrowserTest
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.NUnit;3using NUnit.Framework;4{5 {6 [PlaywrightTest("playwright-dotnet-hello-world/tests/2.cs")]7 public async Task ShouldWork()8 {9 using var playwright = await Playwright.CreateAsync();10 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 await page.ClickAsync("text=Sign in");15 await page.ClickAsync("input[aria-label=\"Email or phone\"]");16 await page.FillAsync("input[aria-label=\"Email or phone\"]", "test");17 await page.ClickAsync("input[aria-label=\"Password\"]");18 await page.FillAsync("input[aria-label=\"Password\"]", "test");19 await page.ClickAsync("text=Sign in");20 await page.ScreenshotAsync(new PageScreenshotOptions21 {22 });23 }24 }25}26dotnet test --test-adapter-path:. --logger:"console;verbosity=normal" --filter "FullyQualifiedName=Playwright.BrowserTest.ShouldWork"
BrowserTest
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Text;4using System.Threading.Tasks;5using Microsoft.Playwright;6using Microsoft.Playwright.NUnit;7using NUnit.Framework;8{9 {10 [PlaywrightTest("firefox")]11 public async Task Test1(IPage page)12 {13 await page.FillAsync("input[aria-label='Search']", "Hello, World!");14 await page.PressAsync("input[aria-label='Search']", "Enter");15 await page.ScreenshotAsync(path: "screenshot.png");16 }17 }18}19using System;20using System.Collections.Generic;21using System.Text;22using System.Threading.Tasks;23using Microsoft.Playwright;24using Microsoft.Playwright.NUnit;25using NUnit.Framework;26{27 {28 [PlaywrightTest("webkit")]29 public async Task Test1(IPage page)30 {31 await page.FillAsync("input[aria-label='Search']", "Hello, World!");32 await page.PressAsync("input[aria-label='Search']", "Enter");33 await page.ScreenshotAsync(path: "screenshot.png");34 }35 }36}37using System;38using System.Collections.Generic;39using System.Text;40using System.Threading.Tasks;41using Microsoft.Playwright;42using Microsoft.Playwright.NUnit;43using NUnit.Framework;44{45 {46 [PlaywrightTest("chromium")]47 public async Task Test1(IPage page)48 {49 await page.FillAsync("input[aria-label='Search']", "Hello, World!");50 await page.PressAsync("input[aria-label='Search']", "Enter");51 await page.ScreenshotAsync(path: "screenshot.png");52 }53 }54}55using System;
Playwright Multiple Elements - Is there an equivalent to Selenium FindElements?
How to handle multiple file downloads in Playwright?
Run Playwright.NET tests in Docker container
How to handle multiple file downloads in Playwright?
Running playwright in headed mode C#
Playwright (.NET) tries to use different browser versions than installed
Playwright "Element is not attached to the DOM"
Playwright Multiple Elements - Is there an equivalent to Selenium FindElements?
Microsoft.Playwright.PlaywrightException : unable to verify the first certificate Using Playwright C# While connecting Moon
How do you create a global configuration for Playwright .NET?
Using a selector that finds a list of locators in Playwright is exactly the same as calling .FindElements() in selenium, except that Playwright does not have a specifically named method like .FindLocators().
Playwright - a selector that matches multiple elements returns a list of locators, which you then iterate over:
var rows = page.GetByRole(AriaRole.Listitem);
var count = await rows.CountAsync();
for (int i = 0; i < count; ++i)
Console.WriteLine(await rows.Nth(i).TextContentAsync());
Selenium - FindElements returns a list of elements that you have to iterate over.
IList < IWebElement > elements = driver.FindElements(By.TagName("p"));
foreach(IWebElement e in elements) {
System.Console.WriteLine(e.Text);
}
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!