Repository: microsoft/playwright
File: tests/library/defaultbrowsercontext-2.spec.ts (TypeScript)

Code before:
/**
 * Copyright 2017 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 { playwrightTest as it, expect } from '../config/browserTest';
import fs from 'fs';
import path from 'path';

it('should support hasTouch option', async ({ server, launchPersistent }) => {
  const { page } = await launchPersistent({ hasTouch: true });
  await page.goto(server.PREFIX + '/mobile.html');
  expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true);
});

it('should work in persistent context', async ({ server, launchPersistent, browserName }) => {
  it.skip(browserName === 'firefox', 'Firefox does not support mobile');

  const { page } = await launchPersistent({ viewport: { width: 320, height: 480 }, isMobile: true });
  await page.goto(server.PREFIX + '/empty.html');
  expect(await page.evaluate(() => window.innerWidth)).toBe(980);
});

it('should support colorScheme option', async ({ launchPersistent }) => {
  const { page } = await launchPersistent({ colorScheme: 'dark' });
  expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(true);
});

it('should support reducedMotion option', async ({ launchPersistent }) => {
  const { page } = await launchPersistent({ reducedMotion: 'reduce' });
  expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches)).toBe(false);
});


Review comment (suggestion):
Some tests in the file call `browserType.launchPersistentContext` instead of using the `launchPersistent` fixture, so we need this skip.

Code after:
/**
 * Copyright 2017 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 { playwrightTest as it, expect } from '../config/browserTest';
import fs from 'fs';
import path from 'path';

it.skip(({ mode }) => mode !== 'default', 'Remote persistent contexts are not supported');

it('should support hasTouch option', async ({ server, launchPersistent }) => {
  const { page } = await launchPersistent({ hasTouch: true });
  await page.goto(server.PREFIX + '/mobile.html');
  expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true);
});

it('should work in persistent context', async ({ server, launchPersistent, browserName }) => {
  it.skip(browserName === 'firefox', 'Firefox does not support mobile');

  const { page } = await launchPersistent({ viewport: { width: 320, height: 480 }, isMobile: true });
  await page.goto(server.PREFIX + '/empty.html');
  expect(await page.evaluate(() => window.innerWidth)).toBe(980);
});

it('should support colorScheme option', async ({ launchPersistent }) => {
  const { page } = await launchPersistent({ colorScheme: 'dark' });
  expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: light)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches)).toBe(true);
});

it('should support reducedMotion option', async ({ launchPersistent }) => {
  const { page } = await launchPersistent({ reducedMotion: 'reduce' });
  expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches)).toBe(false);