Best Playwright-dotnet code snippet using Microsoft.Playwright.Core.Download.PathAsync
BrowserTypeConnectTests.cs
Source:BrowserTypeConnectTests.cs
...305 await context.CloseAsync();306 var videoSavePath = tempDirectory.Path + "my-video.webm";307 await page.Video.SaveAsAsync(videoSavePath);308 Assert.That(videoSavePath, Does.Exist);309 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(async () => await page.Video.PathAsync());310 StringAssert.Contains("Path is not available when connecting remotely. Use SaveAsAsync() to save a local copy", exception.Message);311 }312 [PlaywrightTest("browsertype-connect.spec.ts", "should save download")]313 public async Task ShouldSaveDownload()314 {315 Server.SetRoute("/download", context =>316 {317 context.Response.Headers["Content-Type"] = "application/octet-stream";318 context.Response.Headers["Content-Disposition"] = "attachment";319 return context.Response.WriteAsync("Hello world");320 });321 var browser = await BrowserType.ConnectAsync(_remoteServer.WSEndpoint);322 var page = await browser.NewPageAsync(new() { AcceptDownloads = true });323 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");324 var downloadTask = page.WaitForDownloadAsync();325 await TaskUtils.WhenAll(326 downloadTask,327 page.ClickAsync("a"));328 using var tmpDir = new TempDirectory();329 string userPath = Path.Combine(tmpDir.Path, "these", "are", "directories", "download.txt");330 var download = downloadTask.Result;331 await download.SaveAsAsync(userPath);332 Assert.True(new FileInfo(userPath).Exists);333 Assert.AreEqual("Hello world", File.ReadAllText(userPath));334 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => download.PathAsync());335 Assert.AreEqual("Path is not available when connecting remotely. Use SaveAsAsync() to save a local copy.", exception.Message);336 await browser.CloseAsync();337 }338 [PlaywrightTest("browsertype-connect.spec.ts", "should error when saving download after deletion")]339 public async Task ShouldErrorWhenSavingDownloadAfterDeletion()340 {341 Server.SetRoute("/download", context =>342 {343 context.Response.Headers["Content-Type"] = "application/octet-stream";344 context.Response.Headers["Content-Disposition"] = "attachment";345 return context.Response.WriteAsync("Hello world");346 });347 var browser = await BrowserType.ConnectAsync(_remoteServer.WSEndpoint);348 var page = await browser.NewPageAsync(new() { AcceptDownloads = true });...
DownloadTests.cs
Source:DownloadTests.cs
...77 Page.ClickAsync("a"));78 var download = downloadTask.Result;79 Assert.AreEqual($"{Server.Prefix}/downloadWithFilename", download.Url);80 Assert.AreEqual("file.txt", download.SuggestedFilename);81 string path = await download.PathAsync();82 Assert.True(new FileInfo(path).Exists);83 Assert.AreEqual("Hello world", File.ReadAllText(path));84 }85 [PlaywrightTest("download.spec.ts", "should report downloads with acceptDownloads: true")]86 public async Task ShouldReportDownloadsWithAcceptDownloadsTrue()87 {88 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });89 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");90 var download = await page.RunAndWaitForDownloadAsync(async () =>91 {92 await page.ClickAsync("a");93 });94 string path = await download.PathAsync();95 Assert.True(new FileInfo(path).Exists);96 Assert.AreEqual("Hello world", File.ReadAllText(path));97 }98 [PlaywrightTest("download.spec.ts", "should save to user-specified path")]99 public async Task ShouldSaveToUserSpecifiedPath()100 {101 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });102 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");103 var download = await page.RunAndWaitForDownloadAsync(async () =>104 {105 await page.ClickAsync("a");106 });107 using var tmpDir = new TempDirectory();108 string userPath = Path.Combine(tmpDir.Path, "download.txt");109 await download.SaveAsAsync(userPath);110 Assert.True(new FileInfo(userPath).Exists);111 Assert.AreEqual("Hello world", File.ReadAllText(userPath));112 await page.CloseAsync();113 }114 [PlaywrightTest("download.spec.ts", "should save to user-specified path without updating original path")]115 public async Task ShouldSaveToUserSpecifiedPathWithoutUpdatingOriginalPath()116 {117 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });118 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");119 var download = await page.RunAndWaitForDownloadAsync(async () =>120 {121 await page.ClickAsync("a");122 });123 using var tmpDir = new TempDirectory();124 string userPath = Path.Combine(tmpDir.Path, "download.txt");125 await download.SaveAsAsync(userPath);126 Assert.True(new FileInfo(userPath).Exists);127 Assert.AreEqual("Hello world", File.ReadAllText(userPath));128 string originalPath = await download.PathAsync();129 Assert.True(new FileInfo(originalPath).Exists);130 Assert.AreEqual("Hello world", File.ReadAllText(originalPath));131 await page.CloseAsync();132 }133 [PlaywrightTest("download.spec.ts", "should save to two different paths with multiple saveAs calls")]134 public async Task ShouldSaveToTwoDifferentPathsWithMultipleSaveAsCalls()135 {136 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });137 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");138 var download = await page.RunAndWaitForDownloadAsync(async () =>139 {140 await page.ClickAsync("a");141 });142 using var tmpDir = new TempDirectory();143 string userPath = Path.Combine(tmpDir.Path, "download.txt");144 await download.SaveAsAsync(userPath);145 Assert.True(new FileInfo(userPath).Exists);146 Assert.AreEqual("Hello world", File.ReadAllText(userPath));147 string anotherUserPath = Path.Combine(tmpDir.Path, "download (2).txt");148 await download.SaveAsAsync(anotherUserPath);149 Assert.True(new FileInfo(anotherUserPath).Exists);150 Assert.AreEqual("Hello world", File.ReadAllText(anotherUserPath));151 await page.CloseAsync();152 }153 [PlaywrightTest("download.spec.ts", "should save to overwritten filepath")]154 public async Task ShouldSaveToOverwrittenFilepath()155 {156 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });157 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");158 var downloadTask = page.WaitForDownloadAsync();159 await TaskUtils.WhenAll(160 downloadTask,161 page.ClickAsync("a"));162 using var tmpDir = new TempDirectory();163 string userPath = Path.Combine(tmpDir.Path, "download.txt");164 var download = downloadTask.Result;165 await download.SaveAsAsync(userPath);166 Assert.AreEqual(1, new DirectoryInfo(tmpDir.Path).GetFiles().Length);167 await download.SaveAsAsync(userPath);168 Assert.AreEqual(1, new DirectoryInfo(tmpDir.Path).GetFiles().Length);169 await page.CloseAsync();170 }171 [PlaywrightTest("download.spec.ts", "should create subdirectories when saving to non-existent user-specified path")]172 public async Task ShouldCreateSubdirectoriesWhenSavingToNonExistentUserSpecifiedPath()173 {174 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });175 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");176 var downloadTask = page.WaitForDownloadAsync();177 await TaskUtils.WhenAll(178 downloadTask,179 page.ClickAsync("a"));180 using var tmpDir = new TempDirectory();181 string userPath = Path.Combine(tmpDir.Path, "these", "are", "directories", "download.txt");182 var download = downloadTask.Result;183 await download.SaveAsAsync(userPath);184 Assert.True(new FileInfo(userPath).Exists);185 Assert.AreEqual("Hello world", File.ReadAllText(userPath));186 await page.CloseAsync();187 }188 [PlaywrightTest("download.spec.ts", "should error when saving with downloads disabled")]189 public async Task ShouldErrorWhenSavingWithDownloadsDisabled()190 {191 var page = await Browser.NewPageAsync(new() { AcceptDownloads = false });192 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");193 var downloadTask = page.WaitForDownloadAsync();194 await TaskUtils.WhenAll(195 downloadTask,196 page.ClickAsync("a"));197 using var tmpDir = new TempDirectory();198 string userPath = Path.Combine(tmpDir.Path, "download.txt");199 var download = downloadTask.Result;200 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => download.SaveAsAsync(userPath));201 StringAssert.Contains("Pass { acceptDownloads: true } when you are creating your browser context", exception.Message);202 }203 [PlaywrightTest("download.spec.ts", "should error when saving after deletion")]204 public async Task ShouldErrorWhenSavingAfterDeletion()205 {206 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });207 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");208 var downloadTask = page.WaitForDownloadAsync();209 await TaskUtils.WhenAll(210 downloadTask,211 page.ClickAsync("a"));212 using var tmpDir = new TempDirectory();213 string userPath = Path.Combine(tmpDir.Path, "download.txt");214 var download = downloadTask.Result;215 await download.DeleteAsync();216 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => download.SaveAsAsync(userPath));217 StringAssert.Contains("Target page, context or browser has been closed", exception.Message);218 }219 [PlaywrightTest("download.spec.ts", "should report non-navigation downloads")]220 public async Task ShouldReportNonNavigationDownloads()221 {222 Server.SetRoute("/download", context =>223 {224 context.Response.Headers["Content-Type"] = "application/octet-stream";225 return context.Response.WriteAsync("Hello world");226 });227 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });228 await page.GotoAsync(Server.EmptyPage);229 await page.SetContentAsync($"<a download=\"file.txt\" href=\"{Server.Prefix}/download\">download</a>");230 var downloadTask = page.WaitForDownloadAsync();231 await TaskUtils.WhenAll(232 downloadTask,233 page.ClickAsync("a"));234 var download = downloadTask.Result;235 Assert.AreEqual("file.txt", download.SuggestedFilename);236 string path = await download.PathAsync();237 Assert.True(new FileInfo(path).Exists);238 Assert.AreEqual("Hello world", File.ReadAllText(path));239 await page.CloseAsync();240 }241 [PlaywrightTest("download.spec.ts", "should report download path within page.on('download', â¦) handler for Files")]242 public async Task ShouldReportDownloadPathWithinPageOnDownloadHandlerForFiles()243 {244 var downloadPathTcs = new TaskCompletionSource<string>();245 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });246 page.Download += async (_, e) =>247 {248 downloadPathTcs.TrySetResult(await e.PathAsync());249 };250 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");251 await page.ClickAsync("a");252 string path = await downloadPathTcs.Task;253 Assert.AreEqual("Hello world", File.ReadAllText(path));254 await page.CloseAsync();255 }256 [PlaywrightTest("download.spec.ts", "should report download path within page.on('download', â¦) handler for Blobs")]257 public async Task ShouldReportDownloadPathWithinPageOnDownloadHandlerForBlobs()258 {259 var downloadPathTcs = new TaskCompletionSource<string>();260 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });261 page.Download += async (_, e) =>262 {263 downloadPathTcs.TrySetResult(await e.PathAsync());264 };265 await page.GotoAsync(Server.Prefix + "/download-blob.html");266 await page.ClickAsync("a");267 string path = await downloadPathTcs.Task;268 Assert.AreEqual("Hello world", File.ReadAllText(path));269 await page.CloseAsync();270 }271 [PlaywrightTest("download.spec.ts", "should report alt-click downloads")]272 [Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]273 public async Task ShouldReportAltClickDownloads()274 {275 Server.SetRoute("/download", context =>276 {277 context.Response.Headers["Content-Type"] = "application/octet-stream";278 return context.Response.WriteAsync("Hello world");279 });280 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });281 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");282 var downloadTask = page.WaitForDownloadAsync();283 await TaskUtils.WhenAll(284 downloadTask,285 page.ClickAsync("a", new() { Modifiers = new[] { KeyboardModifier.Alt } }));286 var download = downloadTask.Result;287 string path = await download.PathAsync();288 Assert.True(new FileInfo(path).Exists);289 Assert.AreEqual("Hello world", File.ReadAllText(path));290 }291 [PlaywrightTest("download.spec.ts", "should report new window downloads")]292 public async Task ShouldReportNewWindowDownloads()293 {294 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });295 await page.SetContentAsync($"<a target=_blank href=\"{Server.Prefix}/download\">download</a>");296 var downloadTask = page.WaitForDownloadAsync();297 await TaskUtils.WhenAll(298 downloadTask,299 page.ClickAsync("a"));300 var download = downloadTask.Result;301 string path = await download.PathAsync();302 Assert.True(new FileInfo(path).Exists);303 Assert.AreEqual("Hello world", File.ReadAllText(path));304 }305 [PlaywrightTest("download.spec.ts", "should delete file")]306 public async Task ShouldDeleteFile()307 {308 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });309 await page.SetContentAsync($"<a target=_blank href=\"{Server.Prefix}/download\">download</a>");310 var downloadTask = page.WaitForDownloadAsync();311 await TaskUtils.WhenAll(312 downloadTask,313 page.ClickAsync("a"));314 var download = downloadTask.Result;315 string path = await download.PathAsync();316 Assert.True(new FileInfo(path).Exists);317 await download.DeleteAsync();318 Assert.False(new FileInfo(path).Exists);319 await page.CloseAsync();320 }321 [PlaywrightTest("download.spec.ts", "should expose stream")]322 public async Task ShouldExposeStream()323 {324 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });325 await page.SetContentAsync($"<a target=_blank href=\"{Server.Prefix}/downloadLarge\">download</a>");326 var downloadTask = page.WaitForDownloadAsync();327 await TaskUtils.WhenAll(328 downloadTask,329 page.ClickAsync("a"));330 var download = downloadTask.Result;331 var expected = string.Empty;332 for (var i = 0; i < 10_000; i++)333 {334 expected += $"a{i}";335 }336 using (var stream = await download.CreateReadStreamAsync())337 {338 Assert.AreEqual(expected, await new StreamReader(stream).ReadToEndAsync());339 }340 await page.CloseAsync();341 }342 [PlaywrightTest("download.spec.ts", "should delete downloads on context destruction")]343 public async Task ShouldDeleteDownloadsOnContextDestruction()344 {345 var page = await Browser.NewPageAsync(new() { AcceptDownloads = true });346 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");347 var download1Task = page.WaitForDownloadAsync();348 await TaskUtils.WhenAll(349 download1Task,350 page.ClickAsync("a"));351 var download2Task = page.WaitForDownloadAsync();352 await TaskUtils.WhenAll(353 download2Task,354 page.ClickAsync("a"));355 string path1 = await download1Task.Result.PathAsync();356 string path2 = await download2Task.Result.PathAsync();357 Assert.True(new FileInfo(path1).Exists);358 Assert.True(new FileInfo(path2).Exists);359 await page.Context.CloseAsync();360 Assert.False(new FileInfo(path1).Exists);361 Assert.False(new FileInfo(path2).Exists);362 }363 [PlaywrightTest("download.spec.ts", "should delete downloads on browser gone")]364 public async Task ShouldDeleteDownloadsOnBrowserGone()365 {366 var browser = await BrowserType.LaunchAsync();367 var page = await browser.NewPageAsync(new() { AcceptDownloads = true });368 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");369 var download1Task = page.WaitForDownloadAsync();370 await TaskUtils.WhenAll(371 download1Task,372 page.ClickAsync("a"));373 var download2Task = page.WaitForDownloadAsync();374 await TaskUtils.WhenAll(375 download2Task,376 page.ClickAsync("a"));377 string path1 = await download1Task.Result.PathAsync();378 string path2 = await download2Task.Result.PathAsync();379 Assert.True(new FileInfo(path1).Exists);380 Assert.True(new FileInfo(path2).Exists);381 await browser.CloseAsync();382 Assert.False(new FileInfo(path1).Exists);383 Assert.False(new FileInfo(path2).Exists);384 Assert.False(new FileInfo(Path.Combine(path1, "..")).Exists);385 }386 [PlaywrightTest("download.spec.ts", "should be able to cancel pending downloads")]387 public async Task ShouldBeAbleToCancelPendingDownload()388 {389 var browser = await BrowserType.LaunchAsync();390 var page = await browser.NewPageAsync(new() { AcceptDownloads = true });391 await page.SetContentAsync($"<a href=\"{Server.Prefix}/downloadWithDelay\">download</a>");392 var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a"));393 await download.CancelAsync();394 var failure = await download.FailureAsync();395 Assert.AreEqual("canceled", failure);396 await page.CloseAsync();397 }398 [PlaywrightTest("download.spec.ts", "should not fail explicitly to cancel a download even if that is already finished")]399 public async Task ShouldNotFailWhenCancellingACompletedDownload()400 {401 var browser = await BrowserType.LaunchAsync();402 var page = await browser.NewPageAsync(new() { AcceptDownloads = true });403 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");404 var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a"));405 using var tmpDir = new TempDirectory();406 string userPath = Path.Combine(tmpDir.Path, "download.txt");407 await download.SaveAsAsync(userPath);408 Assert.IsTrue(File.Exists(userPath));409 await download.CancelAsync();410 var failure = await download.FailureAsync();411 Assert.IsNull(failure);412 await page.CloseAsync();413 }414 [PlaywrightTest("download.spec.ts", "should report downloads with interception")]415 public async Task ShouldReportDownloadsWithInterception()416 {417 var browser = await BrowserType.LaunchAsync();418 var page = await browser.NewPageAsync(new() { AcceptDownloads = true });419 await page.RouteAsync("*", r => r.ContinueAsync());420 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");421 var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a"));422 var path = await download.PathAsync();423 Assert.IsTrue(File.Exists(path));424 await page.CloseAsync();425 }426 }427}...
DefaultBrowserContext1Tests.cs
Source:DefaultBrowserContext1Tests.cs
...265 await TaskUtils.WhenAll(266 downloadTask,267 page.ClickAsync("a"));268 var download = downloadTask.Result;269 var path = await download.PathAsync();270 Assert.AreEqual(File.Exists(path), true);271 Assert.AreEqual(File.ReadAllText(path), "Hello world");272 }273 private async Task<(TempDirectory tmp, IBrowserContext context, IPage page)> LaunchPersistentAsync(BrowserTypeLaunchPersistentContextOptions options = null)274 {275 var tmp = new TempDirectory();276 var context = await BrowserType.LaunchPersistentContextAsync(277 tmp.Path,278 options: options);279 var page = context.Pages.First();280 return (tmp, context, page);281 }282 }283}...
DownloadsPathTests.cs
Source:DownloadsPathTests.cs
...43 page.ClickAsync("a"));44 var download = downloadTask.Result;45 Assert.AreEqual($"{Server.Prefix}/download", download.Url);46 Assert.AreEqual("file.txt", download.SuggestedFilename);47 await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => download.PathAsync());48 await page.CloseAsync();49 await _browser.CloseAsync();50 Assert.True(new DirectoryInfo(_tmp.Path).Exists);51 }52 [PlaywrightTest("downloads-path.spec.ts", "should delete downloads when context closes")]53 public async Task ShouldDeleteDownloadsWhenContextCloses()54 {55 var page = await _browser.NewPageAsync(new() { AcceptDownloads = true });56 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");57 var downloadTask = page.WaitForDownloadAsync();58 await TaskUtils.WhenAll(59 downloadTask,60 page.ClickAsync("a"));61 var download = downloadTask.Result;62 string path = await download.PathAsync();63 Assert.True(new FileInfo(path).Exists);64 await page.CloseAsync();65 Assert.False(new FileInfo(path).Exists);66 }67 [PlaywrightTest("downloads-path.spec.ts", "should report downloads in downloadsPath folder")]68 public async Task ShouldReportDownloadsInDownloadsPathFolder()69 {70 var page = await _browser.NewPageAsync(new() { AcceptDownloads = true });71 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");72 var downloadTask = page.WaitForDownloadAsync();73 await TaskUtils.WhenAll(74 downloadTask,75 page.ClickAsync("a"));76 var download = downloadTask.Result;77 string path = await download.PathAsync();78 Assert.That(path, Does.StartWith(_tmp.Path));79 await page.CloseAsync();80 }81 [PlaywrightTest("downloads-path.spec.ts", "should report downloads in downloadsPath folder with a relative path")]82 public async Task ShouldReportDownloadsInDownloadsPathFolderWithARelativePath()83 {84 var browser = await Playwright[TestConstants.BrowserName]85 .LaunchAsync(new()86 {87 DownloadsPath = "."88 });89 var page = await browser.NewPageAsync(new()90 {91 AcceptDownloads = true92 });93 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");94 var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a"));95 string path = await download.PathAsync();96 Assert.That(path, Does.StartWith(Directory.GetCurrentDirectory()));97 await page.CloseAsync();98 }99 [PlaywrightTest("downloads-path.spec.ts", "should accept downloads in persistent context")]100 public async Task ShouldAcceptDownloadsInPersistentContext()101 {102 var userProfile = new TempDirectory();103 var browser = await Playwright[TestConstants.BrowserName]104 .LaunchPersistentContextAsync(userProfile.Path, new()105 {106 AcceptDownloads = true,107 DownloadsPath = _tmp.Path108 });109 var page = await browser.NewPageAsync();110 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");111 var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a"));112 Assert.AreEqual($"{Server.Prefix}/download", download.Url);113 Assert.AreEqual("file.txt", download.SuggestedFilename);114 Assert.That(await download.PathAsync(), Does.StartWith(_tmp.Path));115 await page.CloseAsync();116 }117 [PlaywrightTest("downloads-path.spec.ts", "should delete downloads when persistent context closes")]118 public async Task ShouldDeleteDownloadsWhenPersistentContextCloses()119 {120 var userProfile = new TempDirectory();121 var browser = await Playwright[TestConstants.BrowserName]122 .LaunchPersistentContextAsync(userProfile.Path, new()123 {124 AcceptDownloads = true,125 DownloadsPath = _tmp.Path126 });127 var page = await browser.NewPageAsync();128 await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>");129 var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a"));130 var path = await download.PathAsync();131 Assert.IsTrue(File.Exists(path));132 await browser.CloseAsync();133 Assert.IsFalse(File.Exists(path));134 }135 [SetUp]136 public async Task InitializeAsync()137 {138 Server.SetRoute("/download", context =>139 {140 context.Response.Headers["Content-Type"] = "application/octet-stream";141 context.Response.Headers["Content-Disposition"] = "attachment; filename=file.txt";142 return context.Response.WriteAsync("Hello world");143 });144 _tmp = new();...
Download.cs
Source:Download.cs
...42 }43 public IPage Page { get; }44 public string Url { get; }45 public string SuggestedFilename { get; }46 public Task<string> PathAsync() => _artifact.PathAfterFinishedAsync();47 public Task<string> FailureAsync() => _artifact.FailureAsync();48 public Task DeleteAsync() => _artifact.DeleteAsync();49 public Task SaveAsAsync(string path) => _artifact.SaveAsAsync(path);50 public Task<System.IO.Stream> CreateReadStreamAsync() => _artifact.CreateReadStreamAsync();51 public Task CancelAsync() => _artifact.CancelAsync();52 }53}...
PathAsync
Using AI Code Generation
1await page.PathAsync();2await page.PathAsync();3await page.PathAsync();4await page.PathAsync();5await page.PathAsync();6await page.PathAsync();7await page.PathAsync();8await page.PathAsync();9await page.PathAsync();10await page.PathAsync();11await page.PathAsync();12await page.PathAsync();13await page.PathAsync();14await page.PathAsync();15await page.PathAsync();16await page.PathAsync();17await page.PathAsync();18await page.PathAsync();19await page.PathAsync();
PathAsync
Using AI Code Generation
1var playwright = await Microsoft.Playwright.Core.Playwright.CreateAsync();2var browser = await playwright.Firefox.LaunchAsync(headless: false);3var page = await browser.NewPageAsync();4await browser.CloseAsync();5var playwright = await Microsoft.Playwright.Core.Playwright.CreateAsync();6var browser = await playwright.Firefox.LaunchAsync(headless: false);7var page = await browser.NewPageAsync();8await browser.CloseAsync();9var playwright = await Microsoft.Playwright.Core.Playwright.CreateAsync();10var browser = await playwright.Firefox.LaunchAsync(headless: false);11var page = await browser.NewPageAsync();12await browser.CloseAsync();13var playwright = await Microsoft.Playwright.Core.Playwright.CreateAsync();14var browser = await playwright.Firefox.LaunchAsync(headless: false);15var page = await browser.NewPageAsync();16await browser.CloseAsync();17var playwright = await Microsoft.Playwright.Core.Playwright.CreateAsync();18var browser = await playwright.Firefox.LaunchAsync(headless: false);19var page = await browser.NewPageAsync();
PathAsync
Using AI Code Generation
1await download.PathAsync();2await download.PathAsync();3await download.PathAsync();4await download.PathAsync();5await download.PathAsync();6await download.PathAsync();7await download.PathAsync();
PathAsync
Using AI Code Generation
1var download = await page.WaitForDownloadAsync();2var path = await download.PathAsync();3Console.WriteLine(path);4var download = await page.WaitForDownloadAsync();5await download.SaveAsAsync("C:\\Users\\Downloads\\downloadedfile.txt");6var download = await page.WaitForDownloadAsync();7await download.SaveAsAsync(new System.IO.FileInfo("C:\\Users\\Downloads\\downloadedfile.txt"));8var download = await page.WaitForDownloadAsync();9await download.SaveAsAsync(new System.IO.FileInfo("C:\\Users\\Downloads\\downloadedfile.txt"), new Microsoft.Playwright.Core.DownloadSaveAsOptions{ Timeout = 30000 });10var download = await page.WaitForDownloadAsync();11await download.SaveAsAsync(new System.IO.FileInfo("C:\\Users\\Downloads\\downloadedfile.txt"), new Microsoft.Playwright.Core.DownloadSaveAsOptions{ Timeout = 30000, Path = "C:\\Users\\Downloads\\downloadedfile.txt" });12var download = await page.WaitForDownloadAsync();13await download.SaveAsAsync(new System.IO.FileInfo("C:\\Users\\Downloads\\downloadedfile.txt"), new Microsoft.Playwright.Core.DownloadSaveAsOptions{ Timeout = 30000, Path = "C:\\Users\\Downloads\\downloadedfile.txt", NoOverwrite = true });14var download = await page.WaitForDownloadAsync();15await download.SaveAsAsync(new System.IO.FileInfo("C:\\Users\\Downloads\\downloadedfile.txt"), new Microsoft.Playwright.Core.DownloadSaveAsOptions{ Timeout = 30000, Path = "C:\\Users\\Downloads\\downloadedfile.txt", NoOverwrite = true, SuggestedFilename = "downloadedfile.txt" });
PathAsync
Using AI Code Generation
1var download = await page.WaitForDownloadAsync();2var path = await download.PathAsync();3Console.WriteLine(path);4var download = await page.WaitForDownloadAsync();5var path = await download.PathAsync();6Console.WriteLine(path);7var download = await page.WaitForDownloadAsync();8var path = await download.PathAsync();9Console.WriteLine(path);10var download = await page.WaitForDownloadAsync();11var path = await download.PathAsync();12Console.WriteLine(path);13var download = await page.WaitForDownloadAsync();14var path = await download.PathAsync();15Console.WriteLine(path);16var download = await page.WaitForDownloadAsync();17var path = await download.PathAsync();18Console.WriteLine(path);19var download = await page.WaitForDownloadAsync();20var path = await download.PathAsync();21Console.WriteLine(path);22var download = await page.WaitForDownloadAsync();23var path = await download.PathAsync();24Console.WriteLine(path);25var download = await page.WaitForDownloadAsync();26var path = await download.PathAsync();27Console.WriteLine(path);28var download = await page.WaitForDownloadAsync();29var path = await download.PathAsync();30Console.WriteLine(path);31var download = await page.WaitForDownloadAsync();32var path = await download.PathAsync();33Console.WriteLine(path);34var download = await page.WaitForDownloadAsync();35var path = await download.PathAsync();36Console.WriteLine(path);
PathAsync
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using System;4using System.IO;5using System.Threading.Tasks;6{7 {8 static async Task Main(string[] args)9 {10 using var playwright = await Playwright.CreateAsync();11 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions12 {13 });14 var page = await browser.NewPageAsync();15 await page.ClickAsync("text=Images");16 var image = await page.QuerySelectorAsync("img");17 var download = await image.DownloadAsync();18 var path = await download.PathAsync();19 Console.WriteLine(path);20 await browser.CloseAsync();21 }22 }23}24using Microsoft.Playwright;25using Microsoft.Playwright.Core;26using System;27using System.IO;28using System.Threading.Tasks;29{30 {31 static async Task Main(string[] args)32 {33 using var playwright = await Playwright.CreateAsync();34 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions35 {36 });37 var page = await browser.NewPageAsync();38 await page.ClickAsync("text=Images");39 var image = await page.QuerySelectorAsync("img");40 var download = await image.DownloadAsync();41 await download.SaveAsAsync(@"C:\Users\user\Downloads\download.png");42 await browser.CloseAsync();43 }44 }45}
PathAsync
Using AI Code Generation
1await page.ClickAsync("button");2var download = await page.WaitForEventAsync(PageEvent.Download);3await download.PathAsync();4await page.ClickAsync("button");5var download = await page.WaitForEventAsync(PageEvent.Download);6await download.PathAsync();7await page.ClickAsync("button");8var download = await page.WaitForEventAsync(PageEvent.Download);9await download.PathAsync();10await page.ClickAsync("button");11var download = await page.WaitForEventAsync(PageEvent.Download);12await download.PathAsync();13await page.ClickAsync("button");14var download = await page.WaitForEventAsync(PageEvent.Download);15await download.PathAsync();16await page.ClickAsync("button");17var download = await page.WaitForEventAsync(PageEvent.Download);18await download.PathAsync();19await page.ClickAsync("button");20var download = await page.WaitForEventAsync(PageEvent.Download);21await download.PathAsync();22await page.ClickAsync("button");23var download = await page.WaitForEventAsync(PageEvent.Download);24await download.PathAsync();25await page.ClickAsync("button");26var download = await page.WaitForEventAsync(PageEvent.Download);27await download.PathAsync();28await page.ClickAsync("button");29var download = await page.WaitForEventAsync(PageEvent.Download);30await download.PathAsync();31await page.ClickAsync("button");32var download = await page.WaitForEventAsync(PageEvent.Download);33await download.PathAsync();
PathAsync
Using AI Code Generation
1var downloadTask = download.DownloadPathAsync("C:\\Users\\Downloads\\");2var downloadPath = await downloadTask;3Console.WriteLine(downloadPath);4var downloadTask = download.SaveAsAsync("C:\\Users\\Downloads\\");5var downloadPath = await downloadTask;6Console.WriteLine(downloadPath);7var downloadTask = download.SaveAsAsync("C:\\Users\\Downloads\\");8var downloadPath = await downloadTask;9Console.WriteLine(downloadPath);10var downloadTask = download.SaveAsAsync("C:\\Users\\Downloads\\");11var downloadPath = await downloadTask;12Console.WriteLine(downloadPath);13var downloadTask = download.SaveAsAsync("C:\\Users\\Downloads\\");14var downloadPath = await downloadTask;15Console.WriteLine(downloadPath);16var downloadTask = download.SaveAsAsync("C:\\Users\\Downloads\\");17var downloadPath = await downloadTask;18Console.WriteLine(downloadPath);
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!!