Selenium 테스트 작성 가이드 - dpTablo/spring-boot-template-reactive GitHub Wiki

application-tc.yml

src/test/resources 경로에 application-tc.yml 설정파일이 있습니다. spring profile 명이 ‘tc’ 인 경우에는 testcontainers 를 이용한 테스트 환경에 필요한 설정을 지정합니다.

예제코드와 설명

@ActiveProfiles("tc")
@Testcontainers
@ExtendWith(SeleniumTestSupportExtension.class)
public class SeleniumTest {
    @Test
    void test() throws MalformedURLException {
        var chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--single-process");
        chromeOptions.addArguments("--disable-dev-shm-usage");
        chromeOptions.addArguments("--log-level=3");
        chromeOptions.addArguments("--window-size=1920x1080");
        chromeOptions.addArguments("--disable-gpu");
        chromeOptions.addArguments("--disable-infobars");
        chromeOptions.addArguments("--disable-extensions");

        var seleniumUrl = System.getProperty("dptablo.selenium.url");
        assertThat(seleniumUrl).isNotNull();

        var remoteWebDriver = new RemoteWebDriver(new URL(seleniumUrl), chromeOptions);
        assertThat(remoteWebDriver).isNotNull();
    }
}

@ActiveProfiles("tc")

profile을 ‘tc’ 로 지정하여 테스트를 수행합니다.

@TestContainers

이 어노테이션이 지정되면testcontainers 구성요소가 초기화 됩니다.

@ExtendWith(SeleniumTestSupportExtension.class)

테스트를 수행하는데 필요한 처리를 담당하는 extension 클래스인 SeleniumTestSupportExtension 을 적용합니다. 테스트 케이스를 수행할 때마다 실행해야 하는 코드의 자동화를 담당합니다.

selenium/standalone-chrome 을 이용하여 chrome을 대상으로 구동되도록 구성되어 있습니다. 다른 브라우저로 테스트를 수행할 경우에는 SeleniumTestSupportExtension 클래스를 변경해야 합니다.

System.getProperty("dptablo.selenium.url")

SeleniumTestSupportExtension 클래스에 의해 테스트가 시작되면 system 프로퍼티에 실행되어진 selenium의 정보가 설정됩니다.

System.getProperty("dptablo.selenium.url") 을 통해 구동된 selenium 서비스의 요청 url 을 얻을 수 있습니다.