Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

10.8 KB
Raw Blame
/**
 * 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 { devices } from '@playwright/test';
import { contextTest as it, expect } from '../config/browserTest';
import { browserTest } from '../config/browserTest';
import { verifyViewport } from '../config/utils';
import { server as coreServer } from '../../packages/playwright-core/lib/coreBundle';
const { deviceDescriptors } = coreServer;

it('VP-001 should get the proper default viewport size', async ({ page, server }) => {
  await verifyViewport(page, 1280, 720);
});

it('VP-002 should set the proper viewport size', async ({ page, server }) => {
  await verifyViewport(page, 1280, 720);
  await page.setViewportSize({ width: 345, height: 456 });
  await verifyViewport(page, 345, 456);
});

it('VP-003 should return correct outerWidth and outerHeight', async ({ page }) => {
  await page.setViewportSize({ width: 410, height: 420 });
  const size = await page.evaluate(() => {
    return {
      innerWidth: window.innerWidth,
      innerHeight: window.innerHeight,
      outerWidth: window.outerWidth,
      outerHeight: window.outerHeight,
    };
  });
  expect(size.innerWidth).toBe(410);
  expect(size.innerHeight).toBe(420);
  expect(size.outerWidth >= size.innerWidth).toBeTruthy();
  expect(size.outerHeight >= size.innerHeight).toBeTruthy();
});

it('VP-004 landscape viewport should have width larger than height', async () => {
  for (const device in deviceDescriptors) {
    const configuration = deviceDescriptors[device];
    if (device.includes('landscape') || device.includes('Landscape'))
      expect(configuration.viewport.width).toBeGreaterThan(configuration.viewport.height);
  }
});

it('VP-005 should emulate device width', async ({ page, server }) => {
  expect(page.viewportSize()).toEqual({ width: 1280, height: 720 });
  await page.setViewportSize({ width: 300, height: 300 });
  expect(await page.evaluate(() => window.screen.width)).toBe(300);
  expect(await page.evaluate(() => matchMedia('(min-device-width: 200px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(min-device-width: 400px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-width: 200px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-width: 400px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(device-width: 600px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(device-width: 300px)').matches)).toBe(true);
  await page.setViewportSize({ width: 500, height: 500 });
  expect(await page.evaluate(() => window.screen.width)).toBe(500);
  expect(await page.evaluate(() => matchMedia('(min-device-width: 400px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(min-device-width: 600px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-width: 400px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-width: 600px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(device-width: 200px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(device-width: 500px)').matches)).toBe(true);
});

it('VP-006 should emulate device height', async ({ page, server }) => {
  expect(page.viewportSize()).toEqual({ width: 1280, height: 720 });
  await page.setViewportSize({ width: 300, height: 300 });
  expect(await page.evaluate(() => window.screen.height)).toBe(300);
  expect(await page.evaluate(() => matchMedia('(min-device-height: 200px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(min-device-height: 400px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-height: 200px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-height: 400px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(device-height: 600px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(device-height: 300px)').matches)).toBe(true);
  await page.setViewportSize({ width: 500, height: 500 });
  expect(await page.evaluate(() => window.screen.height)).toBe(500);
  expect(await page.evaluate(() => matchMedia('(min-device-height: 400px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(min-device-height: 600px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-height: 400px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(max-device-height: 600px)').matches)).toBe(true);
  expect(await page.evaluate(() => matchMedia('(device-height: 200px)').matches)).toBe(false);
  expect(await page.evaluate(() => matchMedia('(device-height: 500px)').matches)).toBe(true);
});

it('VP-007 should emulate availWidth and availHeight', async ({ page }) => {
  await page.setViewportSize({ width: 500, height: 600 });
  expect(await page.evaluate(() => window.screen.availWidth)).toBe(500);
  expect(await page.evaluate(() => window.screen.availHeight)).toBe(600);
});

it('VP-008 should not have touch by default', async ({ page, server, browserName, platform }) => {
  await page.goto(server.PREFIX + '/mobile.html');
  expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false);
});

it('VP-009 should throw on tap if hasTouch is not enabled', async ({ page }) => {
  await page.setContent(`<div>a</div>`);
  {
    const error = await page.tap('div').catch(e => e);
    expect(error).toBeTruthy();
    expect(error.message).toContain('The page does not support tap');
  }
  {
    const error = await page.locator('div').tap().catch(e => e);
    expect(error).toBeTruthy();
    expect(error.message).toContain('The page does not support tap');
  }
});

browserTest('VP-010 should support touch with null viewport', async ({ browser, server }) => {
  const context = await browser.newContext({ viewport: null, hasTouch: true });
  const page = await context.newPage();
  await page.goto(server.PREFIX + '/mobile.html');
  expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true);
  await context.close();
});

browserTest('VP-011 should keep touch emulation after screenshot beyond viewport', async ({ browser, server }) => {
  browserTest.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42607' });
  const context = await browser.newContext({ viewport: { width: 393, height: 727 }, hasTouch: true });
  const page = await context.newPage();
  await page.goto(server.EMPTY_PAGE);
  await page.setContent(`<div id="tall" style="height:3000px">tall</div>`);
  const probe = () => page.evaluate(() => ({
    ontouchstart: 'ontouchstart' in window,
    maxTouchPoints: navigator.maxTouchPoints,
    coarse: matchMedia('(pointer: coarse)').matches,
  }));
  const expected = await probe();
  expect(expected.ontouchstart).toBe(true);
  await page.screenshot({ fullPage: true });
  expect(await probe()).toEqual(expected);
  await page.locator('#tall').screenshot();
  expect(await probe()).toEqual(expected);
  await page.goto(server.EMPTY_PAGE);
  expect(await probe()).toEqual(expected);
  await context.close();
});

it('VP-012 should set both screen and viewport options', async ({ contextFactory, browserName, isBidi }) => {
  const context = await contextFactory({
    screen: { 'width': 1280, 'height': 720 },
    viewport: { 'width': 1000, 'height': 600 },
  });
  const page = await context.newPage();
  const screen = await page.evaluate(() => ({ w: screen.width, h: screen.height }));
  expect(screen).toEqual({ w: 1280, h: 720 });
  const inner = await page.evaluate(() => ({ w: window.innerWidth, h: window.innerHeight }));
  expect(inner).toEqual({ w: 1000, h: 600 });
});

browserTest('VP-013 should report null viewportSize when given null viewport', async ({ browser, server }) => {
  const context = await browser.newContext({ viewport: null });
  const page = await context.newPage();
  expect(page.viewportSize()).toBe(null);
  await context.close();
});

browserTest('VP-014 should drag with high dpi', async ({ browser, server, headless }) => {
  browserTest.skip(!headless, 'Stray mouse events mess up the test');

  const page = await browser.newPage({ deviceScaleFactor: 2 });
  await page.goto(server.PREFIX + '/drag-n-drop.html');
  await page.hover('#source');
  await page.mouse.down();
  await page.hover('#target');
  await page.mouse.up();
  expect(await page.$eval('#target', target => target.contains(document.querySelector('#source')))).toBe(true); // could not find source in target
  await page.close();
});

it('VP-015 WebKit Windows headed should have a minimal viewport', async ({ contextFactory, browserName, headless, isWindows }) => {
  it.skip(browserName !== 'webkit' || headless || !isWindows, 'Not relevant for this browser');

  await expect(contextFactory({
    viewport: { 'width': 100, 'height': 100 },
  })).rejects.toThrow('WebKit on Windows has a minimal viewport of 250x240.');

  const context = await contextFactory();
  const page = await context.newPage();
  await expect(page.setViewportSize({ width: 100, height: 100 })).rejects.toThrow('WebKit on Windows has a minimal viewport of 250x240.');
  await context.close();
});

browserTest('VP-016 should be able to get correct orientation angle on non-mobile devices', async ({ browser, browserName, server }) => {
  browserTest.skip(browserName === 'webkit', 'Desktop webkit dont support orientation API');

  const context = await browser.newContext({ viewport: { width: 300, height: 400 }, isMobile: false });
  const page = await context.newPage();
  await page.goto(server.PREFIX + '/index.html');
  expect(await page.evaluate(() => window.screen.orientation.angle)).toBe(0);
  await page.setViewportSize({ width: 400, height: 300 });
  expect(await page.evaluate(() => window.screen.orientation.angle)).toBe(0);
  await context.close();
});

it('VP-017 should set window.screen.orientation.type for mobile devices', async ({ contextFactory, server }) => {
  it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/31151' });
  const context = await contextFactory(devices['iPhone 14']);
  const page = await context.newPage();
  await page.goto(server.PREFIX + '/index.html');
  expect(await page.evaluate(() => window.screen.orientation.type)).toBe('portrait-primary');
  await context.close();
});