Skip to content

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

3.9 KB
Raw Blame
/**
 * 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 type { BrowserContextOptions } from 'playwright-core';
import type * as types from './types';
import type { LanguageGenerator, LanguageGeneratorOptions } from './types';
import type * as actions from './actions';

export function generateCode(actions: actions.ActionInContext[], languageGenerator: LanguageGenerator, options: LanguageGeneratorOptions) {
  languageGenerator.reset();
  const header = languageGenerator.generateHeader(options);
  const footer = languageGenerator.generateFooter(options.saveStorage);
  const actionTexts = actions.map(a => languageGenerator.generateAction(a, options)).filter(Boolean);
  const text = [header, ...actionTexts, footer].join('\n');
  return { header, footer, actionTexts, text };
}

export function expectSignalAction(actionInContext: actions.ActionInContext, signal: actions.ExpectSignal): actions.ActionInContext {
  return {
    pageGuid: actionInContext.pageGuid,
    signals: [],
    action: {
      name: 'assertVisible',
      selector: signal.selector,
    },
  };
}

export function sanitizeDeviceOptions(device: any, options: BrowserContextOptions): BrowserContextOptions {
  // Filter out all the properties from the device descriptor.
  const cleanedOptions: Record<string, any> = {};
  for (const property in options) {
    if (JSON.stringify(device[property]) !== JSON.stringify((options as any)[property]))
      cleanedOptions[property] = (options as any)[property];
  }
  return cleanedOptions;
}

export function toSignalMap(actionInContext: actions.ActionInContext) {
  let popup: actions.PopupSignal | undefined;
  let download: actions.DownloadSignal | undefined;
  let dialog: actions.DialogSignal | undefined;
  let expect: actions.ExpectSignal | undefined;
  for (const signal of actionInContext.signals) {
    if (signal.name === 'popup')
      popup = signal;
    else if (signal.name === 'download')
      download = signal;
    else if (signal.name === 'dialog')
      dialog = signal;
    else if (signal.name === 'expect')
      expect = signal;
  }
  return {
    popup,
    download,
    dialog,
    expect,
  };
}

export function toKeyboardModifiers(modifiers: number): types.SmartKeyboardModifier[] {
  const result: types.SmartKeyboardModifier[] = [];
  if (modifiers & 1)
    result.push('Alt');
  if (modifiers & 2)
    result.push('ControlOrMeta');
  if (modifiers & 4)
    result.push('ControlOrMeta');
  if (modifiers & 8)
    result.push('Shift');
  return result;
}

export function fromKeyboardModifiers(modifiers?: types.SmartKeyboardModifier[]): number {
  let result = 0;
  if (!modifiers)
    return result;
  if (modifiers.includes('Alt'))
    result |= 1;
  if (modifiers.includes('Control'))
    result |= 2;
  if (modifiers.includes('ControlOrMeta'))
    result |= 2;
  if (modifiers.includes('Meta'))
    result |= 4;
  if (modifiers.includes('Shift'))
    result |= 8;
  return result;
}

export function toClickOptionsForSourceCode(action: actions.ClickAction): types.MouseClickOptions {
  const modifiers = toKeyboardModifiers(action.modifiers);
  const options: types.MouseClickOptions = {};
  if (action.button !== 'left')
    options.button = action.button;
  if (modifiers.length)
    options.modifiers = modifiers;
  // Do not render clickCount === 2 for dblclick.
  if (action.clickCount > 2)
    options.clickCount = action.clickCount;
  if (action.position)
    options.position = action.position;
  return options;
}