Skip to content

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

5.6 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 { test as it, expect } from './pageTest';

it('AW-013 clicking on links which do not commit navigation', async ({ page, server, httpsServer }) => {
  await page.goto(server.EMPTY_PAGE);
  await page.setContent(`<a href='${httpsServer.EMPTY_PAGE}'>foobar</a>`);
  await page.click('a');
});

it('AW-014 calling window.stop async', async ({ page, server }) => {
  server.setRoute('/empty.html', async (req, res) => {});
  await page.evaluate(url => {
    window.location.href = url;
    setTimeout(() => window.stop(), 100);
  }, server.EMPTY_PAGE);
});

it('AW-015 calling window.stop sync', async ({ page, server, browserName }) => {
  await page.evaluate(url => {
    window.location.href = url;
    window.stop();
  }, server.EMPTY_PAGE);
});

it('AW-016 assigning location to about:blank', async ({ page, server }) => {
  await page.goto(server.EMPTY_PAGE);
  await page.evaluate(`window.location.href = "about:blank";`);
});

it('AW-017 assigning location to about:blank after non-about:blank', async ({ page, server }) => {
  server.setRoute('/empty.html', async (req, res) => {});
  await page.evaluate(`
      window.location.href = "${server.EMPTY_PAGE}";
      window.location.href = "about:blank";`);
});

it('AW-018 calling window.open and window.close', async function({ page, server }) {
  await page.goto(server.EMPTY_PAGE);
  await page.evaluate(() => {
    const popup = window.open(window.location.href);
    popup.close();
  });
});

it('AW-019 opening a popup', async function({ page, server }) {
  await page.goto(server.EMPTY_PAGE);
  await Promise.all([
    page.waitForEvent('popup'),
    page.evaluate(() => window.open(window.location.href) && 1),
  ]);
});

it('AW-020 clicking in the middle of navigation that aborts', async ({ page, server }) => {
  let abortCallback;
  const abortPromise = new Promise(f => abortCallback = f);

  let stallCallback;
  const stallPromise = new Promise(f => stallCallback = f);

  server.setRoute('/stall.html', async (req, res) => {
    stallCallback();
    await abortPromise;
    req.socket.destroy();
  });

  await page.goto(server.PREFIX + '/one-style.html');
  page.goto(server.PREFIX + '/stall.html').catch(() => {});
  await stallPromise;

  const clickPromise = page.click('body');
  await page.waitForTimeout(1000);
  abortCallback();

  await clickPromise;
});

it('AW-021 clicking in the middle of navigation that commits', async ({ page, server }) => {
  let commitCallback;
  const abortPromise = new Promise(f => commitCallback = f);

  let stallCallback;
  const stallPromise = new Promise(f => stallCallback = f);

  server.setRoute('/stall.html', async (req, res) => {
    stallCallback();
    await abortPromise;
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('hello world');
  });

  await page.goto(server.PREFIX + '/one-style.html');
  page.goto(server.PREFIX + '/stall.html').catch(() => {});
  await stallPromise;

  const clickPromise = page.click('body');
  await page.waitForTimeout(1000);
  commitCallback();

  await clickPromise;
  await expect(page.locator('body')).toContainText('hello world');
});

it('AW-022 clicking a link intercepted by the Navigation API same-document', {
  annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/41125' },
}, async ({ page, server }) => {
  server.setRoute('/intercept.html', (req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(`
      <a id="go" href="/other">go</a>
      <p id="status">initial</p>
      <script>
        navigation.addEventListener('navigate', event => {
          if (!event.canIntercept)
            return;
          event.intercept({
            handler: async () => {
              const dest = new URL(event.destination.url).pathname;
              document.getElementById('status').textContent = 'intercepted:' + dest;
            },
          });
        });
      </script>
    `);
  });

  await page.goto(server.PREFIX + '/intercept.html');
  await expect(page.locator('#status')).toHaveText('initial');

  // Should not hang waiting for the same-document navigation to commit.
  await page.locator('#go').click();

  await expect(page.locator('#status')).toHaveText('intercepted:/other');
  expect(new URL(page.url()).pathname).toBe('/other');
});

it('AW-023 goBack in the middle of navigation that commits', async ({ page, server }) => {
  let commitCallback;
  const abortPromise = new Promise(f => commitCallback = f);

  let stallCallback;
  const stallPromise = new Promise(f => stallCallback = f);

  server.setRoute('/stall.html', async (req, res) => {
    stallCallback();
    await abortPromise;
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('hello world');
  });

  await page.goto(server.PREFIX + '/one-style.html');
  page.goto(server.PREFIX + '/stall.html').catch(() => {});
  await stallPromise;

  const goBackPromise = page.goBack().catch(() => {});
  await page.waitForTimeout(1000);
  commitCallback();

  await goBackPromise;
});