Run Playwright-dotnet automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
class PlaywrightPixelTest
{
public static async Task main(string[] args)
{
using var playwright = await Playwright.CreateAsync();
Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
browserstackOptions.Add("name", "Test on Playwright emulated Pixel 5");
browserstackOptions.Add("build", "playwright-dotnet-4");
browserstackOptions.Add("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
string capsJson = JsonConvert.SerializeObject(browserstackOptions);
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);
await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var context = await browser.NewContextAsync(playwright.Devices["Pixel 5"]); // Complete list of devices - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
var page = await context.NewPageAsync();
try {
await page.GotoAsync("https://www.google.co.in/");
await page.Locator("[aria-label='Search']").ClickAsync();
await page.FillAsync("[aria-label='Search']", "BrowserStack");
await page.Keyboard.PressAsync("Enter");
await page.Locator("[aria-current='page']").WaitForAsync();
var title = await page.TitleAsync();
if (title == "BrowserStack - Google Search")
{
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
await MarkTestStatus("passed", "Title matched", page);
} else {
await MarkTestStatus("failed", "Title did not match", page);
}
}
catch (Exception err) {
await MarkTestStatus("failed", err.Message, page);
}
await browser.CloseAsync();
}
public static async Task MarkTestStatus(string status, string reason, IPage page) {
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
}
}
using Microsoft.Playwright;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
class PlaywrightIPhoneTest
{
public static async Task main(string[] args)
{
using var playwright = await Playwright.CreateAsync();
Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
browserstackOptions.Add("name", "Test on Playwright emulated IPhone 11 Pro");
browserstackOptions.Add("build", "playwright-dotnet-4");
browserstackOptions.Add("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit`
browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");
string capsJson = JsonConvert.SerializeObject(browserstackOptions);
string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);
await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl);
var context = await browser.NewContextAsync(playwright.Devices["iPhone 11 Pro"]); // Complete list of devices - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json
var page = await context.NewPageAsync();
try {
await page.GotoAsync("https://www.google.co.in/");
await page.Locator("[aria-label='Search']").ClickAsync();
await page.FillAsync("[aria-label='Search']", "BrowserStack");
await page.Keyboard.PressAsync("Enter");
await page.Locator("[aria-current='page']").WaitForAsync();
var title = await page.TitleAsync();
if (title == "BrowserStack - Google Search")
{
// following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test
await MarkTestStatus("passed", "Title matched", page);
} else {
await MarkTestStatus("failed", "Title did not match", page);
}
}
catch (Exception err) {
await MarkTestStatus("failed", err.Message, page);
}
await browser.CloseAsync();
}
public static async Task MarkTestStatus(string status, string reason, IPage page) {
await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}");
}
}
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System.Threading.Tasks;
using Microsoft.Playwright.Transport.Channels;
namespace Microsoft.Playwright.Core
{
internal class Keyboard : IKeyboard
{
private readonly PageChannel _channel;
public Keyboard(PageChannel channel)
{
_channel = channel;
}
public Task DownAsync(string key) => _channel.KeyboardDownAsync(key);
public Task UpAsync(string key) => _channel.KeyboardUpAsync(key);
public Task PressAsync(string key, KeyboardPressOptions options = default)
=> _channel.PressAsync(key, options?.Delay);
public Task TypeAsync(string text, KeyboardTypeOptions options = default)
=> _channel.TypeAsync(text, options?.Delay);
public Task InsertTextAsync(string text) => _channel.InsertTextAsync(text);
}
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.