|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using BasicTestApp; |
| 7 | +using BasicTestApp.RouterTest; |
| 8 | +using Microsoft.AspNetCore.BrowserTesting; |
| 9 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; |
| 10 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; |
| 11 | +using Microsoft.AspNetCore.Testing; |
| 12 | +using Moq; |
| 13 | +using PlaywrightSharp; |
| 14 | +using Xunit; |
| 15 | +using Xunit.Abstractions; |
| 16 | + |
| 17 | +namespace Microsoft.AspNetCore.Components.E2ETest.Tests |
| 18 | +{ |
| 19 | + public class DownloadAnchorTest |
| 20 | + : ServerTestBase<ToggleExecutionModeServerFixture<Program>> |
| 21 | + { |
| 22 | + public DownloadAnchorTest( |
| 23 | + ToggleExecutionModeServerFixture<Program> serverFixture, |
| 24 | + ITestOutputHelper output) |
| 25 | + : base(serverFixture, output) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + private IPage _page; |
| 30 | + private IBrowserContext _browser; |
| 31 | + |
| 32 | + protected override async Task InitializeCoreAsync(TestContext context) |
| 33 | + { |
| 34 | + await base.InitializeCoreAsync(context); |
| 35 | + |
| 36 | + _browser = await BrowserManager.GetBrowserInstance(BrowserKind.Chromium, BrowserContextInfo); |
| 37 | + var page = await _browser.NewPageAsync(); |
| 38 | + var url = _serverFixture.RootUri + "subdir"; |
| 39 | + var response = await page.GoToAsync(url); |
| 40 | + |
| 41 | + await MountTestComponentAsync<TestRouter>(page); |
| 42 | + _page = page; |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task DownloadFileFromAnchor() |
| 47 | + { |
| 48 | + // Arrange |
| 49 | + var initialUrl = _page.Url; |
| 50 | + var downloadTask = _page.WaitForEventAsync(PageEvent.Download); |
| 51 | + |
| 52 | + // Act |
| 53 | + await Task.WhenAll( |
| 54 | + downloadTask, |
| 55 | + _page.ClickAsync("a[download]")); |
| 56 | + |
| 57 | + // Assert URL should still be same as before click |
| 58 | + Assert.Equal(initialUrl, _page.Url); |
| 59 | + |
| 60 | + // Assert that the resource was downloaded |
| 61 | + var download = downloadTask.Result.Download; |
| 62 | + Assert.Equal($"{_serverFixture.RootUri}subdir/images/blazor_logo_1000x.png", download.Url); |
| 63 | + Assert.Equal("blazor_logo_1000x.png", download.SuggestedFilename); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments