playwright
playwright
POM (Page Object Model)
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,
},
]);
}
}
ETC
evaluate :
display: none
인 요소는 playwright 통해서 이벤트가 트리거가 제데로 되지 않는데 그럴 때 사용
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'
);
playwright 동작상 딜레이에 따라 동작이 달라질 수 있다.
처음 disabled되고 나중 결과도 disabled 되어야하는 경우에는 동작이 전파되기 전에 playwright이 이미 통과됬다고 판단
가능하다면 항상 변화를 대기했다가 감지하는 방식으로 테스트를 짜자.
async setUpValidation() {
const title = faker.string.sample(2);
const fileName = imgFileName;
await this.fillForm({
title,
fileName,
});
await expect(this.getSumbit).toBeEnabled();
}
uploadFile
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;
}
strictHaveUrl
async strictHaveUrl(relative: string) {
const reg = new RegExp(`^${this.baseUrl}${relative}$`);
await expect(this.page).toHaveURL(reg);
}
signIn
async signIn(authorization: string) {
await this.context.addCookies([
{
name: 'authorization',
value: authorization,
url: this.baseUrl,
},
]);
}
꿀팁
Last updated