mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.3 KiB
82 lines
2.3 KiB
2 years ago
|
import { Locator, Page } from '@playwright/test';
|
||
2 years ago
|
|
||
2 years ago
|
type ResponseSelector = (json: any) => boolean;
|
||
|
|
||
2 years ago
|
export default abstract class BasePage {
|
||
|
readonly rootPage: Page;
|
||
2 years ago
|
|
||
2 years ago
|
abstract get(args?: any): Locator;
|
||
2 years ago
|
|
||
|
constructor(rootPage: Page) {
|
||
|
this.rootPage = rootPage;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
async verifyToast({ message }: { message: string }) {
|
||
2 years ago
|
await this.rootPage.locator('.ant-message .ant-message-notice-content', { hasText: message }).last().isVisible();
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async waitForResponse({
|
||
2 years ago
|
uiAction,
|
||
|
httpMethodsToMatch = [],
|
||
2 years ago
|
requestUrlPathToMatch,
|
||
|
responseJsonMatcher,
|
||
|
}: {
|
||
2 years ago
|
uiAction: Promise<any>;
|
||
2 years ago
|
requestUrlPathToMatch: string;
|
||
2 years ago
|
httpMethodsToMatch?: string[];
|
||
2 years ago
|
responseJsonMatcher?: ResponseSelector;
|
||
|
}) {
|
||
2 years ago
|
await Promise.all([
|
||
2 years ago
|
this.rootPage.waitForResponse(async res => {
|
||
2 years ago
|
let isResJsonMatched = true;
|
||
2 years ago
|
if (responseJsonMatcher) {
|
||
2 years ago
|
try {
|
||
|
isResJsonMatched = responseJsonMatcher(await res.json());
|
||
|
} catch (e) {
|
||
|
return false;
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
return (
|
||
|
res.request().url().includes(requestUrlPathToMatch) &&
|
||
|
httpMethodsToMatch.includes(res.request().method()) &&
|
||
|
isResJsonMatched
|
||
2 years ago
|
);
|
||
2 years ago
|
}),
|
||
|
uiAction,
|
||
|
]);
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async attachFile({ filePickUIAction, filePath }: { filePickUIAction: Promise<any>; filePath: string }) {
|
||
2 years ago
|
const [fileChooser] = await Promise.all([
|
||
|
// It is important to call waitForEvent before click to set up waiting.
|
||
|
this.rootPage.waitForEvent('filechooser'),
|
||
|
// Opens the file chooser.
|
||
|
filePickUIAction,
|
||
|
]);
|
||
|
await fileChooser.setFiles(filePath);
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async downloadAndGetFile({ downloadUIAction }: { downloadUIAction: Promise<any> }) {
|
||
|
const [download] = await Promise.all([
|
||
2 years ago
|
// It is important to call waitForEvent before click to set up waiting.
|
||
|
this.rootPage.waitForEvent('download'),
|
||
|
// Triggers the download.
|
||
|
downloadUIAction,
|
||
|
]);
|
||
|
// wait for download to complete
|
||
2 years ago
|
if (await download.failure()) {
|
||
2 years ago
|
throw new Error('Download failed');
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const file = await download.createReadStream();
|
||
|
const data = await new Promise((resolve, reject) => {
|
||
|
let data = '';
|
||
2 years ago
|
file?.on('data', chunk => (data += chunk));
|
||
2 years ago
|
file?.on('end', () => resolve(data));
|
||
|
file?.on('error', reject);
|
||
|
});
|
||
|
return data as any;
|
||
|
}
|
||
2 years ago
|
}
|