🧪 Selenium · 레슨 5 / 6

Selenium Grid — Docker로 Standalone·Hub/Node 구성, RemoteWebDriver, 병렬 실행

Selenium Grid의 구성 요소(Router, Distributor, Session Map, Node)와 Standalone·Hub/Node 모드, docker-selenium 이미지로 Chrome·Firefox·Edge 노드를 띄우는 docker compose 구성, RemoteWebDriver로 접속하기, noVNC로 실행 화면 보기, pytest-xdist 병렬 실행과 세션 슬롯 조정을 배웁니다.

⏱ 약 5분🗓 2026-11-13 업데이트🏷 Selenium 4.x 기준

이 레슨에서 배우는 것

  • Grid의 구성 요소와 실행 모드 이해하기
  • Docker Compose로 여러 브라우저 노드를 띄우고 접속하기
  • 병렬 실행 수를 Grid 슬롯과 맞추기

Grid가 필요한 순간

테스트가 300개이고 하나에 20초라면 순서대로 100분입니다. Selenium Grid는 여러 머신(또는 컨테이너)의 브라우저를 하나의 주소로 묶어, 테스트 코드가 원격으로 브라우저 세션을 요청하면 비어 있는 노드에 배정해 줍니다. 덕분에

구성 요소와 모드

구성 요소역할
Router모든 요청의 입구 (기본 4444 포트)
Distributor새 세션 요청을 조건(브라우저·OS)에 맞는 노드에 배정
Session Map세션 ID → 노드 위치 기록
New Session Queue빈 슬롯이 없을 때 요청을 대기열에 보관
Event Bus구성 요소 간 메시지
Node실제 브라우저를 띄워 명령 실행. 슬롯 수만큼 동시 세션
모드구성용도
Standalone모든 구성 요소 + 노드가 한 프로세스로컬, 작은 CI
Hub and NodeHub(라우터·배정 등) 하나 + 노드 여러 개일반적인 팀 Grid
Distributed구성 요소를 각각 별도 프로세스로대규모, Kubernetes

가장 빠른 시작: Standalone 컨테이너

bash
docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:latest

운영 Grid에서는 latest 대신 브라우저·Selenium 버전이 고정된 태그를 쓰세요.

RemoteWebDriver로 접속

로컬 드라이버 대신 Grid 주소로 원격 세션을 만듭니다. 이후 코드는 완전히 같습니다.

python
from selenium import webdriver

options = webdriver.ChromeOptions()
options.set_capability("se:name", "test_submit_form")   # Grid UI에 표시될 이름
driver = webdriver.Remote(command_executor="http://localhost:4444", options=options)
try:
    driver.get("https://www.selenium.dev/selenium/web/web-form.html")
    print(driver.title)
finally:
    driver.quit()          # 원격에서는 더더욱 중요: 안 하면 슬롯이 타임아웃까지 묶임
Javajava
ChromeOptions options = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), options);

4강의 픽스처에 --grid-url 옵션을 추가해, 값이 있으면 webdriver.Remote, 없으면 로컬 드라이버를 만들도록 하면 같은 테스트를 로컬과 Grid에서 모두 돌릴 수 있습니다.

conftest.py (일부)python
grid = request.config.getoption("--grid-url")
drv = webdriver.Remote(command_executor=grid, options=options) if grid else webdriver.Chrome(options=options)
💡
원격 파일 업로드

Grid 노드는 테스트 머신의 파일에 접근할 수 없습니다. driver.file_detector = LocalFileDetector()(selenium.webdriver.remote.file_detector)를 설정하면 send_keys(경로) 시 파일을 노드로 전송해 줍니다.

Hub와 여러 브라우저 노드

docker-compose.ymlyaml
services:
  selenium-hub:
    image: selenium/hub:latest
    ports:
      - "4442:4442"     # Event Bus publish
      - "4443:4443"     # Event Bus subscribe
      - "4444:4444"     # Router

  chrome:
    image: selenium/node-chrome:latest
    shm_size: 2gb
    depends_on: [selenium-hub]
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_NODE_MAX_SESSIONS=2              # 노드당 동시 세션 수
      - SE_NODE_OVERRIDE_MAX_SESSIONS=true
    deploy:
      replicas: 2                           # 크롬 노드 2개 → 크롬 슬롯 4개

  firefox:
    image: selenium/node-firefox:latest
    shm_size: 2gb
    depends_on: [selenium-hub]
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub

  edge:
    image: selenium/node-edge:latest
    shm_size: 2gb
    depends_on: [selenium-hub]
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
bash
docker compose up -d
docker compose up -d --scale chrome=4     # 크롬 노드 늘리기
docker compose down

노드는 기본적으로 CPU 코어 수에 맞춰 슬롯을 정하지만, 컨테이너당 1~2개로 두는 편이 안정적입니다. 브라우저 세션 하나가 대략 CPU 1코어·메모리 1GB 안팎을 쓴다고 보고 호스트 자원에 맞춰 노드 수를 정하세요.

병렬 실행

Grid는 세션을 받아 줄 뿐이고, 동시에 여러 세션을 요청하는 것은 테스트 러너의 몫입니다.

bash
pip install pytest-xdist
pytest -n 4 --grid-url http://localhost:4444            # 워커 4개 = 동시 세션 4개
pytest -n auto --browser firefox --grid-url http://localhost:4444
언어병렬 실행 방법
Pythonpytest-xdist의 -n
Java JUnit 5junit-platform.properties에 junit.jupiter.execution.parallel.enabled=true
Java TestNGtestng.xml의 parallel="methods" thread-count="4"
C#NUnit [Parallelizable]
⚠️
워커 수 ≤ Grid 슬롯 수

워커가 슬롯보다 많으면 남는 요청은 대기열에서 기다리다 SE_SESSION_REQUEST_TIMEOUT(기본 300초)을 넘기면 실패합니다. 그리고 병렬 실행은 테스트끼리 데이터를 공유하지 않을 때만 안전합니다. 같은 계정으로 장바구니를 동시에 수정하는 테스트는 서로를 깨뜨립니다.

더 큰 규모

정리