2021-04-01 16:35:26 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2024-11-08 17:18:51 -08:00
|
|
|
import type { Frame, Page, TestType, Locator } from '@playwright/test';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { PlatformWorkerFixtures } from '../config/platformFixtures';
|
2022-07-31 14:31:17 -07:00
|
|
|
import type { TestModeTestFixtures, TestModeWorkerFixtures, TestModeWorkerOptions } from '../config/testModeFixtures';
|
2021-10-28 07:31:30 -08:00
|
|
|
import { androidTest } from '../android/androidTest';
|
|
|
|
import { browserTest } from '../config/browserTest';
|
|
|
|
import { electronTest } from '../electron/electronTest';
|
2022-09-07 20:09:22 +02:00
|
|
|
import { webView2Test } from '../webview2/webView2Test';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { PageTestFixtures, PageWorkerFixtures } from './pageTestApi';
|
|
|
|
import type { ServerFixtures, ServerWorkerOptions } from '../config/serverFixtures';
|
2025-05-08 13:04:15 -07:00
|
|
|
import { expect as baseExpect } from '@playwright/test';
|
2021-04-01 16:35:26 -07:00
|
|
|
|
2022-07-31 14:31:17 -07:00
|
|
|
let impl: TestType<PageTestFixtures & ServerFixtures & TestModeTestFixtures, PageWorkerFixtures & PlatformWorkerFixtures & TestModeWorkerFixtures & TestModeWorkerOptions & ServerWorkerOptions> = browserTest;
|
2024-11-08 17:18:51 -08:00
|
|
|
export type BoundingBox = Awaited<ReturnType<Locator['boundingBox']>>;
|
2021-04-01 16:35:26 -07:00
|
|
|
|
2021-10-28 07:31:30 -08:00
|
|
|
if (process.env.PWPAGE_IMPL === 'android')
|
|
|
|
impl = androidTest;
|
|
|
|
if (process.env.PWPAGE_IMPL === 'electron')
|
|
|
|
impl = electronTest;
|
2022-09-07 20:09:22 +02:00
|
|
|
if (process.env.PWPAGE_IMPL === 'webview2')
|
|
|
|
impl = webView2Test;
|
2021-10-27 07:28:53 -08:00
|
|
|
|
2021-10-28 07:31:30 -08:00
|
|
|
export const test = impl;
|
2024-05-31 14:44:26 -07:00
|
|
|
|
|
|
|
export async function rafraf(target: Page | Frame, count = 1) {
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
await target.evaluate(async () => {
|
2025-03-25 13:49:28 +00:00
|
|
|
await new Promise(f => window.builtins.requestAnimationFrame(() => window.builtins.requestAnimationFrame(f)));
|
2024-05-31 14:44:26 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 17:18:51 -08:00
|
|
|
|
|
|
|
export function roundBox(box: BoundingBox): BoundingBox {
|
|
|
|
return {
|
|
|
|
x: Math.round(box.x),
|
|
|
|
y: Math.round(box.y),
|
|
|
|
width: Math.round(box.width),
|
|
|
|
height: Math.round(box.height),
|
|
|
|
};
|
|
|
|
}
|
2025-05-08 13:04:15 -07:00
|
|
|
|
|
|
|
export const expect = baseExpect.extend({
|
|
|
|
toContainYaml(received: string, expected: string) {
|
|
|
|
const trimmed = expected.split('\n').filter(a => !!a.trim());
|
|
|
|
const maxPrefixLength = Math.min(...trimmed.map(line => line.match(/^\s*/)[0].length));
|
|
|
|
const trimmedExpected = trimmed.map(line => line.substring(maxPrefixLength)).join('\n');
|
|
|
|
try {
|
|
|
|
if (this.isNot)
|
|
|
|
expect(received).not.toContain(trimmedExpected);
|
|
|
|
else
|
|
|
|
expect(received).toContain(trimmedExpected);
|
|
|
|
return {
|
|
|
|
pass: !this.isNot,
|
|
|
|
message: () => '',
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
pass: this.isNot,
|
|
|
|
message: () => e.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|