Playwrightμμ μ μ§λ³΄μμ±, κ°λ
μ±, μ¬μ¬μ©μ±μ λμ΄λ λμμΈ ν¨ν΄
import type { Locator, Page } from '@playwright/test';
import { BrowserContext, expect} from '@playwright/test';
export class BaseHelper {
readonly page: Page;
readonly context: BrowserContext;
readonly baseUrl: string;
constructor(page: Page, context: BrowserContext) {
this.page = page;
this.context = context;
this.baseUrl = process.env.NEXT_PUBLIC_WEB_BASE_URL;
}
async downloadImage() {
await this.page.getByRole('button', { name: 'download' }).click();
await expect(this.page.getByText('success')).toBeVisible();
}
async signIn(authorization: string) {
await this.context.addCookies([
{
name: 'authorization',
value: authorization,
url: this.baseUrl,
},
]);
}
}
const helper = new Helper(page, context);
const setFile = helper.uploadFile();
await helper.getThumbnail.evaluate((el: HTMLInputElement) =>
el.click()
);
await setFile(imgFileName);
await expect(helper.getThumbnailSrc).not.toHaveAttribute(
'src',
'/file.svg'
);
async setUpValidation() {
const title = faker.string.sample(2);
const fileName = imgFileName;
await this.fillForm({
title,
fileName,
});
await expect(this.getSumbit).toBeEnabled();
}
uploadFile() {
const fileChooserPromise = this.page.waitForEvent('filechooser');
const setFile = async (fileName: string) => {
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(path.join('__tests__', 'fixtures', fileName));
};
return setFile;
}
async strictHaveUrl(relative: string) {
const reg = new RegExp(`^${this.baseUrl}${relative}$`);
await expect(this.page).toHaveURL(reg);
}
async signIn(authorization: string) {
await this.context.addCookies([
{
name: 'authorization',
value: authorization,
url: this.baseUrl,
},
]);
}