Best Playwright-dotnet code snippet using Microsoft.Playwright.Core.Artifact
Connection.cs
Source:Connection.cs
...219 var parent = string.IsNullOrEmpty(parentGuid) ? _rootObject : Objects[parentGuid];220#pragma warning disable CA2000 // Dispose objects before losing scope221 switch (type)222 {223 case ChannelOwnerType.Artifact:224 result = new Artifact(parent, guid, initializer?.ToObject<ArtifactInitializer>(DefaultJsonSerializerOptions));225 break;226 case ChannelOwnerType.BindingCall:227 result = new BindingCall(parent, guid, initializer?.ToObject<BindingCallInitializer>(DefaultJsonSerializerOptions));228 break;229 case ChannelOwnerType.Playwright:230 result = new PlaywrightImpl(parent, guid, initializer?.ToObject<PlaywrightInitializer>(DefaultJsonSerializerOptions));231 break;232 case ChannelOwnerType.Browser:233 var browserInitializer = initializer?.ToObject<BrowserInitializer>(DefaultJsonSerializerOptions);234 result = new Browser(parent, guid, browserInitializer);235 break;236 case ChannelOwnerType.BrowserType:237 var browserTypeInitializer = initializer?.ToObject<BrowserTypeInitializer>(DefaultJsonSerializerOptions);238 result = new Core.BrowserType(parent, guid, browserTypeInitializer);...
Tracing.cs
Source:Tracing.cs
1/*2 * MIT License3 *4 * Copyright (c) Microsoft Corporation.5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24using System.Threading.Tasks;25using Microsoft.Playwright.Transport;26using Microsoft.Playwright.Transport.Channels;27using Microsoft.Playwright.Transport.Protocol;28namespace Microsoft.Playwright.Core29{30 internal class Tracing : ChannelOwnerBase, IChannelOwner<Tracing>, ITracing31 {32 private readonly TracingChannel _channel;33 public Tracing(IChannelOwner parent, string guid) : base(parent, guid)34 {35 _channel = new(guid, parent.Connection, this);36 }37 internal LocalUtils LocalUtils { get; set; }38 ChannelBase IChannelOwner.Channel => _channel;39 IChannel<Tracing> IChannelOwner<Tracing>.Channel => _channel;40 public async Task StartAsync(TracingStartOptions options = default)41 {42 await _channel.TracingStartAsync(43 name: options?.Name,44 title: options?.Title,45 screenshots: options?.Screenshots,46 snapshots: options?.Snapshots,47 sources: options?.Sources).ConfigureAwait(false);48 await _channel.StartChunkAsync(options?.Title).ConfigureAwait(false);49 }50 public Task StartChunkAsync() => StartChunkAsync();51 public Task StartChunkAsync(TracingStartChunkOptions options) => _channel.StartChunkAsync(title: options?.Title);52 public async Task StopChunkAsync(TracingStopChunkOptions options = null)53 {54 await DoStopChunkAsync(filePath: options.Path).ConfigureAwait(false);55 }56 public async Task StopAsync(TracingStopOptions options = default)57 {58 await StopChunkAsync(new() { Path = options?.Path }).ConfigureAwait(false);59 await _channel.TracingStopAsync().ConfigureAwait(false);60 }61 private async Task DoStopChunkAsync(string filePath)62 {63 bool isLocal = !_channel.Connection.IsRemote;64 var mode = "doNotSave";65 if (!string.IsNullOrEmpty(filePath))66 {67 if (isLocal)68 {69 mode = "compressTraceAndSources";70 }71 else72 {73 mode = "compressTrace";74 }75 }76 var (artifact, sourceEntries) = await _channel.StopChunkAsync(mode).ConfigureAwait(false);77 // Not interested in artifacts.78 if (string.IsNullOrEmpty(filePath))79 {80 return;81 }82 // The artifact may be missing if the browser closed while stopping tracing.83 if (artifact == null)84 {85 return;86 }87 // Save trace to the final local file.88 await artifact.SaveAsAsync(filePath).ConfigureAwait(false);89 await artifact.DeleteAsync().ConfigureAwait(false);90 // Add local sources to the remote trace if necessary.91 if (sourceEntries.Count > 0)92 {93 await LocalUtils.ZipAsync(filePath, sourceEntries).ConfigureAwait(false);94 }95 }96 }97}...
ArtifactChannelImpl.cs
Source:ArtifactChannelImpl.cs
...35using Microsoft.Playwright.Helpers;36#nullable enable37namespace Microsoft.Playwright.Transport.Channels38{39 internal class ArtifactChannelImpl : Channel<Artifact>40 {41 public ArtifactChannelImpl(string guid, Connection connection, Artifact owner) : base(guid, connection, owner)42 {43 }44 internal virtual async Task<string?> PathAfterFinishedAsync()45 => (await Connection.SendMessageToServerAsync<JsonElement?>(46 Guid,47 "pathAfterFinished",48 null)49 .ConfigureAwait(false)).GetString("value", true);50 internal virtual async Task SaveAsAsync(string path)51 => await Connection.SendMessageToServerAsync<JsonElement>(52 Guid,53 "saveAs",54 new55 {56 path = path,57 }58 )59 .ConfigureAwait(false);60 internal virtual async Task<Stream> SaveAsStreamAsync()61 => (await Connection.SendMessageToServerAsync<JsonElement>(62 Guid,63 "saveAsStream",64 null)65 .ConfigureAwait(false)).GetObject<Stream>("stream", Connection);66 internal virtual async Task<string?> FailureAsync()67 => (await Connection.SendMessageToServerAsync<JsonElement?>(68 Guid,69 "failure",70 null)71 .ConfigureAwait(false)).GetString("error", true);72 internal virtual async Task<Stream?> StreamAsync()73 => (await Connection.SendMessageToServerAsync<JsonElement?>(74 Guid,75 "stream",76 null)77 .ConfigureAwait(false))?.GetObject<Stream>("stream", Connection);78 internal virtual async Task CancelAsync()79 => await Connection.SendMessageToServerAsync<JsonElement>(80 Guid,81 "cancel",82 null)83 .ConfigureAwait(false);84 internal virtual async Task DeleteAsync()85 => await Connection.SendMessageToServerAsync<JsonElement>(86 Guid,87 "delete",88 null)89 .ConfigureAwait(false);90 }91 internal partial class ArtifactChannel : ArtifactChannelImpl92 {93 public ArtifactChannel(string guid, Connection connection, Artifact owner) : base(guid, connection, owner)94 {95 }96 }97}98#nullable disable...
Artifact.cs
Source:Artifact.cs
...29using Microsoft.Playwright.Transport.Channels;30using Microsoft.Playwright.Transport.Protocol;31namespace Microsoft.Playwright.Core32{33 internal class Artifact : ChannelOwnerBase, IChannelOwner<Artifact>34 {35 private readonly Connection _connection;36 private readonly ArtifactChannel _channel;37 internal Artifact(IChannelOwner parent, string guid, ArtifactInitializer initializer) : base(parent, guid)38 {39 _connection = parent.Connection;40 _channel = new(guid, parent.Connection, this);41 AbsolutePath = initializer.AbsolutePath;42 }43 Connection IChannelOwner.Connection => _connection;44 ChannelBase IChannelOwner.Channel => _channel;45 IChannel<Artifact> IChannelOwner<Artifact>.Channel => _channel;46 internal string AbsolutePath { get; }47 public async Task<string> PathAfterFinishedAsync()48 {49 if (_connection.IsRemote)50 {51 throw new PlaywrightException("Path is not available when connecting remotely. Use SaveAsAsync() to save a local copy.");52 }53 return await _channel.PathAfterFinishedAsync().ConfigureAwait(false);54 }55 public async Task SaveAsAsync(string path)56 {57 if (!_connection.IsRemote)58 {59 await _channel.SaveAsAsync(path).ConfigureAwait(false);...
TracingChannel.cs
Source:TracingChannel.cs
...53 => Connection.SendMessageToServerAsync(Guid, "tracingStartChunk", new Dictionary<string, object>54 {55 ["title"] = title,56 });57 internal async Task<(Artifact Artifact, List<NameValueEntry> SourceEntries)> StopChunkAsync(string mode)58 {59 var result = await Connection.SendMessageToServerAsync(Guid, "tracingStopChunk", new Dictionary<string, object>60 {61 ["mode"] = mode,62 }).ConfigureAwait(false);63 var artifact = result.GetObject<Artifact>("artifact", Connection);64 List<NameValueEntry> sourceEntries = new() { };65 if (result.Value.TryGetProperty("sourceEntries", out var sourceEntriesElement))66 {67 var sourceEntriesEnumerator = sourceEntriesElement.EnumerateArray();68 while (sourceEntriesEnumerator.MoveNext())69 {70 JsonElement current = sourceEntriesEnumerator.Current;71 sourceEntries.Add(current.Deserialize<NameValueEntry>(new JsonSerializerOptions()72 {73 PropertyNameCaseInsensitive = true,74 }));75 }76 }77 return (artifact, sourceEntries);...
Video.cs
Source:Video.cs
...26namespace Microsoft.Playwright.Core27{28 internal class Video : IVideo29 {30 private readonly TaskCompletionSource<Artifact> _artifactTcs = new();31 private readonly bool _isRemote;32 public Video(Page page, Connection connection)33 {34 _isRemote = connection.IsRemote;35 page.Close += (_, _) => _artifactTcs.TrySetCanceled();36 page.Crash += (_, _) => _artifactTcs.TrySetCanceled();37 }38 public async Task DeleteAsync()39 {40 var artifact = await _artifactTcs.Task.ConfigureAwait(false);41 await artifact.DeleteAsync().ConfigureAwait(false);42 }43 public async Task<string> PathAsync()44 {45 if (_isRemote)46 {47 throw new PlaywrightException("Path is not available when connecting remotely. Use SaveAsAsync() to save a local copy.");48 }49 var artifact = await _artifactTcs.Task.ConfigureAwait(false);50 return artifact.AbsolutePath;51 }52 public async Task SaveAsAsync(string path)53 {54 var artifact = await _artifactTcs.Task.ConfigureAwait(false);55 await artifact.SaveAsAsync(path).ConfigureAwait(false);56 }57 internal void ArtifactReady(Artifact artifact) => _artifactTcs.TrySetResult(artifact);58 }59}...
Download.cs
Source:Download.cs
...31 /// Download event is emitted once the download starts.32 /// </summary>33 internal class Download : IDownload34 {35 private readonly Artifact _artifact;36 internal Download(IPage page, string url, string suggestedFilename, Artifact artifact)37 {38 Page = page;39 Url = url;40 SuggestedFilename = suggestedFilename;41 _artifact = artifact;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();...
VideoEventArgs.cs
Source:VideoEventArgs.cs
...26namespace Microsoft.Playwright.Transport.Channels27{28 internal class VideoEventArgs : EventArgs29 {30 public Artifact Artifact { get; set; }31 }32}...
Artifact
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 var browser = await playwright.Chromium.LaunchAsync();10 var context = await browser.NewContextAsync();11 var page = await context.NewPageAsync();12 await page.ScreenshotAsync("google.png");13 await browser.CloseAsync();14 }15 }16}17using Microsoft.Playwright;18using System;19using System.Threading.Tasks;20{21 {22 static async Task Main(string[] args)23 {24 using var playwright = await Playwright.CreateAsync();25 var browser = await playwright.Chromium.LaunchAsync();26 var context = await browser.NewContextAsync();27 var page = await context.NewPageAsync();28 await page.ScreenshotAsync("google.png");29 await browser.CloseAsync();30 }31 }32}33using Microsoft.Playwright;34using System;35using System.Threading.Tasks;36{37 {38 static async Task Main(string[] args)39 {40 using var playwright = await Playwright.CreateAsync();41 var browser = await playwright.Chromium.LaunchAsync();42 var context = await browser.NewContextAsync();43 var page = await context.NewPageAsync();44 await page.ScreenshotAsync("google.png");45 await browser.CloseAsync();46 }47 }48}49using Microsoft.Playwright;50using System;51using System.Threading.Tasks;52{53 {54 static async Task Main(string[] args)55 {56 using var playwright = await Playwright.CreateAsync();57 var browser = await playwright.Chromium.LaunchAsync();58 var context = await browser.NewContextAsync();59 var page = await context.NewPageAsync();60 await page.ScreenshotAsync("google.png");
Artifact
Using AI Code Generation
1using Microsoft.Playwright.Core;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 var playwright = await Playwright.CreateAsync();9 var browser = await playwright.Chromium.LaunchAsync();10 var context = await browser.NewContextAsync();11 var page = await context.NewPageAsync();12 var element = await page.QuerySelectorAsync("input");13 var artifact = await element.ScreenshotAsync();14 Console.WriteLine(artifact);15 await browser.CloseAsync();16 }17 }18}19using Microsoft.Playwright.Core;20using System;21using System.Threading.Tasks;22{23 {24 static async Task Main(string[] args)25 {26 var playwright = await Playwright.CreateAsync();27 var browser = await playwright.Chromium.LaunchAsync();28 var context = await browser.NewContextAsync();29 var page = await context.NewPageAsync();30 var element = await page.QuerySelectorAsync("input");31 var artifact = await element.ScreenshotAsync();32 Console.WriteLine(artifact);33 await artifact.SaveAsAsync("C:\\Users\\user\\Desktop\\Playwright\\screenshot.png");34 await browser.CloseAsync();35 }36 }37}
Artifact
Using AI Code Generation
1using Microsoft.Playwright.Core;2using Microsoft.Playwright.Core.Helpers;3using Microsoft.Playwright.Core.Helpers.Json;4using Microsoft.Playwright.Core.Helpers.Json.Converters;5using Microsoft.Playwright.Core.Helpers.Json.Serialization;6using Microsoft.Playwright.Core.Helpers.Logging;7using Microsoft.Playwright.Core.Helpers.Logging.Abstractions;8using Microsoft.Playwright.Core.Helpers.Logging.Abstractions.Internal;9using Microsoft.Playwright.Core.Helpers.Logging.Internal;10using Microsoft.Playwright.Core.Helpers.Logging.Serilog;11using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Extensions;12using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Internal;13using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Sinks;14using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Sinks.Internal;15using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Sinks.SystemConsole;16using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Sinks.SystemConsole.Themes;17using Microsoft.Playwright.Core.Helpers.Logging.Serilog.Sinks.SystemConsole.Themes.Internal;18using Microsoft.Playwright.Core.Helpers.Serialization;19using Microsoft.Playwright.Core.Helpers.Serialization.Json;20using Microsoft.Playwright.Core.Helpers.Serialization.Json.Converters;21using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization;22using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal;23using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Converters;24using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers;25using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Collections;26using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Primitives;27using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs;28using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Collections;29using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives;30using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Collections;31using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Collections.Internal;32using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Internal;33using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Internal.Collections;34using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Internal.Collections.Internal;35using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Internal.Internal;36using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Internal.Internal.Collections;37using Microsoft.Playwright.Core.Helpers.Serialization.Json.Serialization.Internal.Writers.Structs.Primitives.Internal.Internal.Collections.Internal;
Artifact
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using Microsoft.Playwright;5{6 {7 static async Task Main(string[] args)8 {9 using var playwright = await Playwright.CreateAsync();10 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 var element = await page.QuerySelectorAsync("input[title='Search']");15 await element.TypeAsync("Hello World");16 await page.Keyboard.PressAsync("Enter");17 await Task.Delay(2000);18 var artifact = await page.SaveAsAsync(new PageSaveAsOptions19 {20 Path = Path.Combine(Directory.GetCurrentDirectory(), "google.png")21 });22 Console.WriteLine($"Saved as: {artifact.Path}");23 }24 }25}26using System;27using System.Threading.Tasks;28using Microsoft.Playwright;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 var elements = await page.QuerySelectorAllAsync("a");39 foreach (var element in elements)40 {41 Console.WriteLine(await element.TextContentAsync());42 }43 }44 }45}
Artifact
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var page = await browser.NewPageAsync();4await artifact.DeleteAsync();5await browser.CloseAsync();6var playwright = await Playwright.CreateAsync();7var browser = await playwright.Chromium.LaunchAsync();8var page = await browser.NewPageAsync();9await artifact.DeleteAsync();10await browser.CloseAsync();11var playwright = await Playwright.CreateAsync();12var browser = await playwright.Chromium.LaunchAsync();13var page = await browser.NewPageAsync();14await artifact.DeleteAsync();15await browser.CloseAsync();16var playwright = await Playwright.CreateAsync();17var browser = await playwright.Chromium.LaunchAsync();18var page = await browser.NewPageAsync();19await artifact.DeleteAsync();20await browser.CloseAsync();21var playwright = await Playwright.CreateAsync();22var browser = await playwright.Chromium.LaunchAsync();23var page = await browser.NewPageAsync();24await artifact.DeleteAsync();25await browser.CloseAsync();26var playwright = await Playwright.CreateAsync();27var browser = await playwright.Chromium.LaunchAsync();28var page = await browser.NewPageAsync();
Artifact
Using AI Code Generation
1using Microsoft.Playwright.Core;2using System.Threading.Tasks;3{4 {5 public async Task TestMethod1()6 {7 using var playwright = await Playwright.CreateAsync();8 var browser = await playwright.Chromium.LaunchAsync();9 var page = await browser.NewPageAsync();10 var element = await page.QuerySelectorAsync("#content > div > div > div > div > div.col-md-6.col-sm-6.col-xs-12 > div > div > div > div > div > div > p:nth-child(2)");11 var text = await element.TextContentAsync();12 await browser.CloseAsync();13 }14 }15}
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!!