Best Playwright-dotnet code snippet using Microsoft.Playwright.Assertions.Expect
LocatorAssertionsTests.cs
Source:LocatorAssertionsTests.cs
...33 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toBeChecked")]34 public async Task ShouldSupportToBeChecked()35 {36 await Page.SetContentAsync("<input type=checkbox checked></input>");37 await Expect(Page.Locator("input")).ToBeCheckedAsync();38 await Expect(Page.Locator("input")).ToBeCheckedAsync(new() { Checked = true });39 await Expect(Page.Locator("input")).Not.ToBeCheckedAsync(new() { Checked = false });40 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page.Locator("input")).ToBeCheckedAsync(new() { Checked = false, Timeout = 300 }));41 StringAssert.Contains("Locator expected not to be checked", exception.Message);42 StringAssert.Contains("LocatorAssertions.ToBeCheckedAsync with timeout 300ms", exception.Message);43 exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page.Locator("input")).Not.ToBeCheckedAsync(new() { Timeout = 300 }));44 StringAssert.Contains("Locator expected not to be checked", exception.Message);45 StringAssert.Contains("LocatorAssertions.ToBeCheckedAsync with timeout 300ms", exception.Message);46 }47 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toBeEditable, toBeEnabled, toBeDisabled, toBeEmpty")]48 public async Task ShouldSupportToBeEditableToBeEnabledToBeDisabledToBeEmpty()49 {50 {51 await Page.SetContentAsync("<input></input>");52 await Expect(Page.Locator("input")).ToBeEditableAsync();53 }54 {55 await Page.SetContentAsync("<button>Text</button>");56 await Expect(Page.Locator("button")).ToBeEnabledAsync();57 }58 {59 await Page.SetContentAsync("<button disabled>Text</button>");60 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page.Locator("button")).ToBeEnabledAsync(new() { Timeout = 500 }));61 StringAssert.Contains("Locator expected to be enabled", exception.Message);62 StringAssert.Contains("LocatorAssertions.ToBeEnabledAsync with timeout 500ms", exception.Message);63 }64 {65 await Page.SetContentAsync("<button disabled>Text</button>");66 var locator = Page.Locator("button");67 await locator.EvaluateAsync("e => setTimeout(() => { e.removeAttribute('disabled') }, 500);");68 await Expect(locator).ToBeEnabledAsync();69 }70 {71 await Page.SetContentAsync("<button>Text</button>");72 var locator = Page.Locator("button");73 await locator.EvaluateAsync("e => setTimeout(() => { e.setAttribute('disabled', '') }, 500);");74 await Expect(locator).Not.ToBeEnabledAsync();75 }76 {77 await Page.SetContentAsync("<button disabled>Text</button>");78 var locator = Page.Locator("button");79 await Expect(locator).ToBeDisabledAsync();80 }81 {82 await Page.SetContentAsync("<input></input>");83 var locator = Page.Locator("input");84 await Expect(locator).ToBeEmptyAsync();85 }86 {87 await Page.SetContentAsync("<input value=text></input>");88 var locator = Page.Locator("input");89 await Expect(locator).Not.ToBeEmptyAsync();90 }91 }92 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toBeVisible, toBeHidden")]93 public async Task ShouldSupportToBeVisibleToBeHidden()94 {95 {96 await Page.SetContentAsync("<input></input>");97 var locator = Page.Locator("input");98 await Expect(locator).ToBeVisibleAsync();99 }100 {101 await Page.SetContentAsync("<button style=\"display: none\"></button>");102 var locator = Page.Locator("button");103 await Expect(locator).Not.ToBeVisibleAsync();104 }105 {106 await Page.SetContentAsync("<button style=\"display: none\"></button>");107 var locator = Page.Locator("button");108 await Expect(locator).ToBeHiddenAsync();109 }110 {111 await Page.SetContentAsync("<div></div>");112 var locator = Page.Locator("div");113 await Expect(locator).ToBeHiddenAsync();114 }115 {116 await Page.SetContentAsync("<input></input>");117 var locator = Page.Locator("input");118 await Expect(locator).Not.ToBeHiddenAsync();119 }120 }121 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toBeVisible, toBeHidden fail")]122 public async Task ShouldSupportToBeVisibleToBeHiddenFail()123 {124 {125 await Page.SetContentAsync("<button style=\"display: none\"></button>");126 var locator = Page.Locator("button");127 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(locator).ToBeVisibleAsync(new() { Timeout = 500 }));128 StringAssert.Contains("Locator expected to be visible", exception.Message);129 StringAssert.Contains("LocatorAssertions.ToBeVisibleAsync with timeout 500ms", exception.Message);130 }131 {132 await Page.SetContentAsync("<input></input>");133 var locator = Page.Locator("input");134 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(locator).Not.ToBeVisibleAsync(new() { Timeout = 500 }));135 StringAssert.Contains("Locator expected not to be visible", exception.Message);136 StringAssert.Contains("LocatorAssertions.ToBeVisibleAsync with timeout 500ms", exception.Message);137 }138 }139 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toBeFocused")]140 public async Task ShouldSupportToBeFocused()141 {142 await Page.SetContentAsync("<input></input>");143 var locator = Page.Locator("input");144 await locator.FocusAsync();145 await Expect(locator).ToBeFocusedAsync();146 }147 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toContainText")]148 public async Task ShouldSupportToContainText()149 {150 {151 await Page.SetContentAsync("<div id=node>Text content</div>");152 await Expect(Page.Locator("#node")).ToContainTextAsync(new Regex("ex"));153 // Should not normalize whitespace.154 await Expect(Page.Locator("#node")).ToContainTextAsync(new Regex("ext cont"));155 }156 {157 await Page.SetContentAsync("<div id=node>Text content</div>");158 var exeption = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page.Locator("#node")).ToContainTextAsync(new Regex("ex2"), new() { Timeout = 100 }));159 StringAssert.Contains("Locator expected text matching regex 'ex2'", exeption.Message);160 StringAssert.Contains("But was: 'Text content'", exeption.Message);161 StringAssert.Contains("LocatorAssertions.ToContainTextAsync with timeout 100ms", exeption.Message);162 }163 {164 await Page.SetContentAsync("<div id=node><span></span>Text \ncontent </div>");165 var locator = Page.Locator("#node");166 // Should normalize whitespace.167 await Expect(locator).ToHaveTextAsync("Text content");168 // Should normalize zero width whitespace.169 await Expect(locator).ToHaveTextAsync("T\u200be\u200bx\u200bt content");170 await Expect(locator).ToHaveTextAsync(new Regex("Text\\s+content"));171 }172 {173 await Page.SetContentAsync("<div id=node>Text content</div>");174 var locator = Page.Locator("#node");175 await Expect(locator).ToContainTextAsync("Text");176 // Should normalize whitespace.177 await Expect(locator).ToContainTextAsync(" ext cont\n ");178 }179 {180 await Page.SetContentAsync("<div id=node>Text content</div>");181 var locator = Page.Locator("#node");182 var exeption = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(locator).ToHaveTextAsync("Text", new() { Timeout = 100 }));183 StringAssert.Contains("Locator expected to have text 'Text'", exeption.Message);184 StringAssert.Contains("But was: 'Text content'", exeption.Message);185 StringAssert.Contains("LocatorAssertions.ToHaveTextAsync with timeout 100ms", exeption.Message);186 }187 }188 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toContainText w/ array")]189 public async Task ShouldSupportToContainTextWithArray()190 {191 await Page.SetContentAsync("<div>Text \n1</div><div>Text2</div><div>Text3</div>");192 var locator = Page.Locator("div");193 await Expect(locator).ToContainTextAsync(new string[] { "ext 1", "ext3" });194 await Expect(locator).ToContainTextAsync(new Regex[] { new Regex("ext \\s+1"), new Regex("ext3") });195 }196 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toHaveText w/ array")]197 public async Task ShouldSupportToHaveTextWithArray()198 {199 await Page.SetContentAsync("<div>Text \n1</div><div>Text 2a</div>");200 var locator = Page.Locator("div");201 // Should normalize whitespace.202 await Expect(locator).ToHaveTextAsync(new string[] { "Text 1", "Text 2a" });203 // But not for Regex.204 await Expect(locator).ToHaveTextAsync(new Regex[] { new Regex("Text \\s+1"), new Regex("Text \\d+a") });205 }206 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toHaveAttribute")]207 public async Task ShouldSupportToHaveAttribute()208 {209 await Page.SetContentAsync("<div id=node>Text content</div>");210 var locator = Page.Locator("#node");211 await Expect(locator).ToHaveAttributeAsync("id", "node");212 await Expect(locator).ToHaveAttributeAsync("id", new Regex("node"));213 }214 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toHaveCSS")]215 public async Task ShouldSupportToHaveCSS()216 {217 await Page.SetContentAsync("<div id=node style=\"color: rgb(255, 0, 0)\">Text content</div>");218 var locator = Page.Locator("#node");219 await Expect(locator).ToHaveCSSAsync("color", "rgb(255, 0, 0)");220 await Expect(locator).ToHaveCSSAsync("color", new Regex("rgb\\(\\d+, 0, 0\\)"));221 }222 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toHaveClass")]223 public async Task ShouldSupportToHaveClass()224 {225 {226 await Page.SetContentAsync("<div class=\"foo bar baz\"></div>");227 var locator = Page.Locator("div");228 await Expect(locator).ToHaveClassAsync("foo bar baz");229 await Expect(locator).ToHaveClassAsync(new Regex("foo bar baz"));230 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(locator).ToHaveClassAsync("kektus", new() { Timeout = 300 }));231 StringAssert.Contains("Locator expected to have class 'kektus'", exception.Message);232 StringAssert.Contains("But was: 'foo bar baz'", exception.Message);233 StringAssert.Contains("LocatorAssertions.ToHaveClassAsync with timeout 300ms", exception.Message);234 }235 {236 await Page.SetContentAsync("<div class=\"foo\"></div><div class=\"bar\"></div><div class=\"baz\"></div>");237 var locator = Page.Locator("div");238 await Expect(locator).ToHaveClassAsync(new string[] { "foo", "bar", "baz" });239 await Expect(locator).ToHaveClassAsync(new Regex[] { new("^f.o$"), new("^b.r$"), new("^[a-z]az$") });240 }241 }242 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toHaveCount")]243 public async Task ShouldSupportToHaveCount()244 {245 await Page.SetContentAsync("<select><option>One</option></select>");246 var locator = Page.Locator("option");247 await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(locator).ToHaveCountAsync(2, new() { Timeout = 300 }));248 await Page.SetContentAsync("<select><option>One</option><option>Two</option></select>");249 await Expect(locator).ToHaveCountAsync(2);250 }251 [PlaywrightTest("playwright-test/playwright.expect.spec.ts", "should support toHaveId")]252 public async Task ShouldSupportToHaveId()253 {254 await Page.SetContentAsync("<div id=node>Text content</div>");255 var locator = Page.Locator("#node");256 await Expect(locator).ToHaveIdAsync("node");257 await Expect(locator).ToHaveIdAsync(new Regex("n.de"));258 }259 [PlaywrightTest("playwright-test/playwright.expect.misc.spec.ts", "should support toHaveJSProperty")]260 public async Task ShouldSupportToHaveJSProperty()261 {262 await Page.SetContentAsync("<div></div>");263 await Page.EvalOnSelectorAsync("div", "e => e.foo = { a: 1, b: 'string', c: new Date(1627503992000) }");264 var locator = Page.Locator("div");265 await Expect(locator).ToHaveJSPropertyAsync("foo", new Dictionary<string, object>266 {267 ["a"] = 1,268 ["b"] = "string",269 ["c"] = DateTime.Parse("2021-07-28T20:26:32.000Z"),270 });271 }272 [PlaywrightTest("playwright-test/playwright.expect.misc.spec.ts", "should support toHaveValue")]273 public async Task ShouldSupportToHaveValue()274 {275 {276 await Page.SetContentAsync("<input id=node></input>");277 var locator = Page.Locator("#node");278 await locator.FillAsync("Text content");279 await Expect(locator).ToHaveValueAsync("Text content");280 await Expect(locator).ToHaveValueAsync(new Regex("Text( |)content"));281 }282 {283 await Page.SetContentAsync("<label><input></input></label>");284 await Page.Locator("label input").FillAsync("Text content");285 await Expect(Page.Locator("label")).ToHaveValueAsync("Text content");286 }287 }288 }289}...
AssertionsBase.cs
Source:AssertionsBase.cs
...38 IsNot = isNot;39 }40 protected bool IsNot { get; private set; }41 internal Locator ActualLocator { get; private set; }42 internal async Task ExpectImplAsync(string expression, ExpectedTextValue textValue, object expected, string message, FrameExpectOptions options)43 {44 await ExpectImplAsync(expression, new ExpectedTextValue[] { textValue }, expected, message, options).ConfigureAwait(false);45 }46 internal async Task ExpectImplAsync(string expression, ExpectedTextValue[] expectedText, object expected, string message, FrameExpectOptions options)47 {48 options = options ?? new();49 options.ExpectedText = expectedText;50 options.IsNot = IsNot;51 await ExpectImplAsync(expression, options, expected, message).ConfigureAwait(false);52 }53 internal async Task ExpectImplAsync(string expression, FrameExpectOptions expectOptions, object expected, string message)54 {55 if (expectOptions.Timeout == null)56 {57 expectOptions.Timeout = 5_000;58 }59 if (expectOptions.IsNot)60 {61 message = message.Replace("expected to", "expected not to");62 }63 var result = await ActualLocator.ExpectAsync(expression, expectOptions).ConfigureAwait(false);64 if (result.Matches == IsNot)65 {66 var actual = result.Received;67 var log = string.Join("\n", result.Log);68 if (!string.IsNullOrEmpty(log))69 {70 log = "\nCall log:\n" + log;71 }72 if (expected == null)73 {74 throw new PlaywrightException($"{message} {log}");75 }76 throw new PlaywrightException($"{message} '{FormatValue(expected)}'\nBut was: '{FormatValue(actual)}' {log}");77 }78 }79 internal ExpectedTextValue ExpectedRegex(Regex pattern, ExpectedTextValue options = null)80 {81 if (pattern == null)82 {83 throw new ArgumentNullException(nameof(pattern));84 }85 ExpectedTextValue textValue = options ?? new() { };86 textValue.RegexSource = pattern.ToString();87 textValue.RegexFlags = pattern.Options.GetInlineFlags();88 return textValue;89 }90 internal T ConvertToType<T>(object source)91 where T : new()92 {93 T target = new();94 if (source == null)95 {96 return target;97 }98 var sourceType = source.GetType();99 var targetType = target.GetType();100 foreach (var sourceProperty in sourceType.GetProperties())101 {102 var targetProperty = targetType.GetProperty(sourceProperty.Name);103 if (targetProperty != null)104 {105 targetProperty.SetValue(target, sourceProperty.GetValue(source));106 }107 }108 return target;109 }110 internal FrameExpectOptions ConvertToFrameExpectOptions(object source) => ConvertToType<FrameExpectOptions>(source);111 private string FormatValue(object value)112 {113 if (value == null)114 {115 return "null";116 }117 if (value is string)118 {119 return (string)value;120 }121 if (value is IEnumerable<object>)122 {123 return "[" + string.Join(", ", ((IEnumerable<object>)value).Select(value => $"'{value}'")) + "]";124 }...
IPageAssertions.cs
Source:IPageAssertions.cs
...39 /// <summary>40 /// <para>41 /// The <see cref="IPageAssertions"/> class provides assertion methods that can be used42 /// to make assertions about the <see cref="IPage"/> state in the tests. A new instance43 /// of <see cref="IPageAssertions"/> is created by calling <see cref="IPlaywrightAssertions.Expect"/>:44 /// </para>45 /// <code>46 /// using System.Text.RegularExpressions;<br/>47 /// using System.Threading.Tasks;<br/>48 /// using Microsoft.Playwright.NUnit;<br/>49 /// using NUnit.Framework;<br/>50 /// <br/>51 /// namespace PlaywrightTests<br/>52 /// {<br/>53 /// public class ExampleTests : PageTest<br/>54 /// {<br/>55 /// [Test]<br/>56 /// public async Task NavigatetoLoginPage()<br/>57 /// {<br/>58 /// // ..<br/>59 /// await Page.ClickAsync("#login");<br/>60 /// await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));<br/>61 /// }<br/>62 /// }<br/>63 /// }64 /// </code>65 /// </summary>66 public partial interface IPageAssertions67 {68 /// <summary>69 /// <para>70 /// Makes the assertion check for the opposite condition. For example, this code tests71 /// that the page URL doesn't contain <c>"error"</c>:72 /// </para>73 /// <code>await Expect(page).Not.ToHaveURL("error");</code>74 /// </summary>75 public IPageAssertions Not { get; }76 /// <summary>77 /// <para>Ensures the page has the given title.</para>78 /// <code>await Expect(page).ToHaveTitle("Playwright");</code>79 /// </summary>80 /// <param name="titleOrRegExp">Expected title or RegExp.</param>81 /// <param name="options">Call options</param>82 Task ToHaveTitleAsync(string titleOrRegExp, PageAssertionsToHaveTitleOptions? options = default);83 /// <summary>84 /// <para>Ensures the page has the given title.</para>85 /// <code>await Expect(page).ToHaveTitle("Playwright");</code>86 /// </summary>87 /// <param name="titleOrRegExp">Expected title or RegExp.</param>88 /// <param name="options">Call options</param>89 Task ToHaveTitleAsync(Regex titleOrRegExp, PageAssertionsToHaveTitleOptions? options = default);90 /// <summary>91 /// <para>Ensures the page is navigated to the given URL.</para>92 /// <code>await Expect(page).ToHaveURL(new Regex(".*checkout"));</code>93 /// </summary>94 /// <param name="urlOrRegExp">Expected substring or RegExp.</param>95 /// <param name="options">Call options</param>96 Task ToHaveURLAsync(string urlOrRegExp, PageAssertionsToHaveURLOptions? options = default);97 /// <summary>98 /// <para>Ensures the page is navigated to the given URL.</para>99 /// <code>await Expect(page).ToHaveURL(new Regex(".*checkout"));</code>100 /// </summary>101 /// <param name="urlOrRegExp">Expected substring or RegExp.</param>102 /// <param name="options">Call options</param>103 Task ToHaveURLAsync(Regex urlOrRegExp, PageAssertionsToHaveURLOptions? options = default);104 }105}106#nullable disable
PageAssertionsTests.cs
Source:PageAssertionsTests.cs
...31 [PlaywrightTest("playwright-test/playwright.expect.misc.spec.ts", "should support toHaveTitle")]32 public async Task ShouldSupportToHaveTitleAsync()33 {34 await Page.SetContentAsync("<title> Hello world</title>");35 await Expect(Page).ToHaveTitleAsync("Hello world");36 await Page.SetContentAsync("<title>Bye</title>");37 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page).ToHaveTitleAsync("Hello", new() { Timeout = 100 }));38 StringAssert.Contains("Page title expected to be 'Hello'", exception.Message);39 StringAssert.Contains("But was: 'Bye'", exception.Message);40 StringAssert.Contains("PageAssertions.ToHaveTitleAsync with timeout 100ms", exception.Message);41 await Page.SetContentAsync("<title>Foo Bar Kek</title>");42 await Expect(Page).ToHaveTitleAsync(new Regex("^Foo .* Kek$"));43 await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page).ToHaveTitleAsync(new Regex("nooo"), new() { Timeout = 100 }));44 await Page.SetContentAsync("<title>Bye</title>");45 await Expect(Page).Not.ToHaveTitleAsync("Hello");46 }47 [PlaywrightTest("playwright-test/playwright.expect.misc.spec.ts", "should support toHaveURL")]48 public async Task ShouldSupportToHaveURLAsync()49 {50 await Page.GotoAsync("data:text/html,<div>A</div>");51 await Expect(Page).ToHaveURLAsync("data:text/html,<div>A</div>");52 await Page.GotoAsync("data:text/html,<div>B</div>");53 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page).ToHaveURLAsync("wrong", new() { Timeout = 100 }));54 StringAssert.Contains("Page URL expected to be 'wrong'", exception.Message);55 StringAssert.Contains("But was: 'data:text/html,<div>B</div>'", exception.Message);56 StringAssert.Contains("PageAssertions.ToHaveURLAsync with timeout 100ms", exception.Message);57 await Page.GotoAsync(Server.EmptyPage);58 await Expect(Page).ToHaveURLAsync(new Regex(".*empty.html"));59 await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Expect(Page).ToHaveURLAsync(new Regex("nooo"), new() { Timeout = 100 }));60 await Page.GotoAsync(Server.EmptyPage);61 await Expect(Page).ToHaveURLAsync(Server.Prefix + "/empty.html");62 await Expect(Page).Not.ToHaveURLAsync(Server.Prefix + "/foobar.html");63 var page = await Browser.NewPageAsync(new() { BaseURL = Server.Prefix });64 try65 {66 await page.GotoAsync("/empty.html");67 await Expect(page).ToHaveURLAsync("/empty.html");68 await Expect(page).Not.ToHaveURLAsync("/kek.html");69 }70 finally71 {72 await page.CloseAsync();73 }74 }75 }76}...
IPlaywrightAssertions.cs
Source:IPlaywrightAssertions.cs
...53 /// {<br/>54 /// [Test]<br/>55 /// public async Task StatusBecomesSubmitted()<br/>56 /// {<br/>57 /// await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");<br/>58 /// }<br/>59 /// }<br/>60 /// }61 /// </code>62 /// <para>63 /// Playwright will be re-testing the node with the selector <c>.status</c> until fetched64 /// Node has the <c>"Submitted"</c> text. It will be re-fetching the node and checking65 /// it over and over, until the condition is met or until the timeout is reached. You66 /// can pass this timeout as an option.67 /// </para>68 /// <para>By default, the timeout for assertions is set to 5 seconds.</para>69 /// </summary>70 public partial interface IPlaywrightAssertions71 {72 /// <summary>73 /// <para>Creates a <see cref="ILocatorAssertions"/> object for the given <see cref="ILocator"/>.</para>74 /// <code>await Expect(locator).ToBeVisibleAsync();</code>75 /// </summary>76 /// <param name="locator"><see cref="ILocator"/> object to use for assertions.</param>77 ILocatorAssertions Expect(ILocator locator);78 /// <summary>79 /// <para>Creates a <see cref="IPageAssertions"/> object for the given <see cref="IPage"/>.</para>80 /// <code>await Expect(page).ToHaveTitleAsync("News");</code>81 /// </summary>82 /// <param name="page"><see cref="IPage"/> object to use for assertions.</param>83 IPageAssertions Expect(IPage page);84 }85}86#nullable disable...
PageAssertions.cs
Source:PageAssertions.cs
...43 }44 return value;45 }46 public Task ToHaveTitleAsync(string titleOrRegExp, PageAssertionsToHaveTitleOptions options = null) =>47 ExpectImplAsync("to.have.title", new ExpectedTextValue() { String = titleOrRegExp, NormalizeWhiteSpace = true }, titleOrRegExp, "Page title expected to be", ConvertToFrameExpectOptions(options));48 public Task ToHaveTitleAsync(Regex titleOrRegExp, PageAssertionsToHaveTitleOptions options = null) =>49 ExpectImplAsync("to.have.title", ExpectedRegex(titleOrRegExp, new() { NormalizeWhiteSpace = true }), titleOrRegExp, "Page title expected to be", ConvertToFrameExpectOptions(options));50 public Task ToHaveURLAsync(string urlOrRegExp, PageAssertionsToHaveURLOptions options = null) =>51 ExpectImplAsync("to.have.url", new ExpectedTextValue() { String = _page.Context.CombineUrlWithBase(urlOrRegExp) }, urlOrRegExp, "Page URL expected to be", ConvertToFrameExpectOptions(options));52 public Task ToHaveURLAsync(Regex urlOrRegExp, PageAssertionsToHaveURLOptions options = null) =>53 ExpectImplAsync("to.have.url", ExpectedRegex(urlOrRegExp), urlOrRegExp, "Page URL expected to match regex", ConvertToFrameExpectOptions(options));54 }55}...
PlaywrightTest.cs
Source:PlaywrightTest.cs
...38 Playwright = await _playwrightTask.ConfigureAwait(false);39 BrowserType = Playwright[BrowserName];40 Assert.IsNotNull(BrowserType, $"The requested browser ({BrowserName}) could not be found - make sure your BROWSER env variable is set correctly.");41 }42 public ILocatorAssertions Expect(ILocator locator) => Assertions.Expect(locator);43 public IPageAssertions Expect(IPage page) => Assertions.Expect(page);44 }45}...
Assertions.cs
Source:Assertions.cs
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}...
Expect
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Assertions;3using System;4using System.Threading.Tasks;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();10 var page = await browser.NewPageAsync();11 await page.ExpectAsync("input[name=q]");12 await page.FillAsync("input[name=q]", "Playwright");13 await page.PressAsync("input[name=q]", "Enter");14 await page.ScreenshotAsync("screenshot.png");15 }16}
Expect
Using AI Code Generation
1using Microsoft.Playwright;2var playwright = await Playwright.CreateAsync();3var browser = await playwright.Chromium.LaunchAsync();4var page = await browser.NewPageAsync();5await page.ExpectNavigationAsync(() => page.ClickAsync("a"));6await page.ExpectNavigationAsync(() => page.ClickAsync("button"));7await page.ExpectNavigationAsync(() => page.ClickAsync("input"));8await page.ExpectNavigationAsync(() => page.ClickAsync("img"));9await page.ExpectNavigationAsync(() => page.ClickAsync("link"));10await page.ExpectNavigationAsync(() => page.ClickAsync("ul"));11await page.ExpectNavigationAsync(() => page.ClickAsync("li"));12await page.ExpectNavigationAsync(() => page.ClickAsync("div"));13await page.ExpectNavigationAsync(() => page.ClickAsync("span"));14await page.ExpectNavigationAsync(() => page.ClickAsync("i"));15await page.ExpectNavigationAsync(() => page.ClickAsync("b"));16await page.ExpectNavigationAsync(() => page.ClickAsync("body"));17await page.ExpectNavigationAsync(() => page.ClickAsync("p"));18await page.ExpectNavigationAsync(() => page.ClickAsync("h1"));19await page.ExpectNavigationAsync(() => page.ClickAsync("h2"));20await page.ExpectNavigationAsync(() => page.ClickAsync("h3"));21await page.ExpectNavigationAsync(() => page.ClickAsync("h4"));22await page.ExpectNavigationAsync(() => page.ClickAsync("h5"));23await page.ExpectNavigationAsync(() => page.ClickAsync("h6"));24await page.ExpectNavigationAsync(() => page.ClickAsync("html"));25await page.ExpectNavigationAsync(() => page.ClickAsync("form"));26await page.ExpectNavigationAsync(() => page.ClickAsync("select"));27await page.ExpectNavigationAsync(() => page.ClickAsync("option"));28await page.ExpectNavigationAsync(() => page.ClickAsync("textarea"));29await page.ExpectNavigationAsync(() => page.ClickAsync("label"));30await page.ExpectNavigationAsync(() => page.ClickAsync("table"));31await page.ExpectNavigationAsync(() => page.ClickAsync("tr"));32await page.ExpectNavigationAsync(() => page.ClickAsync("th"));33await page.ExpectNavigationAsync(() => page.ClickAsync("td"));34await page.ExpectNavigationAsync(() => page.ClickAsync("thead"));35await page.ExpectNavigationAsync(() => page.ClickAsync("tbody"));36await page.ExpectNavigationAsync(() => page.ClickAsync("tfoot"));37await page.ExpectNavigationAsync(() => page.ClickAsync("caption"));38await page.ExpectNavigationAsync(() => page.ClickAsync("colgroup"));
Expect
Using AI Code Generation
1var page = await context.NewPageAsync();2await page.TypeAsync("input[title=\"Search\"]", "Hello World");3await page.PressAsync("input[title=\"Search\"]", "Enter");4await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);5var element = await page.QuerySelectorAsync("a");6await element.ClickAsync();7await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);8var element1 = await page.QuerySelectorAsync("a");9await element1.ClickAsync();10await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);11await page.ExpectNavigationAsync(() => page.ClickAsync("a"));12var element2 = await page.QuerySelectorAsync("a");13await element2.ClickAsync();14await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);15await page.ExpectNavigationAsync(() => page.ClickAsync("a"));16var element3 = await page.QuerySelectorAsync("a");17await element3.ClickAsync();18await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);19await page.ExpectNavigationAsync(() => page.ClickAsync("a"));20var element4 = await page.QuerySelectorAsync("a");21await element4.ClickAsync();22await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);23await page.ExpectNavigationAsync(() => page.ClickAsync("a"));24var element5 = await page.QuerySelectorAsync("a");25await element5.ClickAsync();26await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);27await page.ExpectNavigationAsync(() => page.ClickAsync("a"));28var element6 = await page.QuerySelectorAsync("a");29await element6.ClickAsync();30await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);31await page.ExpectNavigationAsync(() => page.ClickAsync("a"));32var element7 = await page.QuerySelectorAsync("a");33await element7.ClickAsync();34await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);35await page.ExpectNavigationAsync(() => page.ClickAsync("a"));36var element8 = await page.QuerySelectorAsync("a");37await element8.ClickAsync();38await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);39await page.ExpectNavigationAsync(() => page.ClickAsync("a"));40var element9 = await page.QuerySelectorAsync("a");41await element9.ClickAsync();42await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);43await page.ExpectNavigationAsync(() => page.ClickAsync("a"));44var element10 = await page.QuerySelectorAsync("a");45await element10.ClickAsync();
Expect
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Assertions;3using System.Threading.Tasks;4{5 {6 [PlaywrightTest("2.cs", "Expect", "Expect")]7 public async Task Expect()8 {9 using var playwright = await Playwright.CreateAsync();10 using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 await page.SetViewportSizeAsync(1280, 720);15 await page.ClickAsync("text=Sign in");
Expect
Using AI Code Generation
1await page.ExpectSelectorAsync("text=Hello, world!");2await page.ExpectSelectorAsync("css=div");3await page.ExpectSelectorAsync("id=foo");4await page.ExpectSelectorAsync("name=foo");5await page.ExpectSelectorAsync("selector=div");6await page.ExpectSelectorAsync("text=foo");7await page.ExpectSelectorAsync("data-testid=foo");8await page.ExpectSelectorAsync("data-test-id=foo");9await page.ExpectSelectorAsync("data-test=foo");10await page.ExpectSelectorAsync("data-testid=foo");11await page.ExpectSelectorAsync("data-test-id=foo");12await page.ExpectSelectorAsync("data-test=foo");13await page.ExpectSelectorAsync("data-testid=foo");14await page.ExpectSelectorAsync("data-test-id=foo");15await page.ExpectSelectorAsync("data-test=foo");16await page.ExpectSelectorAsync("data-testid=foo");17await page.ExpectSelectorAsync("data-test-id=foo");18await page.ExpectSelectorAsync("data-test=foo");19await page.ExpectSelectorAsync("text=Hello, world!");20await page.ExpectSelectorAsync("css=div");21await page.ExpectSelectorAsync("id=foo");22await page.ExpectSelectorAsync("name=foo");23await page.ExpectSelectorAsync("selector=div");24await page.ExpectSelectorAsync("text=foo");25await page.ExpectSelectorAsync("data-testid=foo");26await page.ExpectSelectorAsync("data-test-id=foo");27await page.ExpectSelectorAsync("data-test=foo");28await page.ExpectSelectorAsync("data-testid=foo");29await page.ExpectSelectorAsync("data-test-id=foo");30await page.ExpectSelectorAsync("data-test=foo");31await page.ExpectSelectorAsync("data-testid=foo");32await page.ExpectSelectorAsync("data-test-id=foo");33await page.ExpectSelectorAsync("data-test=foo");34await page.ExpectSelectorAsync("data-testid=foo");35await page.ExpectSelectorAsync("data-test-id=foo");36await page.ExpectSelectorAsync("data-test=foo");37await page.ExpectSelectorAsync("text=Hello, world!");38await page.ExpectSelectorAsync("css=div");39await page.ExpectSelectorAsync("id
Expect
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4using Microsoft.Playwright.Assertions;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 BrowserTypeLaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 await page.ExpectAsync("input[name='q']");15 await browser.CloseAsync();16 }17 }18}
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!!