๐Ÿ“ฆ microsoft / playwright

๐Ÿ“„ browsercontext-timezone-id.spec.ts ยท 118 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118/**
 * Copyright 2018 Google Inc. All rights reserved.
 * Modifications copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { browserTest as it, expect } from '../config/browserTest';

it('should work @smoke', async ({ browser, browserName }) => {
  const func = () => new Date(1479579154987).toString();
  {
    const context = await browser.newContext({ locale: 'en-US', timezoneId: 'America/Jamaica' });
    const page = await context.newPage();
    expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)');
    await context.close();
  }
  {
    const context = await browser.newContext({ locale: 'en-US', timezoneId: 'Pacific/Honolulu' });
    const page = await context.newPage();
    expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)');
    await context.close();
  }
  {
    const context = await browser.newContext({ locale: 'en-US', timezoneId: browserName === 'firefox' ? 'America/Argentina/Buenos_Aires' : 'America/Buenos_Aires' });
    const page = await context.newPage();
    expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)');
    await context.close();
  }
  {
    const context = await browser.newContext({ locale: 'en-US', timezoneId: 'Europe/Berlin' });
    const page = await context.newPage();
    expect(await page.evaluate(func)).toBe('Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)');
    await context.close();
  }
});

it('should throw for invalid timezone IDs when creating pages', async ({ browser, channel, browserName, isBidi }) => {
  for (const timezoneId of ['Foo/Bar', 'Baz/Qux']) {
    if (isBidi) {
      const error = await browser.newContext({ timezoneId }).catch(e => e);
      if (browserName === 'chromium')
        expect(error.message).toContain(`Invalid timezone "${timezoneId}"`);
      else if (browserName === 'firefox')
        expect(error.message).toContain(`Expected "timezone" to be a valid timezone ID (e.g., "Europe/Berlin") or a valid timezone offset (e.g., "+01:00"), got ${timezoneId}`);
    } else {
      let error = null;
      let context = null;
      try {
        context = await browser.newContext({ timezoneId });
        await context.newPage();
      } catch (e) {
        error = e;
      }
      expect(error.message).toContain(`Invalid timezone ID: ${timezoneId}`);
      await context?.close();
    }
  }
});

it('should work for multiple pages sharing same process', async ({ browser, server }) => {
  const context = await browser.newContext({ timezoneId: 'Europe/Moscow' });
  const page = await context.newPage();
  await page.goto(server.EMPTY_PAGE);
  let [popup] = await Promise.all([
    page.waitForEvent('popup'),
    page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
  ]);
  [popup] = await Promise.all([
    popup.waitForEvent('popup'),
    popup.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
  ]);
  await context.close();
});

it('should not change default timezone in another context', async ({ browser, server }) => {
  async function getContextTimezone(context) {
    const page = await context.newPage();
    return await page.evaluate(() => Intl.DateTimeFormat().resolvedOptions().timeZone);
  }

  let defaultTimezone;
  {
    const context = await browser.newContext();
    defaultTimezone = await getContextTimezone(context);
    await context.close();
  }
  const timezoneOverride = defaultTimezone === 'Europe/Moscow' ? 'America/Los_Angeles' : 'Europe/Moscow';
  {
    const context = await browser.newContext({ timezoneId: timezoneOverride });
    expect(await getContextTimezone(context)).toBe(timezoneOverride);
    await context.close();
  }
  {
    const context = await browser.newContext();
    expect(await getContextTimezone(context)).toBe(defaultTimezone);
    await context.close();
  }
});

it('should affect Intl.DateTimeFormat().resolvedOptions().timeZone', async ({ browser, server }) => {
  const context = await browser.newContext({ timezoneId: 'America/Jamaica' });
  const page = await context.newPage();
  await page.goto(server.EMPTY_PAGE);
  expect(await page.evaluate(() => (new Intl.DateTimeFormat()).resolvedOptions().timeZone)).toBe('America/Jamaica');
  await context.close();
});