How to use ToHaveAttributeAsync method of Microsoft.Playwright.Core.LocatorAssertions class

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

Run Playwright-dotnet automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

LocatorAssertions.cs

Source: LocatorAssertions.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * 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 THE
22 * SOFTWARE.
23 */
24
25using System.Collections.Generic;
26using System.Linq;
27using System.Text.RegularExpressions;
28using System.Threading.Tasks;
29using Microsoft.Playwright.Transport.Protocol;
30
31namespace Microsoft.Playwright.Core
32{
33    internal class LocatorAssertions : AssertionsBase, ILocatorAssertions
34    {
35        public LocatorAssertions(ILocator locator, bool isNot) : base(locator, isNot)
36        {
37        }
38
39        public ILocatorAssertions Not => new LocatorAssertions(ActualLocator, !IsNot);
40
41        public Task ToBeCheckedAsync(LocatorAssertionsToBeCheckedOptions options = null)
42        {
43            var isChecked = options == null || options.Checked == null || options.Checked == true;
44            return ExpectTrueAsync(isChecked ? "to.be.checked" : "to.be.unchecked", $"Locator expected {(!isChecked ? "not " : string.Empty)}to be checked", ConvertToFrameExpectOptions(options));
45        }
46
47        public Task ToBeDisabledAsync(LocatorAssertionsToBeDisabledOptions options = null) => ExpectTrueAsync("to.be.disabled", "Locator expected to be disabled", ConvertToFrameExpectOptions(options));
48
49        public Task ToBeEditableAsync(LocatorAssertionsToBeEditableOptions options = null) => ExpectTrueAsync("to.be.editable", "Locator expected to be editable", ConvertToFrameExpectOptions(options));
50
51        public Task ToBeEmptyAsync(LocatorAssertionsToBeEmptyOptions options = null) => ExpectTrueAsync("to.be.empty", "Locator expected to be empty", ConvertToFrameExpectOptions(options));
52
53        public Task ToBeEnabledAsync(LocatorAssertionsToBeEnabledOptions options = null) => ExpectTrueAsync("to.be.enabled", "Locator expected to be enabled", ConvertToFrameExpectOptions(options));
54
55        public Task ToBeFocusedAsync(LocatorAssertionsToBeFocusedOptions options = null) => ExpectTrueAsync("to.be.focused", "Locator expected to be focused", ConvertToFrameExpectOptions(options));
56
57        public Task ToBeHiddenAsync(LocatorAssertionsToBeHiddenOptions options = null) => ExpectTrueAsync("to.be.hidden", "Locator expected to be hidden", ConvertToFrameExpectOptions(options));
58
59        public Task ToBeVisibleAsync(LocatorAssertionsToBeVisibleOptions options = null) => ExpectTrueAsync("to.be.visible", "Locator expected to be visible", ConvertToFrameExpectOptions(options));
60
61        private Task ExpectTrueAsync(string expression, string message, FrameExpectOptions options)
62        {
63            ExpectedTextValue[] expectedText = null;
64            return ExpectImplAsync(expression, expectedText, null, message, options);
65        }
66
67        public Task ToContainTextAsync(string expected, LocatorAssertionsToContainTextOptions options = null) =>
68            ExpectImplAsync("to.have.text", new ExpectedTextValue() { String = expected, MatchSubstring = true, NormalizeWhiteSpace = true }, expected, "Locator expected to contain text", ConvertToFrameExpectOptions(options));
69
70        public Task ToContainTextAsync(Regex expected, LocatorAssertionsToContainTextOptions options = null) =>
71            ExpectImplAsync("to.have.text", ExpectedRegex(expected, new() { MatchSubstring = true, NormalizeWhiteSpace = true }), expected, "Locator expected text matching regex", ConvertToFrameExpectOptions(options));
72
73        public Task ToContainTextAsync(IEnumerable<string> expected, LocatorAssertionsToContainTextOptions options = null) =>
74            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));
75
76        public Task ToContainTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToContainTextOptions options = null) =>
77            ExpectImplAsync("to.contain.text.array", expected.Select(regex => ExpectedRegex(regex, new() { MatchSubstring = true, NormalizeWhiteSpace = true })).ToArray(), expected, "Locator expected text matching regex", ConvertToFrameExpectOptions(options));
78
79        public Task ToHaveAttributeAsync(string name, string value, LocatorAssertionsToHaveAttributeOptions options = null) =>
80            ToHaveAttributeAsync(name, new() { String = value }, value, options);
81
82        public Task ToHaveAttributeAsync(string name, Regex value, LocatorAssertionsToHaveAttributeOptions options = null) =>
83            ToHaveAttributeAsync(name, ExpectedRegex(value), value, options);
84
85        private Task ToHaveAttributeAsync(string name, ExpectedTextValue expectedText, object expectedValue, LocatorAssertionsToHaveAttributeOptions options = null)
86        {
87            var commonOptions = ConvertToFrameExpectOptions(options);
88            commonOptions.ExpressionArg = name;
89            string message = $"Locator expected to have attribute '{name}'";
90            if (expectedValue is Regex)
91            {
92                message += " matching regex";
93            }
94            return ExpectImplAsync("to.have.attribute", expectedText, expectedValue, message, commonOptions);
95        }
96
97        public Task ToHaveClassAsync(string expected, LocatorAssertionsToHaveClassOptions options = null) =>
98            ExpectImplAsync("to.have.class", new ExpectedTextValue() { String = expected }, expected, "Locator expected to have class", ConvertToFrameExpectOptions(options));
99
100        public Task ToHaveClassAsync(Regex expected, LocatorAssertionsToHaveClassOptions options = null) =>
101            ExpectImplAsync("to.have.class", ExpectedRegex(expected), expected, "Locator expected matching regex", ConvertToFrameExpectOptions(options));
102
103        public Task ToHaveClassAsync(IEnumerable<string> expected, LocatorAssertionsToHaveClassOptions options = null) =>
104            ExpectImplAsync("to.have.class.array", expected.Select(text => new ExpectedTextValue() { String = text }).ToArray(), expected, "Locator expected to have class", ConvertToFrameExpectOptions(options));
105
106        public Task ToHaveClassAsync(IEnumerable<Regex> expected, LocatorAssertionsToHaveClassOptions options = null) =>
107            ExpectImplAsync("to.have.class.array", expected.Select(regex => ExpectedRegex(regex)).ToArray(), expected, "Locator expected to have class matching regex", ConvertToFrameExpectOptions(options));
108
109        public Task ToHaveCountAsync(int count, LocatorAssertionsToHaveCountOptions options = null)
110        {
111            ExpectedTextValue[] expectedText = null;
112            var commonOptions = ConvertToFrameExpectOptions(options);
113            commonOptions.ExpectedNumber = count;
114            return ExpectImplAsync("to.have.count", expectedText, count, "Locator expected to have count", commonOptions);
115        }
116
117        public Task ToHaveCSSAsync(string name, string value, LocatorAssertionsToHaveCSSOptions options = null) =>
118            ToHaveCSSAsync(name, new ExpectedTextValue() { String = value }, value, options);
119
120        public Task ToHaveCSSAsync(string name, Regex value, LocatorAssertionsToHaveCSSOptions options = null) =>
121            ToHaveCSSAsync(name, ExpectedRegex(value), value, options);
122
123        internal Task ToHaveCSSAsync(string name, ExpectedTextValue expectedText, object expectedValue, LocatorAssertionsToHaveCSSOptions options = null)
124        {
125            var commonOptions = ConvertToFrameExpectOptions(options);
126            commonOptions.ExpressionArg = name;
127            var message = $"Locator expected to have CSS property '{name}'";
128            if (expectedValue is Regex)
129            {
130                message += " matching regex";
131            }
132            return ExpectImplAsync("to.have.css", expectedText, expectedValue, message, commonOptions);
133        }
134
135        public Task ToHaveIdAsync(string id, LocatorAssertionsToHaveIdOptions options = null) =>
136            ExpectImplAsync("to.have.id", new ExpectedTextValue() { String = id }, id, "Locator expected to have ID", ConvertToFrameExpectOptions(options));
137
138        public Task ToHaveIdAsync(Regex id, LocatorAssertionsToHaveIdOptions options = null) =>
139            ExpectImplAsync("to.have.id", ExpectedRegex(id), id, "Locator expected to have ID", ConvertToFrameExpectOptions(options));
140
141        public Task ToHaveJSPropertyAsync(string name, object value, LocatorAssertionsToHaveJSPropertyOptions options = null)
142        {
143            var commonOptions = ConvertToFrameExpectOptions(options);
144            commonOptions.ExpressionArg = name;
145            commonOptions.ExpectedValue = ScriptsHelper.SerializedArgument(value);
146            ExpectedTextValue[] expectedText = null;
147            return ExpectImplAsync("to.have.property", expectedText, value, $"Locator expected to have JavaScript property '{name}'", commonOptions);
148        }
149
150        public Task ToHaveTextAsync(string expected, LocatorAssertionsToHaveTextOptions options = null) =>
151            ExpectImplAsync("to.have.text", new ExpectedTextValue() { String = expected, NormalizeWhiteSpace = true }, expected, "Locator expected to have text", ConvertToFrameExpectOptions(options));
152
153        public Task ToHaveTextAsync(Regex expected, LocatorAssertionsToHaveTextOptions options = null) =>
154        ExpectImplAsync("to.have.text", ExpectedRegex(expected, new() { NormalizeWhiteSpace = true }), expected, "Locator expected to have text matching regex", ConvertToFrameExpectOptions(options));
155
156        public Task ToHaveTextAsync(IEnumerable<string> expected, LocatorAssertionsToHaveTextOptions options = null) =>
157            ExpectImplAsync("to.have.text.array", expected.Select(text => new ExpectedTextValue() { String = text, NormalizeWhiteSpace = true }).ToArray(), expected, "Locator expected to have text", ConvertToFrameExpectOptions(options));
158
159        public Task ToHaveTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToHaveTextOptions options = null) =>
160            ExpectImplAsync("to.have.text.array", expected.Select(regex => ExpectedRegex(regex, new() { NormalizeWhiteSpace = true })).ToArray(), expected, "Locator expected to have text", ConvertToFrameExpectOptions(options));
161
162        public Task ToHaveValueAsync(string value, LocatorAssertionsToHaveValueOptions options = null) =>
163            ExpectImplAsync("to.have.value", new ExpectedTextValue() { String = value }, value, "Locator expected to have value", ConvertToFrameExpectOptions(options));
164
165        public Task ToHaveValueAsync(Regex value, LocatorAssertionsToHaveValueOptions options = null) =>
166            ExpectImplAsync("to.have.value", ExpectedRegex(value), value, "Locator expected to have value matching regex", ConvertToFrameExpectOptions(options));
167    }
168}
169
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger ToHaveAttributeAsync code on LambdaTest Cloud Grid

Execute automation tests with ToHaveAttributeAsync on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)