Playwright 소개
Selenium
을 사용해 봤었는데 비슷한 라이브러리로 Playwright
가 있다고 추천받아 살펴보았다.
Playwright
는 Microsoft에서 만든 end-to-end 테스트를 위해 사용되는 라이브러리인데 Chromium
, WebKit
, Firefox
를 포함한 모든 최신 rendering engine을 지원하고 Windows
, Linux
및 MacOS
에서 로컬 또는 CI에서 headless 또는 native mobile emulation을 사용한 headed 테스트를 지원한다.
Java
, Node.js
, Python
, .Net
중 원하는 언어를 선택해 사용하면 된다.
내 경우 Java에 익숙하여 Java에서 사용해 보았다.
구경해 보기
내 경우 Spring Boot
에 익숙해서 Spring Boot 환경에서 테스트해 보았다.
dependency 설정
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/>
</parent>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.51.0</version>
</dependency>
</dependencies>
java test code로 실행
간단한 예제는 다음과 같다.
@Slf4j
class AppTest {
@Test
void test() {
log.debug("test ");
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
page.navigate("https://playwright.dev");
while (true) {
// System.out.println(page.title());
}
}
}
}
Playwright
는 default로 browser를 headless mode로 실행한다.
browser UI를 보려면 setHeadless
option을 false
로 설정한다.
처음 실행하면 알아서 chromium을 환경에 맞게 다운로드한 후 실행한다.
2025-03-29T00:03:28.493+09:00 DEBUG --- [ main] i.g.luversof.study.playwright.AppTest : test
Downloading Chromium 134.0.6998.35 (playwright build v1161)[2m from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1161/chromium-win64.zip[22m
| | 0% of 141.8 MiB
|■■■■■■■■ | 10% of 141.8 MiB
|■■■■■■■■■■■■■■■■ | 20% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■ | 30% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 40% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 50% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 60% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 70% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 80% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 90% of 141.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 141.8 MiB
Chromium 134.0.6998.35 (playwright build v1161) downloaded to C:\Users\bluesky\AppData\Local\ms-playwright\chromium-1161
Downloading Chromium Headless Shell 134.0.6998.35 (playwright build v1161)[2m from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1161/chromium-headless-shell-win64.zip[22m
| | 0% of 87.8 MiB
|■■■■■■■■ | 10% of 87.8 MiB
|■■■■■■■■■■■■■■■■ | 20% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■ | 30% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 40% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 50% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 60% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 70% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 80% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 90% of 87.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 87.8 MiB
Chromium Headless Shell 134.0.6998.35 (playwright build v1161) downloaded to C:\Users\bluesky\AppData\Local\ms-playwright\chromium_headless_shell-1161
Downloading Firefox 135.0 (playwright build v1475)[2m from https://cdn.playwright.dev/dbazure/download/playwright/builds/firefox/1475/firefox-win64.zip[22m
| | 0% of 91.5 MiB
|■■■■■■■■ | 10% of 91.5 MiB
|■■■■■■■■■■■■■■■■ | 20% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■ | 30% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 40% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 50% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 60% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 70% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 80% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 90% of 91.5 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 91.5 MiB
Firefox 135.0 (playwright build v1475) downloaded to C:\Users\bluesky\AppData\Local\ms-playwright\firefox-1475
Downloading Webkit 18.4 (playwright build v2140)[2m from https://cdn.playwright.dev/dbazure/download/playwright/builds/webkit/2140/webkit-win64.zip[22m
| | 0% of 52.8 MiB
|■■■■■■■■ | 10% of 52.8 MiB
|■■■■■■■■■■■■■■■■ | 20% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■ | 30% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 40% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 50% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 60% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 70% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 80% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 90% of 52.8 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 52.8 MiB
Webkit 18.4 (playwright build v2140) downloaded to C:\Users\bluesky\AppData\Local\ms-playwright\webkit-2140
Downloading FFMPEG playwright build v1011[2m from https://cdn.playwright.dev/dbazure/download/playwright/builds/ffmpeg/1011/ffmpeg-win64.zip[22m
| | 0% of 1.3 MiB
|■■■■■■■■ | 10% of 1.3 MiB
|■■■■■■■■■■■■■■■■ | 20% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■ | 30% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 40% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 50% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 60% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 70% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 80% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 90% of 1.3 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 1.3 MiB
FFMPEG playwright build v1011 downloaded to C:\Users\bluesky\AppData\Local\ms-playwright\ffmpeg-1011
Downloading Winldd playwright build v1007[2m from https://cdn.playwright.dev/dbazure/download/playwright/builds/winldd/1007/winldd-win64.zip[22m
| | 1% of 0.1 MiB
|■■■■■■■■ | 10% of 0.1 MiB
|■■■■■■■■■■■■■■■■ | 24% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■ | 37% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 49% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 59% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 72% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 85% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ | 97% of 0.1 MiB
|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 0.1 MiB
Winldd playwright build v1007 downloaded to C:\Users\bluesky\AppData\Local\ms-playwright\winldd-1007
headless를 false로 하고 대기 상태로 실행하였기 때문에 brower가 뜨는 것을 확인할 수 있다.
일반 크롬 브라우저나 다른게 없다.
테스트 코드에서 테스트 수행 코드를 작성하면 위 브라우저에서 실행되는 것을 확인할 수 있다.
너무 빠르게 테스트가 진행된다면 setSlowMo
를 설정하여 실행 속도를 늦출 수도 있다.
playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
Codegen 사용
https://playwright.dev/java/docs/codegen
브라우저에서 수행할 동작을 일일이 작성하는 건 여간 귀찮은 일이 아니다.
또 어떻게 해야 할지 기능을 숙지해야 하는 부담감도 크다.
codegen
기능을 활용하면 편하다.
Selenium
의 경우 Selenium IDE
를 사용해 recording을 한 후 해당 기록을 참고해 실행 코드를 작성했었다.
Playwright
의 경우도 기능은 동일하지만 별도 IDE를 chrome plugin으로 설치할 필요 없이 대상 프로젝트에서 maven 명령 만으로 사용할 수 있다는 점이 장점이다.
maven으로 다음과 같이 실행하면 브라우저와 recording을 기록하는 inspector가 같이 실행된다.
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="codegen demo.playwright.dev/todomvc"
내 경우 cmd로 실행했다.
browser에서 수행하는 동작이 모두 inspector에 기록된다.
다음과 같은 언어를 inspector에서 선택해서 기록할 수 있다.
JUnit을 선택하고 예제 사이트에서 TODO를 입력해 보고 삭제를 실행하면 다음과 같이 기록이 되는 것을 확인할 수 있다.
import com.microsoft.playwright.junit.UsePlaywright;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.*;
import org.junit.jupiter.api.*;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;
@UsePlaywright
public class TestExample {
@Test
void test(Page page) {
page.navigate("https://demo.playwright.dev/todomvc/#/");
page.locator("html").click();
page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("What needs to be done?")).click();
page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("What needs to be done?")).click();
page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("What needs to be done?")).fill("테스트로 TO 입력");
page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("What needs to be done?")).press("Enter");
page.getByRole(AriaRole.CHECKBOX, new Page.GetByRoleOptions().setName("Toggle Todo")).check();
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Delete")).click();
}
}
이 코드를 예제 프로젝트로 옮겨 적당히 다듬고 실행하면 내가 수행했던 작업을 그대로 다시 재 실행하는 것을 확인할 수 있다.
Auto-waiting 기능
https://playwright.dev/java/docs/writing-tests
test code를 작성하면서 느낀 Selenium
과 가장 큰 차이점 중 하나는 바로 Playwright
가 제공하는 Auto-waiting
기능이다.
작업을 수행하기 전 element가 실행 가능해질 때까지 기다려주는 기능인데 Selenium을 사용할 땐 페이지가 렌더링 되고 javascript가 동작할 때까지 기다리기 위해 readyState를 체크하거나
new WebDriverWait(driver, Duration.ofSeconds(30)).until(ExpectedConditions.jsReturnsValue("return document.readyState==\"complete\""));
또는 클릭을 하기 위해서는 클릭이 가능한 상태가 될 때까지 기다리는 등
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(by));
동작마다 불편한 상태 대기 체크 과정이 필요했었다.
Playwright
는 이런 과정을 알아서 처리해 줘서 코드 작성이 매우 편했다.
그 외에 느낀 점으로는 Selenium
에 비해 확실히 가벼웠다. (약 54% 정도 빠르다고 하는데 여전히 무겁긴 하지만 확실히 체감이 되는 정도이다.)
https://www.zenrows.com/blog/playwright-vs-selenium#speed
다만 Selenium
의 경우 Selenium Grid
같은 더 풍부한 기능을 지원하긴 하는데 Playwright
도 Selenium Grid
을 사용할 수 있는 기능이 현재 experimental로 제공되고 있는 듯하다.
https://playwright.dev/java/docs/selenium-grid
Selenium
사용에 대해서는 이전 글을 참고하면 된다.
'Study > Java' 카테고리의 다른 글
Spring Boot ConfigurationMetaData 사용해 보기 (0) | 2025.03.28 |
---|---|
JDK 24 New Features (0) | 2025.03.26 |
Copilot4Eclipse 사용해 보기 (0) | 2025.01.28 |
Window 개발 환경에서 https로 Spring Boot Application 개발하기 (1) | 2025.01.04 |
Mybatis에서 custom MapTypeHandler 사용해 보기 (0) | 2024.12.29 |
ApplicationContextRunner에서 Condition Evaluation Report 확인하기 (0) | 2024.12.29 |
Spring Boot 3.4 Release Notes (0) | 2024.12.03 |
hibernate SqlType.JSON (json data) 사용해 보기 (0) | 2024.11.18 |
Spring Cloud Context의 @RefreshScope를 사용하여 properties 설정 갱신하기 (0) | 2024.11.17 |
springdoc swagger ui에 authorize 사용해 보기 (0) | 2024.11.07 |