模拟
使用 Playwright,您可以在任何浏览器上测试您的应用程序,以及模拟真实设备,如手机或平板电脑。只需配置您想要模拟的设备,Playwright 就会模拟浏览器行为,例如 "userAgent"、"screenSize"、"viewport" 以及是否启用 "hasTouch"。您还可以为所有测试或特定测试模拟 "geolocation"、"locale" 和 "timezone",以及设置 "permissions" 以显示通知或更改 "colorScheme"。
设备
Playwright 附带了一个 设备参数注册表,使用 playwright.devices 用于选定的桌面、平板电脑和移动设备。它可用于模拟特定设备的浏览器行为,例如用户代理、屏幕尺寸、视口以及是否启用了触摸。所有测试都将使用指定的设备参数运行。
- TypeScript
- JavaScript
- Library
// playwright.config.ts
import { type PlaywrightTestConfig, devices } from '@playwright/test'; // import devices
const config: PlaywrightTestConfig = {
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
{
name: 'Mobile Safari',
use: {
...devices['iPhone 12'],
},
},
],
};
export default config;
// playwright.config.js
// @ts-check
const { devices } = require('@playwright/test'); // require devices
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
{
name: 'Mobile Safari',
use: {
...devices['iPhone 12'],
},
},
],
};
module.exports = config;
const { chromium, devices } = require('playwright');
const browser = await chromium.launch();
const iphone12 = devices['iPhone 12'];
const context = await browser.newContext({
...iphone12,
});
视口
视口包含在设备中,但您可以使用 page.setViewportSize(viewportSize) 为某些测试覆盖它。
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
// 使用类似纵向的视口运行此文件中的测试。
test.use({
viewport: { width: 600, height: 900 },
});
test('my portrait test', async ({ page }) => {
// ...
});
const { test, expect } = require('@playwright/test');
// 使用类似纵向的视口运行此文件中的测试。
test.use({
viewport: { width: 600, height: 900 },
});
test('my portrait test', async ({ page }) => {
// ...
});
// 使用给定视口创建上下文
const context = await browser.newContext({
viewport: { width: 1280, height: 1024 }
});
// 调整单个页面的视口大小
await page.setViewportSize({ width: 1600, height: 1200 });
// 模拟高 DPI
const context = await browser.newContext({
viewport: { width: 2560, height: 1440 },
deviceScaleFactor: 2,
});
同样适用于 describe 块内部。
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test.describe('locale block', () => {
// 使用类似纵向的视口运行此 describe 块中的测试。
test.use({ viewport: { width: 600, height: 900 } });
test('my portrait test', async ({ page }) => {
// ...
});
});
const { test, expect } = require('@playwright/test');
test.describe('locale block', () => {
// 使用类似纵向的视口运行此 describe 块中的测试。
test.use({ viewport: { width: 600, height: 900 } });
test('my portrait test', async ({ page }) => {
// ...
});
});
区域设置和时区
模拟用户区域设置和时区,可以在配置中为所有测试全局设置,然后为特定测试覆盖。
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
test.use({
locale: 'de-DE',
timezoneId: 'Europe/Berlin',
});
test('my test for de lang in Berlin timezone', async ({ page }) => {
// ...
});
const { test, expect } = require('@playwright/test');
test.use({
locale: 'de-DE',
timezoneId: 'Europe/Berlin',
});
test('my test for de lang in Berlin timezone', async ({ page }) => {
// ...
});
// 模拟区域设置和时间
const context = await browser.newContext({
locale: 'de-DE',
timezoneId: 'Europe/Berlin',
});
权限
允许应用显示系统通知。
- TypeScript
- JavaScript
- Library
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
use: {
permissions: ['notifications'],
},
};
export default config;
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
use: {
permissions: ['notifications'],
},
};
module.exports = config;
const context = await browser.newContext({
permissions: ['notifications'],
});
允许测试请求当前位置。
- TypeScript
- JavaScript
- Library
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
use: {
permissions: ['geolocation'],
},
};
export default config;
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
use: {
permissions: ['geolocation'],
},
};
module.exports = config;
await context.grantPermissions(['geolocation']);
允许特定域的通知。
- TypeScript
- JavaScript
- Library
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
use: {
permissions: ['notifications'], {origin: 'https://skype.com'},
},
};
export default config;
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
use: {
permissions: ['notifications'], {origin: 'https://skype.com'},
},
};
module.exports = config;
await context.grantPermissions(['notifications'], {origin: 'https://skype.com'} );
使用 browserContext.clearPermissions() 撤销所有权限。
// Library
await context.clearPermissions();
地理位置
创建一个授予 "geolocation" 权限并将地理位置设置为特定区域的测试。
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
test.use({
geolocation: { longitude: 48.858455, latitude: 2.294474 },
permissions: ['geolocation'],
});
test('my test with geolocation', async ({ page }) => {
// ...
});
const { test, expect } = require('@playwright/test');
test.use({
geolocation: { longitude: 48.858455, latitude: 2.294474 },
permissions: ['geolocation'],
});
test('my test with geolocation', async ({ page }) => {
// ...
});
const context = await browser.newContext({
geolocation: { longitude: 48.858455, latitude: 2.294474 },
permissions: ['geolocation']
});
稍后更改位置:
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
test.use({
geolocation: { longitude: 48.858455, latitude: 2.294474 },
permissions: ['geolocation'],
});
test('my test with geolocation', async ({ page, context }) => {
// 覆盖此测试的位置
context.setGeolocation({ longitude: 29.979097, latitude: 31.134256 });
});
const { test, expect } = require('@playwright/test');
test.use({
geolocation: { longitude: 48.858455, latitude: 2.294474 },
permissions: ['geolocation'],
});
test('my test with geolocation', async ({ page, context }) => {
// 覆盖此测试的位置
context.setGeolocation({ longitude: 29.979097, latitude: 31.134256 });
});
await context.setGeolocation({ longitude: 29.979097, latitude: 31.134256 });
注意 您只能更改上下文中所有页面的地理位置。
配色方案和媒体
创建一个模拟用户 "colorScheme" 的测试。支持的值为 'light'、'dark'、'no-preference'。您还可以使用 page.emulateMedia([options]) 模拟媒体类型。
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
test.use({
colorScheme: 'dark' // or 'light'
});
test('my test with dark mode', async ({ page }) => {
// ...
});
const { test, expect } = require('@playwright/test');
test.use({
colorScheme: 'dark' // or 'light'
});
test('my test with dark mode', async ({ page }) => {
// ...
});
// 创建具有深色模式的上下文
const context = await browser.newContext({
colorScheme: 'dark' // or 'light'
});
// 创建具有深色模式的页面
const page = await browser.newPage({
colorScheme: 'dark' // or 'light'
});
// 更改页面的配色方案
await page.emulateMedia({ colorScheme: 'dark' });
// 更改页面的媒体
await page.emulateMedia({ media: 'print' });
用户代理
用户代理包含在设备中,因此您很少需要更改它,但是如果您确实需要测试不同的用户代理,可以使用 userAgent 属性覆盖它。
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
test.use({ userAgent: 'My user agent'});
test('my user agent test', async ({ page }) => {
// ...
});
const { test, expect } = require('@playwright/test');
test.use({ userAgent: 'My user agent' });
test('my user agent test', async ({ page }) => {
// ...
});
const context = await browser.newContext({
userAgent: 'My user agent'
});
启用 JavaScript
模拟禁用 JavaScript 的用户场景。
- TypeScript
- JavaScript
- Library
import { test, expect } from '@playwright/test';
test.use({ javaScriptEnabled: false });
test('test with no JavaScript', async ({ page }) => {
// ...
});
const { test, expect } = require('@playwright/test');
test.use({ javaScriptEnabled: false });
test('test with no JavaScript', async ({ page }) => {
// ...
});
const context = await browser.newContext({
javaScriptEnabled: false
});