WebElement를 활용하여 스크린샷 캡처

정태경·2022년 9월 18일
0
post-thumbnail

Selenium 4가 릴리즈되며 몇 가지 신규 기능들이 추가되었는데, 그중 하나 WebElement를 활용하여 스크린샷을 캡처하는 기능이다. 예를 들어, 인풋 필드에 sendKeys() 메서드를 활용하여 특정 키워드를 입력한 후 해당 WebElement를 캡처하여 실제 입력된 값을 확인하는 것이 가능하다. 물론 이전에도 스크린샷 캡처 기능이 있었지만, 브라우저 전체를 캡처하는 형태였기 때문에 활용도가 낮았었다.

이 기능을 통해 테스트 결과 리포트에 스크린샷을 캡처한다거나, 이전 테스트에 수행되었던 스크린샷과 비교해본다거나 다양한 용도로 활용해볼 수 있을 것 같다.

import java.io.File;
import java.io.IOException;
import java.time.Duration;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class Base {

    public static void main(String[] args) throws IOException {
        WebDriverManager.chromiumdriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5000));

        driver.get("https://www.myrealtrip.com");
        WebElement searchBar = driver.findElement(By.id("globalSearchInput"));
        searchBar.sendKeys("제주도");

		//WebElement를 활용하여 스크린샷 캡처
        File file= searchBar.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("searchBar.png"));
    }
}



profile
現 두나무 업비트 QA 엔지니어, 前 마이리얼트립 TQA 엔지니어

0개의 댓글