Run Playwright-dotnet automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
/*
* 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.AspNetCore.Http;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace Microsoft.Playwright.Tests
{
///<playwright-file>capabilities.spec.ts</playwright-file>
public class CapabilitiesTests : PageTestEx
{
[PlaywrightTest("capabilities.spec.ts", "Web Assembly should work")]
[Skip(SkipAttribute.Targets.Webkit | SkipAttribute.Targets.Windows)]
public async Task WebAssemblyShouldWork()
{
await Page.GotoAsync(Server.Prefix + "/wasm/table2.html");
Assert.AreEqual("42, 83", await Page.EvaluateAsync<string>("() => loadTable()"));
}
#if NETCOREAPP
[PlaywrightTest("capabilities.spec.ts", "WebSocket should work")]
[Skip(SkipAttribute.Targets.Webkit | SkipAttribute.Targets.Windows)]
public async Task WebSocketShouldWork()
{
Server.SendOnWebSocketConnection("incoming");
string value = await Page.EvaluateAsync<string>(
[email protected]"(port) => {{
let cb;
const result = new Promise(f => cb = f);
const ws = new WebSocket('ws://localhost:' + port + '/ws');
ws.addEventListener('message', data => {{ ws.close(); cb(data.data); console.log(data); console.log(data.data) }});
ws.addEventListener('error', error => cb('Error'));
return result;
}}",
Server.Port);
Assert.AreEqual("incoming", value);
}
#endif
[PlaywrightTest("capabilities.spec.ts", "should respect CSP")]
public async Task ShouldRespectCSP()
{
Server.SetRoute("/empty.html", context =>
{
const string message = @"
<script>
window.testStatus = 'SUCCESS';
window.testStatus = eval(""'FAILED'"");
</script>
";
context.Response.Headers["Content-Length"] = message.Length.ToString();
context.Response.Headers["Content-Security-Policy"] = "script-src 'unsafe-inline';";
return context.Response.WriteAsync(message);
});
await Page.GotoAsync(Server.EmptyPage);
Assert.AreEqual("SUCCESS", await Page.EvaluateAsync<string>("() => window.testStatus"));
}
[PlaywrightTest("capabilities.spec.ts", "should play video")]
[Skip(SkipAttribute.Targets.Webkit | SkipAttribute.Targets.Linux, SkipAttribute.Targets.Webkit | SkipAttribute.Targets.Windows, SkipAttribute.Targets.Firefox)]
public async Task ShouldPlayVideo()
{
await Page.GotoAsync(Server.Prefix + (TestConstants.IsWebKit ? "/video_mp4.html" : "/video.html"));
await Page.EvalOnSelectorAsync("video", "v => v.play()");
await Page.EvalOnSelectorAsync("video", "v => v.pause()");
}
}
}
/*
* 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.NUnit;
using NUnit.Framework;
namespace Microsoft.Playwright.Tests
{
public class PageEventCrashTests : PageTestEx
{
// We skip all browser because crash uses internals.
[PlaywrightTest("page-event-crash.spec.ts", "should emit crash event when page crashes")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
public async Task ShouldEmitCrashEventWhenPageCrashes()
{
await Page.SetContentAsync("<div>This page should crash</div>");
var crashEvent = new TaskCompletionSource<bool>();
Page.Crash += (_, _) => crashEvent.TrySetResult(true);
await CrashAsync(Page);
await crashEvent.Task;
}
// We skip all browser because crash uses internals.
[PlaywrightTest("page-event-crash.spec.ts", "should throw on any action after page crashes")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
public async Task ShouldThrowOnAnyActionAfterPageCrashes()
{
await Page.SetContentAsync("<div>This page should crash</div>");
var crashEvent = new TaskCompletionSource<bool>();
Page.Crash += (_, _) => crashEvent.TrySetResult(true);
await CrashAsync(Page);
await crashEvent.Task;
var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.EvaluateAsync("() => {}"));
StringAssert.Contains("Target crashed", exception.Message);
}
// We skip all browser because crash uses internals.
[PlaywrightTest("page-event-crash.spec.ts", "should cancel waitForEvent when page crashes")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
public async Task ShouldCancelWaitForEventWhenPageCrashes()
{
await Page.SetContentAsync("<div>This page should crash</div>");
var responseTask = Page.WaitForResponseAsync("**/*");
await CrashAsync(Page);
var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => responseTask);
StringAssert.Contains("Page crashed", exception.Message);
}
// We skip all browser because crash uses internals.
[PlaywrightTest("page-event-crash.spec.ts", "should cancel navigation when page crashes")]
[Ignore("Not relevant downstream")]
public void ShouldCancelNavigationWhenPageCrashes()
{
}
// We skip all browser because crash uses internals.
[PlaywrightTest("page-event-crash.spec.ts", "should be able to close context when page crashes")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
public async Task ShouldBeAbleToCloseContextWhenPageCrashes()
{
await Page.SetContentAsync("<div>This page should crash</div>");
var crashEvent = new TaskCompletionSource<bool>();
Page.Crash += (_, _) => crashEvent.TrySetResult(true);
await CrashAsync(Page);
await crashEvent.Task;
await Page.Context.CloseAsync();
}
private async Task CrashAsync(IPage page)
{
try
{
await page.GotoAsync("chrome://crash");
}
catch
{
}
}
}
}
/*
* 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.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Playwright.Helpers;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace Microsoft.Playwright.Tests
{
///<playwright-file>chromium/chromium.spec.ts</playwright-file>
public class BrowserTypeConnectOverCDPTests : PlaywrightTestEx
{
[PlaywrightTest("chromium/chromium.spec.ts", "should connect to an existing cdp session")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
public async Task ShouldConnectToAnExistingCDPSession()
{
int port = 9393 + WorkerIndex;
IBrowser browserServer = await BrowserType.LaunchAsync(new() { Args = new[] { $"--remote-debugging-port={port}" } });
try
{
IBrowser cdpBrowser = await BrowserType.ConnectOverCDPAsync($"http://localhost:{port}/");
var contexts = cdpBrowser.Contexts;
Assert.AreEqual(1, cdpBrowser.Contexts.Count);
var page = await cdpBrowser.Contexts[0].NewPageAsync();
Assert.AreEqual(2, await page.EvaluateAsync<int>("1 + 1"));
await cdpBrowser.CloseAsync();
}
finally
{
await browserServer.CloseAsync();
}
}
[PlaywrightTest("chromium/chromium.spec.ts", "should send extra headers with connect request")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
public async Task ShouldSendExtraHeadersWithConnectRequest()
{
var waitForRequest = Server.WaitForWebSocketConnectionRequest();
BrowserType.ConnectOverCDPAsync($"ws://localhost:{Server.Port}/ws", new()
{
Headers = new Dictionary<string, string> {
{ "x-foo-bar", "fookek" }
},
}).IgnoreException();
var req = await waitForRequest;
Assert.AreEqual("fookek", req.Headers["x-foo-bar"]);
StringAssert.Contains("Playwright", req.Headers["user-agent"]);
}
}
}
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.