How to use ShouldAffectNavigatorLanguage method of Microsoft.Playwright.Tests.BrowserContextLocaleTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.BrowserContextLocaleTests.ShouldAffectNavigatorLanguage

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

BrowserContextLocaleTests.cs

Source: BrowserContextLocaleTests.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.Threading.Tasks;
26using Microsoft.Playwright.NUnit;
27using NUnit.Framework;
28
29namespace Microsoft.Playwright.Tests
30{
31    public class BrowserContextLocaleTests : BrowserTestEx
32    {
33        [PlaywrightTest("browsercontext-locale.spec.ts", "should affect accept-language header")]
34        public async Task ShouldAffectAcceptLanguageHeader()
35        {
36            await using var context = await Browser.NewContextAsync(new() { Locale = "fr-CH" });
37            string acceptLanguage = string.Empty;
38            var page = await context.NewPageAsync();
39            var requestTask = Server.WaitForRequest("/empty.html", c => acceptLanguage = c.Headers["accept-language"]);
40
41            await TaskUtils.WhenAll(
42                requestTask,
43                page.GotoAsync(Server.EmptyPage));
44
45            Assert.That(acceptLanguage, Does.StartWith("fr-CH"));
46        }
47
48        [PlaywrightTest("browsercontext-locale.spec.ts", "should affect navigator.language")]
49        public async Task ShouldAffectNavigatorLanguage()
50        {
51            await using var context = await Browser.NewContextAsync(new()
52            {
53                Locale = "fr-CH"
54            });
55            var page = await context.NewPageAsync();
56            Assert.AreEqual("fr-CH", await page.EvaluateAsync<string>("navigator.language"));
57        }
58
59        [PlaywrightTest("browsercontext-locale.spec.ts", "should format number")]
60        public async Task ShouldFormatNumber()
61        {
62            await using (var context = await Browser.NewContextAsync(new()
63            {
64                Locale = "en-US"
65            }))
66            {
67                var page = await context.NewPageAsync();
68                await page.GotoAsync(Server.EmptyPage);
69                Assert.AreEqual("1,000,000.5", await page.EvaluateAsync<string>("() => (1000000.50).toLocaleString()"));
70            }
71
72            await using (var context = await Browser.NewContextAsync(new()
73            {
74                Locale = "fr-CH"
75            }))
76            {
77                var page = await context.NewPageAsync();
78                await page.GotoAsync(Server.EmptyPage);
79                string value = await page.EvaluateAsync<string>("() => (1000000.50).toLocaleString().replace(/\\s/g, ' ')");
80                Assert.AreEqual("1 000 000,5", value);
81            }
82        }
83
84        [PlaywrightTest("browsercontext-locale.spec.ts", "should format date")]
85        public async Task ShouldFormatDate()
86        {
87            await using (var context = await Browser.NewContextAsync(new()
88            {
89                Locale = "en-US",
90                TimezoneId = "America/Los_Angeles",
91            }))
92            {
93                var page = await context.NewPageAsync();
94                await page.GotoAsync(Server.EmptyPage);
95                Assert.AreEqual(
96                    "Sat Nov 19 2016 10:12:34 GMT-0800 (Pacific Standard Time)",
97                    await page.EvaluateAsync<string>("() => new Date(1479579154987).toString()"));
98            }
99
100            await using (var context = await Browser.NewContextAsync(new()
101            {
102                Locale = "de-DE",
103                TimezoneId = "Europe/Berlin",
104            }))
105            {
106                var page = await context.NewPageAsync();
107                await page.GotoAsync(Server.EmptyPage);
108                Assert.AreEqual(
109                    "Sat Nov 19 2016 19:12:34 GMT+0100 (MitteleuropÀische Normalzeit)",
110                    await page.EvaluateAsync<string>("() => new Date(1479579154987).toString()"));
111            }
112        }
113
114        [PlaywrightTest("browsercontext-locale.spec.ts", "should format number in popups")]
115        public async Task ShouldFormatNumberInPopups()
116        {
117            await using var context = await Browser.NewContextAsync(new()
118            {
119                Locale = "fr-CH"
120            });
121
122            var page = await context.NewPageAsync();
123            await page.GotoAsync(Server.EmptyPage);
124            var popupTask = page.WaitForPopupAsync();
125
126            await TaskUtils.WhenAll(
127                popupTask,
128                page.EvaluateAsync("url => window._popup = window.open(url)", Server.Prefix + "/formatted-number.html"));
129
130            var popup = popupTask.Result;
131            await popup.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
132            Assert.AreEqual("1 000 000,5", await popup.EvaluateAsync<string>("() => window.result"));
133        }
134
135        [PlaywrightTest("browsercontext-locale.spec.ts", "should affect navigator.language in popups")]
136        public async Task ShouldAffectNavigatorLanguageInPopups()
137        {
138            await using var context = await Browser.NewContextAsync(new()
139            {
140                Locale = "fr-CH"
141            });
142
143            var page = await context.NewPageAsync();
144            await page.GotoAsync(Server.EmptyPage);
145            var popupTask = page.WaitForPopupAsync();
146
147            await TaskUtils.WhenAll(
148                popupTask,
149                page.EvaluateAsync("url => window._popup = window.open(url)", Server.Prefix + "/formatted-number.html"));
150
151            var popup = popupTask.Result;
152            await popup.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
153            Assert.AreEqual("fr-CH", await popup.EvaluateAsync<string>("() => window.initialNavigatorLanguage"));
154        }
155    }
156}
157
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 ShouldAffectNavigatorLanguage code on LambdaTest Cloud Grid

Execute automation tests with ShouldAffectNavigatorLanguage 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)