How to use LocatorAssertions class of Microsoft.Playwright.Core package

Best Playwright-dotnet code snippet using Microsoft.Playwright.Core.LocatorAssertions

LocatorAssertions.cs

Source:LocatorAssertions.cs Github

copy

Full Screen

...27using System.Threading.Tasks;28using Microsoft.Playwright.Transport.Protocol;29namespace Microsoft.Playwright.Core30{31 internal class LocatorAssertions : AssertionsBase, ILocatorAssertions32 {33 public LocatorAssertions(ILocator locator, bool isNot) : base(locator, isNot)34 {35 }36 public ILocatorAssertions Not => new LocatorAssertions(ActualLocator, !IsNot);37 public Task ToBeCheckedAsync(LocatorAssertionsToBeCheckedOptions options = null)38 {39 var isChecked = options == null || options.Checked == null || options.Checked == true;40 return ExpectTrueAsync(isChecked ? "to.be.checked" : "to.be.unchecked", $"Locator expected {(!isChecked ? "not " : string.Empty)}to be checked", ConvertToFrameExpectOptions(options));41 }42 public Task ToBeDisabledAsync(LocatorAssertionsToBeDisabledOptions options = null) => ExpectTrueAsync("to.be.disabled", "Locator expected to be disabled", ConvertToFrameExpectOptions(options));43 public Task ToBeEditableAsync(LocatorAssertionsToBeEditableOptions options = null) => ExpectTrueAsync("to.be.editable", "Locator expected to be editable", ConvertToFrameExpectOptions(options));44 public Task ToBeEmptyAsync(LocatorAssertionsToBeEmptyOptions options = null) => ExpectTrueAsync("to.be.empty", "Locator expected to be empty", ConvertToFrameExpectOptions(options));45 public Task ToBeEnabledAsync(LocatorAssertionsToBeEnabledOptions options = null) => ExpectTrueAsync("to.be.enabled", "Locator expected to be enabled", ConvertToFrameExpectOptions(options));46 public Task ToBeFocusedAsync(LocatorAssertionsToBeFocusedOptions options = null) => ExpectTrueAsync("to.be.focused", "Locator expected to be focused", ConvertToFrameExpectOptions(options));47 public Task ToBeHiddenAsync(LocatorAssertionsToBeHiddenOptions options = null) => ExpectTrueAsync("to.be.hidden", "Locator expected to be hidden", ConvertToFrameExpectOptions(options));48 public Task ToBeVisibleAsync(LocatorAssertionsToBeVisibleOptions options = null) => ExpectTrueAsync("to.be.visible", "Locator expected to be visible", ConvertToFrameExpectOptions(options));49 private Task ExpectTrueAsync(string expression, string message, FrameExpectOptions options)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 }75 return ExpectImplAsync("to.have.attribute", expectedText, expectedValue, message, commonOptions);76 }77 public Task ToHaveClassAsync(string expected, LocatorAssertionsToHaveClassOptions options = null) =>78 ExpectImplAsync("to.have.class", new ExpectedTextValue() { String = expected }, expected, "Locator expected to have class", ConvertToFrameExpectOptions(options));79 public Task ToHaveClassAsync(Regex expected, LocatorAssertionsToHaveClassOptions options = null) =>80 ExpectImplAsync("to.have.class", ExpectedRegex(expected), expected, "Locator expected matching regex", ConvertToFrameExpectOptions(options));81 public Task ToHaveClassAsync(IEnumerable<string> expected, LocatorAssertionsToHaveClassOptions options = null) =>82 ExpectImplAsync("to.have.class.array", expected.Select(text => new ExpectedTextValue() { String = text }).ToArray(), expected, "Locator expected to have class", ConvertToFrameExpectOptions(options));83 public Task ToHaveClassAsync(IEnumerable<Regex> expected, LocatorAssertionsToHaveClassOptions options = null) =>84 ExpectImplAsync("to.have.class.array", expected.Select(regex => ExpectedRegex(regex)).ToArray(), expected, "Locator expected to have class matching regex", ConvertToFrameExpectOptions(options));85 public Task ToHaveCountAsync(int count, LocatorAssertionsToHaveCountOptions options = null)86 {87 ExpectedTextValue[] expectedText = null;88 var commonOptions = ConvertToFrameExpectOptions(options);89 commonOptions.ExpectedNumber = count;90 return ExpectImplAsync("to.have.count", expectedText, count, "Locator expected to have count", commonOptions);91 }92 public Task ToHaveCSSAsync(string name, string value, LocatorAssertionsToHaveCSSOptions options = null) =>93 ToHaveCSSAsync(name, new ExpectedTextValue() { String = value }, value, options);94 public Task ToHaveCSSAsync(string name, Regex value, LocatorAssertionsToHaveCSSOptions options = null) =>95 ToHaveCSSAsync(name, ExpectedRegex(value), value, options);96 internal Task ToHaveCSSAsync(string name, ExpectedTextValue expectedText, object expectedValue, LocatorAssertionsToHaveCSSOptions options = null)97 {98 var commonOptions = ConvertToFrameExpectOptions(options);99 commonOptions.ExpressionArg = name;100 var message = $"Locator expected to have CSS property '{name}'";101 if (expectedValue is Regex)102 {103 message += " matching regex";104 }105 return ExpectImplAsync("to.have.css", expectedText, expectedValue, message, commonOptions);106 }107 public Task ToHaveIdAsync(string id, LocatorAssertionsToHaveIdOptions options = null) =>108 ExpectImplAsync("to.have.id", new ExpectedTextValue() { String = id }, id, "Locator expected to have ID", ConvertToFrameExpectOptions(options));109 public Task ToHaveIdAsync(Regex id, LocatorAssertionsToHaveIdOptions options = null) =>110 ExpectImplAsync("to.have.id", ExpectedRegex(id), id, "Locator expected to have ID", ConvertToFrameExpectOptions(options));111 public Task ToHaveJSPropertyAsync(string name, object value, LocatorAssertionsToHaveJSPropertyOptions options = null)112 {113 var commonOptions = ConvertToFrameExpectOptions(options);114 commonOptions.ExpressionArg = name;115 commonOptions.ExpectedValue = ScriptsHelper.SerializedArgument(value);116 ExpectedTextValue[] expectedText = null;117 return ExpectImplAsync("to.have.property", expectedText, value, $"Locator expected to have JavaScript property '{name}'", commonOptions);118 }119 public Task ToHaveTextAsync(string expected, LocatorAssertionsToHaveTextOptions options = null) =>120 ExpectImplAsync("to.have.text", new ExpectedTextValue() { String = expected, NormalizeWhiteSpace = true }, expected, "Locator expected to have text", ConvertToFrameExpectOptions(options));121 public Task ToHaveTextAsync(Regex expected, LocatorAssertionsToHaveTextOptions options = null) =>122 ExpectImplAsync("to.have.text", ExpectedRegex(expected, new() { NormalizeWhiteSpace = true }), expected, "Locator expected to have text matching regex", ConvertToFrameExpectOptions(options));123 public Task ToHaveTextAsync(IEnumerable<string> expected, LocatorAssertionsToHaveTextOptions options = null) =>124 ExpectImplAsync("to.have.text.array", expected.Select(text => new ExpectedTextValue() { String = text, NormalizeWhiteSpace = true }).ToArray(), expected, "Locator expected to have text", ConvertToFrameExpectOptions(options));125 public Task ToHaveTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToHaveTextOptions options = null) =>126 ExpectImplAsync("to.have.text.array", expected.Select(regex => ExpectedRegex(regex, new() { NormalizeWhiteSpace = true })).ToArray(), expected, "Locator expected to have text", ConvertToFrameExpectOptions(options));127 public Task ToHaveValueAsync(string value, LocatorAssertionsToHaveValueOptions options = null) =>128 ExpectImplAsync("to.have.value", new ExpectedTextValue() { String = value }, value, "Locator expected to have value", ConvertToFrameExpectOptions(options));129 public Task ToHaveValueAsync(Regex value, LocatorAssertionsToHaveValueOptions options = null) =>130 ExpectImplAsync("to.have.value", ExpectedRegex(value), value, "Locator expected to have value matching regex", ConvertToFrameExpectOptions(options));131 }132}...

Full Screen

Full Screen

Assertions.cs

Source:Assertions.cs Github

copy

Full Screen

2namespace Microsoft.Playwright3{4 public static class Assertions5 {6 public static ILocatorAssertions Expect(ILocator locator) => new LocatorAssertions(locator, false);7 public static IPageAssertions Expect(IPage page) => new PageAssertions(page, false);8 }9}...

Full Screen

Full Screen

LocatorAssertions

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2var playwright = await Playwright.CreateAsync();3var browser = await playwright.Chromium.LaunchAsync();4var context = await browser.NewContextAsync();5var page = await context.NewPageAsync();6var locator = page.Locator("input[title='Search']");7await locator.AssertCountAsync(1);8await locator.AssertCountAsync(2);9await page.CloseAsync();10await context.CloseAsync();11await browser.CloseAsync();12await playwright.DisposeAsync();

Full Screen

Full Screen

LocatorAssertions

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using Microsoft.Playwright.Helpers;4using Microsoft.Playwright.NUnit;5using NUnit.Framework;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12 {13 private IPlaywright _playwright;14 private IBrowser _browser;15 private IPage _page;16 private LocatorAssertions _locatorAssertions;17 public async Task Setup()18 {19 _playwright = await Playwright.CreateAsync();20 _browser = await _playwright.Chromium.LaunchAsync(new LaunchOptions21 {22 });23 _page = await _browser.NewPageAsync();24 _locatorAssertions = new LocatorAssertions(_page);25 }26 public async Task Test1()27 {

Full Screen

Full Screen

LocatorAssertions

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using Microsoft.Playwright.Core.Helpers;4using Microsoft.Playwright.Core.Tests.Helpers;5using Microsoft.Playwright.NUnit;6using NUnit.Framework;7using System;8using System.Collections.Generic;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12{13 {14 private static readonly LocatorAssertions<Page> LocatorAssertions = new LocatorAssertions<Page>();15 private static readonly LocatorAssertions<Frame> LocatorFrameAssertions = new LocatorAssertions<Frame>();16 public async Task LocatorShouldWaitForVisible()17 {18 await using var playwright = await Playwright.CreateAsync();19 await using var browser = await playwright.Chromium.LaunchAsync();20 var page = await browser.NewPageAsync();21 await page.GotoAsync(Server.EmptyPage);22 var locator = page.Locator("div");23 var task = locator.WaitForElementStateAsync(ElementState.Visible, new() { Timeout = 500 });24 await page.EvaluateAsync("() => window.setTimeout(() => document.querySelector('div').style.display = 'block', 200)");25 await task;26 }27 public async Task LocatorShouldWaitForHidden()28 {29 await using var playwright = await Playwright.CreateAsync();30 await using var browser = await playwright.Chromium.LaunchAsync();31 var page = await browser.NewPageAsync();32 await page.GotoAsync(Server.EmptyPage);33 var locator = page.Locator("div");34 var task = locator.WaitForElementStateAsync(ElementState.Hidden, new() { Timeout = 500 });35 await page.EvaluateAsync("() => window.setTimeout(() => document.querySelector('div').style.display = 'none', 200)");36 await task;37 }38 public async Task LocatorShouldWaitForStablePosition()39 {40 await using var playwright = await Playwright.CreateAsync();41 await using var browser = await playwright.Chromium.LaunchAsync();42 var page = await browser.NewPageAsync();43 await page.GotoAsync(Server.EmptyPage);44 var locator = page.Locator("div");45 var task = locator.WaitForElementStateAsync(ElementState.Stable, new() { Timeout = 500 });46 await page.EvaluateAsync("() => window.setTimeout(() => document.querySelector('div').style.transform = 'translateX(100px)', 200)");47 await task;48 }

Full Screen

Full Screen

LocatorAssertions

Using AI Code Generation

copy

Full Screen

1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using System;4using System.Collections.Generic;5using System.Text;6{7 {8 private readonly Locator _locator;9 public LocatorAssertions(Locator locator)10 {11 _locator = locator;12 }13 public LocatorAssertions ToHaveCount(int expected)14 {15 var actual = _locator.CountAsync().Result;16 if (actual != expected)17 {18 throw new Exception($"Locator has {actual} elements, but expected {expected}.");19 }20 return this;21 }22 public LocatorAssertions ToHaveText(string expected)23 {24 var actual = _locator.TextContentAsync().Result;25 if (actual != expected)26 {27 throw new Exception($"Locator has text {actual}, but expected {expected}.");28 }29 return this;30 }31 }32}33using Microsoft.Playwright;34using Microsoft.Playwright.Core;35using System;36using System.Collections.Generic;37using System.Text;38{39 {40 public static LocatorAssertions Should(this Locator locator)41 {42 return new LocatorAssertions(locator);43 }44 }45}46using Microsoft.Playwright;47using Microsoft.Playwright.Core;48using System;49using System.Collections.Generic;50using System.Text;51{52 {53 private readonly Locator _locator;54 public LocatorAssertions(Locator locator)55 {56 _locator = locator;57 }58 public LocatorAssertions ToHaveCount(int expected)59 {60 var actual = _locator.CountAsync().Result;61 if (actual != expected)62 {63 throw new Exception($"Locator has {actual} elements, but expected {expected}.");64 }65 return this;66 }67 public LocatorAssertions ToHaveText(string expected)68 {69 var actual = _locator.TextContentAsync().Result;70 if (actual != expected)71 {72 throw new Exception($"Locator has text {actual}, but expected {expected}.");73 }74 return this;75 }76 }77}78using Microsoft.Playwright;

Full Screen

Full Screen

Playwright tutorial

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.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful