Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions tests/E2E.NUnit.Tests/Dialog/PageDialogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright 2026 TesterArmy.
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0

using E2E.Engine;
using static E2E.NUnit.Tests.Helpers;

namespace E2E.NUnit.Tests.Dialog;

/// <summary>
/// <c>test.describe('page dialog')</c> of upstream's <c>apps/testbed/tests/dialog.e2e.ts</c> (Playwright's
/// <c>page-dialog.spec.ts</c>). <c>page.on('dialog')</c> is <c>Browser.OnDialogAsync</c>. A dialog no handler answers is dismissed as
/// in Playwright, and e2e also fails the next step for it. Not ported, as upstream: should fire dialogclosed when dialog is accepted,
/// should fire dialogclosed when dialog is dismissed, should fire dialogclosed for auto-dismissed dialogs (no dialogclosed event),
/// should be able to close context with open alert (e2e answers every dialog). The <c>defaultValue()</c> assertions are dropped:
/// e2e dialogs have no default value.
/// </summary>
public sealed class PageDialogTests : PlaywrightTest
{
[Test]
public async Task Should_fire()
{
await Browser.GotoAsync("/empty.html");
DialogType? type = null;
string? message = null;
await Browser.OnDialogAsync(async dialog =>
{
type = dialog.Type;
message = dialog.Message;
await dialog.AcceptAsync();
});
await Browser.EvaluateAsync<object>("() => alert('yo')");
Assert.That(type, Is.EqualTo(DialogType.Alert));
Assert.That(message, Is.EqualTo("yo"));
}

[Test]
public async Task Should_allow_accepting_prompts()
{
await Browser.GotoAsync("/empty.html");
DialogType? type = null;
string? message = null;
await Browser.OnDialogAsync(async dialog =>
{
type = dialog.Type;
message = dialog.Message;
await dialog.AcceptAsync("answer!");
});
var result = await Browser.EvaluateAsync<string>("() => prompt('question?', 'yes.')");
Assert.That(type, Is.EqualTo(DialogType.Prompt));
Assert.That(message, Is.EqualTo("question?"));
Assert.That(result, Is.EqualTo("answer!"));
}

[Test]
public async Task Should_dismiss_the_prompt()
{
await Browser.GotoAsync("/empty.html");
await Browser.OnDialogAsync(dialog => dialog.DismissAsync());
var result = await Browser.EvaluateAsync<string>("() => prompt('question?')");
Assert.That(result, Is.Null);
}

[Test]
public async Task Should_accept_the_confirm_prompt()
{
await Browser.GotoAsync("/empty.html");
await Browser.OnDialogAsync(async dialog =>
{
await dialog.AcceptAsync();
});
var result = await Browser.EvaluateAsync<bool>("() => confirm('boolean?')");
Assert.That(result, Is.True);
}

[Test]
public async Task Should_dismiss_the_confirm_prompt()
{
await Browser.GotoAsync("/empty.html");
await Browser.OnDialogAsync(async dialog =>
{
await dialog.DismissAsync();
});
var result = await Browser.EvaluateAsync<bool>("() => confirm('boolean?')");
Assert.That(result, Is.False);
}

[Test]
public async Task Should_handle_multiple_alerts()
{
var seen = 0;
await Browser.OnDialogAsync(async dialog =>
{
Interlocked.Increment(ref seen);
try
{
await dialog.AcceptAsync();
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Upstream: `.catch(() => {})`.
}
});
await Browser.GotoAsync(Content("""
<p>Hello World</p>
<script>
alert('Please dismiss this dialog');
alert('Please dismiss this dialog');
alert('Please dismiss this dialog');
</script>
"""));
Assert.That(await Browser.Locator("p").TextContentAsync(), Is.EqualTo("Hello World"));
Assert.That(Volatile.Read(ref seen), Is.EqualTo(3));
}

[Test]
public async Task Should_handle_multiple_confirms()
{
var seen = 0;
await Browser.OnDialogAsync(async dialog =>
{
Interlocked.Increment(ref seen);
try
{
await dialog.AcceptAsync();
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Upstream: `.catch(() => {})`.
}
});
await Browser.GotoAsync(Content("""
<p>Hello World</p>
<script>
confirm('Please confirm me?');
confirm('Please confirm me?');
confirm('Please confirm me?');
</script>
"""));
Assert.That(await Browser.Locator("p").TextContentAsync(), Is.EqualTo("Hello World"));
Assert.That(Volatile.Read(ref seen), Is.EqualTo(3));
}

[Test]
public async Task Should_auto_dismiss_the_prompt_without_listeners()
{
await Browser.GotoAsync("/empty.html");
var result = await Browser.EvaluateAsync<string>("() => prompt('question?')");
Assert.That(result, Is.Null);
var error = await Failure(() => Browser.TitleAsync());
Assert.That(error?.Message, Does.Contain("unhandled prompt dialog: question?"));
}

[Test]
public async Task Should_auto_dismiss_the_alert_without_listeners()
{
await Browser.GotoAsync(Content("""<div onclick="window.alert(123); window._clicked=true">Click me</div>"""));
var error = await Failure(async () =>
{
await Browser.Locator("div").TapAsync();
await Browser.TitleAsync();
});
Assert.That(error?.Message, Does.Contain("unhandled alert dialog: 123"));
Assert.That(await Browser.EvaluateAsync<bool>("window._clicked"), Is.True);
}
}
168 changes: 168 additions & 0 deletions tests/E2E.NUnit.Tests/Download/DownloadEventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2026 TesterArmy.
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0

using E2E.Engine;
using static E2E.NUnit.Tests.Helpers;

namespace E2E.NUnit.Tests.Download;

/// <summary>
/// <c>test.describe('download event')</c> of upstream's <c>apps/testbed/tests/download.e2e.ts</c> (Playwright's
/// <c>download.spec.ts</c>). <c>page.waitForEvent('download')</c> is <c>Browser.WaitForDownloadAsync(trigger)</c>, which saves the
/// file, and <c>download.path()</c> is the saved file's <c>AbsolutePath</c>. e2e has no <c>download.url()</c>, so its assertions are
/// dropped. Not ported, as upstream: should report downloads with acceptDownloads: false, should report proper download url when
/// download is from download attribute, should save to user-specified path without updating original path, should save to two
/// different paths with multiple saveAs calls, should save to overwritten filepath, should create subdirectories when saving to
/// non-existent user-specified path, should error when saving with downloads disabled, should error when saving after deletion,
/// should delete file, should expose stream, should delete downloads on context destruction, should delete downloads on browser
/// gone, should save downloads to artifactsDir, should close the context without awaiting the failed download, should close the
/// context without awaiting the download, should throw if browser dies, should be able to cancel pending downloads, should not fail
/// explicitly to cancel a download even if that is already finished, should save to user-specified path (no
/// <c>download.saveAs</c>, <c>delete</c>, <c>createReadStream</c>, <c>cancel</c>, <c>failure</c>, <c>acceptDownloads</c>, context or
/// browser control from the test).
/// </summary>
public sealed class DownloadEventTests : PlaywrightTest
{
[Test]
public async Task Should_report_download_when_navigation_turns_into_download_smoke()
{
// e2e opens no page before the first goto, where Playwright starts on about:blank.
await Browser.GotoAsync("/empty.html");
Exception? responseOrError = null;
var download = await Browser.WaitForDownloadAsync(async () =>
{
responseOrError = await Failure(() => Browser.GotoAsync($"{PREFIX}/download"));
});
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
Assert.That(responseOrError, Is.Not.Null);
Assert.That(responseOrError!.Message, Does.Contain("Download is starting"));
Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/empty.html"));
}

[Test]
public async Task Should_work_with_Cross_Origin_Opener_Policy()
{
// e2e opens no page before the first goto, where Playwright starts on about:blank.
await Browser.GotoAsync("/empty.html");
Exception? responseOrError = null;
var url = Respond(new Dictionary<string, string>
{
["content-type"] = "application/octet-stream",
["content-disposition"] = "attachment",
["cross-origin-opener-policy"] = "same-origin",
});
var download = await Browser.WaitForDownloadAsync(async () =>
{
responseOrError = await Failure(() => Browser.GotoAsync(url));
});
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
Assert.That(responseOrError, Is.Not.Null);
Assert.That(responseOrError!.Message, Does.Contain("Download is starting"));
Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/empty.html"));
}

[Test]
public async Task Should_report_downloads_with_acceptDownloads_true()
{
await Browser.GotoAsync(Content($"""<a href="{PREFIX}/download">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}

[Test]
public async Task Should_report_downloads_for_download_attribute()
{
await Browser.GotoAsync("/empty.html");
await Browser.GotoAsync(Content($"""<a href="{PREFIX}/chromium-linux.zip" download="foo.zip">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(download.SuggestedFilename, Is.EqualTo("foo.zip"));
Assert.That(File.Exists(download.AbsolutePath), Is.True);
}

[Test]
public async Task Should_report_non_navigation_downloads()
{
await Browser.GotoAsync("/empty.html");
await Browser.GotoAsync(Content($"""<a download="file.txt" href="{PREFIX}{Respond(new Dictionary<string, string> { ["content-type"] = "application/octet-stream" })}">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(download.SuggestedFilename, Is.EqualTo("file.txt"));
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}

[Test]
public async Task Should_report_download_path_within_page_on_download_handler_for_Files()
{
await Browser.GotoAsync(Content($"""<a href="{PREFIX}/download">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}

[Test]
public async Task Should_report_download_path_within_page_on_download_handler_for_Blobs()
{
await Browser.GotoAsync("/download-blob.html");
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}

[Test]
public async Task Should_report_alt_click_downloads()
{
await Browser.GotoAsync("/empty.html");
await Browser.GotoAsync(Content($"""<a href="{PREFIX}{Respond(new Dictionary<string, string> { ["content-type"] = "application/octet-stream" })}">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync(new ClickOptions { Modifiers = [KeyModifier.Alt] }));
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}

[Test]
public async Task Should_report_new_window_downloads()
{
await Browser.GotoAsync(Content($"""<a target=_blank href="{PREFIX}/download">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(File.Exists(download.AbsolutePath), Is.True);
}

[Test]
public async Task Should_download_large_binary_zip()
{
var fileContent = new byte[1 << 20];
for (var i = 0; i < fileContent.Length; i++)
{
fileContent[i] = (byte)(((uint)(i + 1) * 2654435761u) >> 24);
}

await Browser.GotoAsync("/empty.html");
await Browser.GotoAsync(Content($"""<a href="{PREFIX}/binary?bytes={1 << 20}" download="binary.zip">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
var downloaded = await File.ReadAllBytesAsync(download.AbsolutePath);
Assert.That(downloaded, Has.Length.EqualTo(fileContent.Length));
Assert.That(downloaded.AsSpan().SequenceEqual(fileContent), Is.True);
}

[Test]
public async Task Should_report_downloads_with_interception()
{
await Browser.RouteAsync(new System.Text.RegularExpressions.Regex(".*"), route => route.ContinueAsync());
await Browser.GotoAsync(Content($"""<a href="{PREFIX}/download">download</a>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.Locator("a").TapAsync());
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}

[Test]
public async Task Should_emit_download_event_from_nested_iframes()
{
var third = Content($""" <a href="{PREFIX}/download">download</a>""");
var second = Content($"""<iframe src="{PREFIX}{third}"></iframe>""");
await Browser.GotoAsync(Content($"""<iframe src="{PREFIX}{second}"></iframe>"""));
var download = await Browser.WaitForDownloadAsync(() => Browser.FrameLocator("iframe").FrameLocator("iframe").Locator("a").TapAsync());
Assert.That(File.Exists(download.AbsolutePath), Is.True);
Assert.That(await File.ReadAllTextAsync(download.AbsolutePath), Is.EqualTo("Hello world"));
}
}
Loading
Loading