Best Playwright-dotnet code snippet using Microsoft.Playwright.LocatorAssertionsToContainTextOptions
ILocatorAssertions.cs
Source:ILocatorAssertions.cs  
...174        /// </code>175        /// </summary>176        /// <param name="expected">Expected substring or RegExp or a list of those.</param>177        /// <param name="options">Call options</param>178        Task ToContainTextAsync(string expected, LocatorAssertionsToContainTextOptions? options = default);179        /// <summary>180        /// <para>181        /// Ensures the <see cref="ILocator"/> points to an element that contains the given182        /// text. You can use regular expressions for the value as well.183        /// </para>184        /// <code>185        /// var locator = Page.Locator(".title");<br/>186        /// await Expect(locator).ToContainTextAsync("substring");<br/>187        /// await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));188        /// </code>189        /// <para>190        /// Note that if array is passed as an expected value, entire lists of elements can191        /// be asserted:192        /// </para>193        /// <code>194        /// var locator = Page.Locator("list > .list-item");<br/>195        /// await Expect(locator).ToContainTextAsync(new string[] { "Text 1", "Text 4", "Text 5" });196        /// </code>197        /// </summary>198        /// <param name="expected">Expected substring or RegExp or a list of those.</param>199        /// <param name="options">Call options</param>200        Task ToContainTextAsync(Regex expected, LocatorAssertionsToContainTextOptions? options = default);201        /// <summary>202        /// <para>203        /// Ensures the <see cref="ILocator"/> points to an element that contains the given204        /// text. You can use regular expressions for the value as well.205        /// </para>206        /// <code>207        /// var locator = Page.Locator(".title");<br/>208        /// await Expect(locator).ToContainTextAsync("substring");<br/>209        /// await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));210        /// </code>211        /// <para>212        /// Note that if array is passed as an expected value, entire lists of elements can213        /// be asserted:214        /// </para>215        /// <code>216        /// var locator = Page.Locator("list > .list-item");<br/>217        /// await Expect(locator).ToContainTextAsync(new string[] { "Text 1", "Text 4", "Text 5" });218        /// </code>219        /// </summary>220        /// <param name="expected">Expected substring or RegExp or a list of those.</param>221        /// <param name="options">Call options</param>222        Task ToContainTextAsync(IEnumerable<string> expected, LocatorAssertionsToContainTextOptions? options = default);223        /// <summary>224        /// <para>225        /// Ensures the <see cref="ILocator"/> points to an element that contains the given226        /// text. You can use regular expressions for the value as well.227        /// </para>228        /// <code>229        /// var locator = Page.Locator(".title");<br/>230        /// await Expect(locator).ToContainTextAsync("substring");<br/>231        /// await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));232        /// </code>233        /// <para>234        /// Note that if array is passed as an expected value, entire lists of elements can235        /// be asserted:236        /// </para>237        /// <code>238        /// var locator = Page.Locator("list > .list-item");<br/>239        /// await Expect(locator).ToContainTextAsync(new string[] { "Text 1", "Text 4", "Text 5" });240        /// </code>241        /// </summary>242        /// <param name="expected">Expected substring or RegExp or a list of those.</param>243        /// <param name="options">Call options</param>244        Task ToContainTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToContainTextOptions? options = default);245        /// <summary>246        /// <para>Ensures the <see cref="ILocator"/> points to an element with given attribute.</para>247        /// <code>248        /// var locator = Page.Locator("input");<br/>249        /// await Expect(locator).ToHaveAttributeAsync("type", "text");250        /// </code>251        /// </summary>252        /// <param name="name">Attribute name.</param>253        /// <param name="value">Expected attribute value.</param>254        /// <param name="options">Call options</param>255        Task ToHaveAttributeAsync(string name, string value, LocatorAssertionsToHaveAttributeOptions? options = default);256        /// <summary>257        /// <para>Ensures the <see cref="ILocator"/> points to an element with given attribute.</para>258        /// <code>...LocatorAssertions.cs
Source:LocatorAssertions.cs  
...50        {51            ExpectedTextValue[] expectedText = null;52            return ExpectImplAsync(expression, expectedText, null, message, options);53        }54        public Task ToContainTextAsync(string expected, LocatorAssertionsToContainTextOptions options = null) =>55            ExpectImplAsync("to.have.text", new ExpectedTextValue() { String = expected, MatchSubstring = true, NormalizeWhiteSpace = true }, expected, "Locator expected to contain text", ConvertToFrameExpectOptions(options));56        public Task ToContainTextAsync(Regex expected, LocatorAssertionsToContainTextOptions options = null) =>57            ExpectImplAsync("to.have.text", ExpectedRegex(expected, new() { MatchSubstring = true, NormalizeWhiteSpace = true }), expected, "Locator expected text matching regex", ConvertToFrameExpectOptions(options));58        public Task ToContainTextAsync(IEnumerable<string> expected, LocatorAssertionsToContainTextOptions options = null) =>59            ExpectImplAsync("to.contain.text.array", expected.Select(text => new ExpectedTextValue() { String = text, MatchSubstring = true, NormalizeWhiteSpace = true }).ToArray(), expected, "Locator expected to contain text", ConvertToFrameExpectOptions(options));60        public Task ToContainTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToContainTextOptions options = null) =>61            ExpectImplAsync("to.contain.text.array", expected.Select(regex => ExpectedRegex(regex, new() { MatchSubstring = true, NormalizeWhiteSpace = true })).ToArray(), expected, "Locator expected text matching regex", ConvertToFrameExpectOptions(options));62        public Task ToHaveAttributeAsync(string name, string value, LocatorAssertionsToHaveAttributeOptions options = null) =>63            ToHaveAttributeAsync(name, new() { String = value }, value, options);64        public Task ToHaveAttributeAsync(string name, Regex value, LocatorAssertionsToHaveAttributeOptions options = null) =>65            ToHaveAttributeAsync(name, ExpectedRegex(value), value, options);66        private Task ToHaveAttributeAsync(string name, ExpectedTextValue expectedText, object expectedValue, LocatorAssertionsToHaveAttributeOptions options = null)67        {68            var commonOptions = ConvertToFrameExpectOptions(options);69            commonOptions.ExpressionArg = name;70            string message = $"Locator expected to have attribute '{name}'";71            if (expectedValue is Regex)72            {73                message += " matching regex";74            }...LocatorAssertionsToContainTextOptions.cs
Source:LocatorAssertionsToContainTextOptions.cs  
...35using System.Threading.Tasks;36#nullable enable37namespace Microsoft.Playwright38{39    public class LocatorAssertionsToContainTextOptions40    {41        public LocatorAssertionsToContainTextOptions() { }42        public LocatorAssertionsToContainTextOptions(LocatorAssertionsToContainTextOptions clone)43        {44            if (clone == null)45            {46                return;47            }48            Timeout = clone.Timeout;49            UseInnerText = clone.UseInnerText;50        }51        /// <summary><para>Time to retry the assertion for.</para></summary>52        [JsonPropertyName("timeout")]53        public float? Timeout { get; set; }54        /// <summary>55        /// <para>56        /// Whether to use <c>element.innerText</c> instead of <c>element.textContent</c> when...LocatorAssertionsToContainTextOptions
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var context = await browser.NewContextAsync();4var page = await context.NewPageAsync();5await page.ClickAsync("text=Docs");6await page.ClickAsync("text=API Reference");7await page.ClickAsync("text=LocatorAssertionsToContainTextOptions");8await page.ScreenshotAsync(new PageScreenshotOptions { Path = @"C:\Users\user\Desktop\Playwright\screenshots\2.png" });9await browser.CloseAsync();10var playwright = await Playwright.CreateAsync();11var browser = await playwright.Chromium.LaunchAsync();12var context = await browser.NewContextAsync();13var page = await context.NewPageAsync();14await page.ClickAsync("text=Docs");15await page.ClickAsync("text=API Reference");16await page.ClickAsync("text=LocatorAssertionsToContainTextOptions");17await page.ScreenshotAsync(new PageScreenshotOptions { Path = @"C:\Users\user\Desktop\Playwright\screenshots\3.png" });18await browser.CloseAsync();19var playwright = await Playwright.CreateAsync();20var browser = await playwright.Chromium.LaunchAsync();21var context = await browser.NewContextAsync();22var page = await context.NewPageAsync();23await page.ClickAsync("text=Docs");24await page.ClickAsync("text=API Reference");25await page.ClickAsync("text=LocatorAssertionsToContainTextOptions");26await page.ScreenshotAsync(new PageScreenshotOptions { Path = @"C:\Users\user\Desktop\Playwright\screenshots\4.png" });27await browser.CloseAsync();28var playwright = await Playwright.CreateAsync();29var browser = await playwright.Chromium.LaunchAsync();30var context = await browser.NewContextAsync();31var page = await context.NewPageAsync();32await page.ClickAsync("text=Docs");33await page.ClickAsync("text=API Reference");LocatorAssertionsToContainTextOptions
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Assertions;3using System.Threading.Tasks;4{5    static async Task Main(string[] args)6    {7        using var playwright = await Playwright.CreateAsync();8        await using var browser = await playwright.Chromium.LaunchAsync();9        var page = await browser.NewPageAsync();10        {11        };12        await page.WaitForSelectorAsync("input[title='Search']");13        await page.FillAsync("input[title='Search']", "playwright");14        await page.PressAsync("input[title='Search']", "Enter");15        await page.WaitForSelectorAsync("text=Playwright");16        await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");17        await page.AssertSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API", locatorAssertionsToContainTextOptions);18        await browser.CloseAsync();19    }20}LocatorAssertionsToContainTextOptions
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using Microsoft.Playwright.Helpers;4using Microsoft.Playwright.Transport;5using Microsoft.Playwright.Transport.Channels;6using Microsoft.Playwright.Transport.Protocol;7using System;8using System.Collections.Generic;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12{13    {14        static async Task Main(string[] args)15        {16            using var playwright = await Playwright.CreateAsync();17            var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });18            var page = await browser.NewPageAsync();19            await page.ClickAsync("text=Sign in");20            await page.ClickAsync("text=Create account");21            await page.FillAsync("input[name=\"firstName\"]", "John");22            await page.FillAsync("input[name=\"lastName\"]", "Doe");23            await page.FillAsync("input[name=\"Username\"]", "johndoe");24            await page.FillAsync("input[name=\"Passwd\"]", "johndoe");25            await page.FillAsync("input[name=\"ConfirmPasswd\"]", "johndoe");26            await page.ClickAsync("text=Next");27            await page.ClickAsync("text=I agreLocatorAssertionsToContainTextOptions
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using Microsoft.Playwright.Helpers;4using Microsoft.Playwright.Transport;5using Microsoft.Playwright.Transport.Channels;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12    {13        static async Task Main(string[] args)14        {15            using var playwright = await Playwright.CreateAsync();16            var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions17            {18            });19            var page = await browser.NewPageAsync();20            var locatorAssertionsToContainTextOptions = new LocatorAssertionsToContainTextOptions();21            locatorAssertionsToContainTextOptions.State = LocatorState.Attached;22            locatorAssertionsToContainTextOptions.Timeout = 10000;23            await page.WaitForSelectorAsync("input[name=\"q\"]", locatorAssertionsToContainTextOptions);24            await page.TypeAsync("input[name=\"q\"]", "Hello World");25            await page.PressAsync("input[name=\"q\"]", "Enter");26            await page.WaitForSelectorAsync("div#search", locatorAssertionsToContainTextOptions);27            await page.ScreenshotAsync("screenshot.png");28            await browser.CloseAsync();29        }30    }31}LocatorAssertionsToContainTextOptions
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            await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions{ Headless = false});10            var page = await browser.NewPageAsync();11            await page.FillAsync("input[name='q']", "Playwright");12            await page.ClickAsync("input[name='btnK']");13            await page.WaitForSelectorAsync("text=Playwright", new LocatorAssertionsToContainTextOptions{ State = State.Visible});14            await page.ScreenshotAsync("google-playwright.png");15        }16    }17}18using Microsoft.Playwright;19using System;20using System.Threading.Tasks;21{22    {23        static async Task Main(string[] args)24        {25            using var playwright = await Playwright.CreateAsync();26            await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions{ Headless = false});27            var page = await browser.NewPageAsync();28            await page.FillAsync("input[name='q']", "Playwright");29            await page.ClickAsync("input[name='btnK']");30            await page.WaitForSelectorAsync("text=Playwright", new LocatorAssertionsToContainTextOptions{ State = State.Visible});31            await page.ScreenshotAsync("google-playwright.png");32        }33    }34}35using Microsoft.Playwright;36using System;37using System.Threading.Tasks;38{39    {40        static async Task Main(string[] args)41        {42            using var playwright = await Playwright.CreateAsync();43            await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions{ Headless = false});44            var page = await browser.NewPageAsync();45            await page.FillAsync("input[name='q']", "Playwright");46            await page.ClickAsync("input[name='btnK']");47            await page.WaitForSelectorAsync("text=Playwright",LocatorAssertionsToContainTextOptions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Threading.Tasks;4using Microsoft.Playwright;5{6    {7        static async Task Main(string[] args)8        {9            Console.WriteLine("Hello World!");10            using var playwright = await Playwright.CreateAsync();11            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions12            {13            });14            var page = await browser.NewPageAsync();15            await page.TypeAsync("input[aria-label=\"Search\"]", "playwright");16            await page.PressAsync("input[aria-label=\"Search\"]", "Enter");17            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");18            await page.ClickAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");19            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");20            await page.ClickAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");21            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");22            await page.ClickAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");23            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");24            await page.ClickAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");25            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");26            await page.ClickAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");27            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");28            await page.ClickAsync("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API");29            await page.WaitForSelectorAsync("text=Playwright is a Node library to automate Chromium, Firefox andLocatorAssertionsToContainTextOptions
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Helpers;3using Microsoft.Playwright.Tests.Helpers;4using System;5using System.Collections.Generic;6using System.Text;7using System.Threading.Tasks;8{9    {10        [PlaywrightTest("2.cs", "should work")]11        public async Task ShouldWork()12        {13            using var playwright = await Playwright.CreateAsync();14            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });15            var page = await browser.NewPageAsync();16            await page.ClickAsync("text=I'm Feeling Lucky");17        }18    }19}20using Microsoft.Playwright;21using Microsoft.Playwright.Helpers;22using Microsoft.Playwright.Tests.Helpers;23using System;24using System.Collections.Generic;25using System.Text;26using System.Threading.Tasks;27{28    {29        [PlaywrightTest("3.cs", "should work")]30        public async Task ShouldWork()31        {32            using var playwright = await Playwright.CreateAsync();33            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });34            var page = await browser.NewPageAsync();35            await page.ClickAsync("text=I'm Feeling Lucky");36        }37    }38}39using Microsoft.Playwright;40using Microsoft.Playwright.Helpers;41using Microsoft.Playwright.Tests.Helpers;42using System;43using System.Collections.Generic;44using System.Text;45using System.Threading.Tasks;46{47    {48        [PlaywrightTest("4.cs", "should work")]49        public async Task ShouldWork()50        {51            using var playwright = await Playwright.CreateAsync();52            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });53            var page = await browser.NewPageAsync();54            await page.ClickAsync("text=I'm Feeling Lucky");55        }56    }57}LocatorAssertionsToContainTextOptions
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.NUnit;3using NUnit.Framework;4using System.Threading.Tasks;5{6    {7        private IPage page;8        public async Task Setup()9        {10            var playwright = await Playwright.CreateAsync();11            var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions12            {13            });14            page = await browser.NewPageAsync();15        }16        public async Task Test1()17        {18            await page.ClickAsync("text=Sign in");19            await page.ClickAsync("input[name=\"LocatorAssertionsToContainTextOptions
Using AI Code Generation
1using Microsoft.Playwright;2var locatorAssertionsToContainTextOptions = new LocatorAssertionsToContainTextOptions {3};4var locator = page.Locator("text=locator");5await locator.ShouldHaveTextAsync("locator", locatorAssertionsToContainTextOptions);6using Microsoft.Playwright;7var locatorAssertionsToHaveAttributeOptions = new LocatorAssertionsToHaveAttributeOptions {8};9var locator = page.Locator("text=locator");10await locator.ShouldHaveAttributeAsync("attribute", "value", locatorAssertionsToHaveAttributeOptions);11using Microsoft.Playwright;12var locatorAssertionsToHaveClassOptions = new LocatorAssertionsToHaveClassOptions {13};14var locator = page.Locator("text=locator");15await locator.ShouldHaveClassAsync("class", locatorAssertionsToHaveClassOptions);16using Microsoft.Playwright;17var locatorAssertionsToHaveCssValueOptions = new LocatorAssertionsToHaveCssValueOptions {18};19var locator = page.Locator("text=locator");20await locator.ShouldHaveCssValueAsync("css", "value", locatorAssertionsToHaveCssValueOptions);21using Microsoft.Playwright;22var locatorAssertionsToHaveFocusOptions = new LocatorAssertionsToHaveFocusOptions {23};24var locator = page.Locator("text=locator");25await locator.ShouldHaveFocusAsync(locatorAssertionsToHaveFocusOptions);26using Microsoft.Playwright;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!!
