Playwright URL 配置 - 多个应用程序和不同基本 URL 的环境变量

Hop*_*rka 4 automated-tests base-url playwright

为项目配置和使用 url 的最佳方法是什么

  • 2x 网络应用程序
  • 3x 环境

...作为示例,我们有这样的情况

水果应用程序

  • 基本网址:
    • dev.fruits.com
    • test.fruits.com
    • prod.fruits.com
  • 端点:
    • /香蕉/
    • /猕猴桃/
    • /苹果/

色彩应用程序

  • 基本网址:
    • dev.colors.com
    • test.colors.com
    • prod.colors.com
  • 端点:
    • /红色的/
    • /蓝色的/
    • /绿色的/

...和这样的测试


    test('Navigate to fruits/banana', async () => {
      await page.goto('https://https://dev.fruits.com/banana/');
      ...
    });
     
    test('Navigate to colors/red', async () => {
      await page.goto('https://https://dev.colors.com/red/');
      ...
    });

Run Code Online (Sandbox Code Playgroud)

...我想去的地方

  1. 将 dev.fruits.com 和 dev.colors.com 替换为 baseurl 变量
  2. “dev”部分应该是动态的,具体取决于我运行测试的环境

Yur*_*sky 9

您可以针对不同的配置使用单独的项目:

// @ts-check
const { devices } = require('@playwright/test');

/**
 * @see https://playwright.dev/docs/test-configuration
 * @type{import('@playwright/test').PlaywrightTestConfig}
 */
const config = {
  projects: [
    {
      name: 'Fruit Dev',
      testMatch: 'fruits/**/*',
      use: {
        baseURL: 'https://dev.fruits.com'
      }
    },
    {
      name: 'Fruit Test',
      testMatch: 'fruits/**/*',
      use: {
        baseURL: 'https://test.fruits.com'
      }
    },
    {
      name: 'Fruit Prod',
      testMatch: 'fruits/**/*',
      use: {
        baseURL: 'https://test.fruits.com'
      }
    },
    {
      name: 'Color Dev',
      testMatch: 'colors/**/*',
      use: {
        baseURL: 'https://dev.colors.com'
      }
    },
    {
      name: 'Color Test',
      testMatch: 'colors/**/*',
      use: {
        baseURL: 'https://test.colors.com'
      }
    },
    {
      name: 'Color Prod',
      testMatch: 'colors/**/*',
      use: {
        baseURL: 'https://test.colors.com'
      }
    },
  ]
};
module.exports = config;

Run Code Online (Sandbox Code Playgroud)