Java에서 selenium 배포에서 캡처 저장
목차
selenium이란?
목표
준비
도입 절차
테스트 케이스
테스트 케이스 실행
selenium이란?
Selenium은 브라우저의 자동화 도구입니다.
자동으로 브라우저를 조작하는 것으로 Web 사이트의 동작의 테스트등이 실시할 수 있습니다.
목표
자바 selenium 소개, 크롬 열기 → 야후 뉴스로 전환 → 캡처까지.
준비
Java 프로젝트 작성
프로젝트 이름 "selenium"
selenium DL
Java selenium version3.141.59
Google 크롬 DL
최신 버전
Chromedriver DL
GooglChrome과 동일한 버전의 것
JUnit DL
Download and install에서 junit.jar, hamcrest-core.jar의 최신 jar 버전을 다운로드하십시오.
도입 절차
1.selenium 프로젝트 바로 아래에 lib 폴더와 exe 폴더 만들기 만들기

2. 다운로드한 selenium-java-3.141.59.zip 내의 libs 폴더내의 .jar 파일 전부와 client-combined-3.141.59.jar를 작성한 lib 폴더에 카피한다.
3. lib 폴더에 복사한 jar 파일을 클래스 경로에 추가
selenium 프로젝트의 속성에서 Java 빌드 경로를 추가합니다.


4. 다운로드한 Chromedriver.exe를 만든 exe 폴더에 복사합니다.
이것으로 준비 완료입니다.
테스트 케이스
실제로 움직여갑니다.
이 근처를 참고로 하면 대체로 움직일 수 있다.
htps : // 이 m/ゔぁ_나카츠/있어 ms/0095755dc48
htps : // 이 m / 떡 / ms / dc9935 예 607895420186
프로젝트 구성

· ChromeTest.java
메인 처리 부분.
TestSetting.java를 상속받습니다.
selenium에서는 Javascript가 사용 가능.
package test.selenium;
import java.io.IOException;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;
import util.TestUtils;
public class ChromeTest extends TestSetting {
/**
* テスト実行処理
*
* @throws WebDriverException
* @throws IOException
*/
@Test
public void testChrome() throws WebDriverException, IOException{
// キャプチャ保存フォルダを作成
String path = TestUtils.mkdir(this.capturePath, "保存フォルダ");
// YAHOOニュースページに遷移
this.driver.get("https://news.yahoo.co.jp/");
// ITニュース一覧画面へ遷移
this.driver.findElement(By.cssSelector("#snavi > ul.yjnHeader_sub_cat > li:nth-child(7)")).click();
// 画面をキャプチャする
TestUtils.screenShot(this.driver, path, "スクショ");
// 画面を下へ移動
JavascriptExecutor js = (JavascriptExecutor) this.driver;
js.executeScript("window.scrollBy(0,3000)");
// 画面をキャプチャする
TestUtils.screenShot(this.driver, path, "画面移動後");
}
}
· TestSetting.java
테스트를 실행할 때 처음 작동하고 캡처 저장 폴더를 만들고 Chrome을 열어줍니다.
package test.selenium;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import util.TestUtils;
public class TestSetting {
// キャプチャ用ファイル配置先
protected String capturePath;
protected WebDriver driver;
// テスト実行前動作
@Before
public void initSet() throws IOException{
// 実行ユーザーのデスクトップパスを取得する
String desktopPath = System.getProperty("user.home") + "\\Desktop";
// デスクトップにキャプチャ保存フォルダを作成
this.capturePath = TestUtils.mkdir(desktopPath, "キャプチャ保存フォルダ");
// Chromedriveのパスを通す
System.setProperty("webdriver.chrome.driver", "./exe/chromedriver.exe");
// ダウンロード先を指定のフォルダに変更する
Map<String, Object> dir = new HashMap<String, Object>();
dir.put("download.default_directory", this.capturePath);
ChromeOptions option = new ChromeOptions();
option.setExperimentalOption("dir", dir);
// chromeを起動
this.driver = new ChromeDriver();
// ウィンドウを最大化
this.driver.manage().window().maximize();
}
// テスト実行後動作
@After
public void yeild() throws IOException{
// chromeを閉じる
this.driver.quit();
}
}
· TestUtils.java
스크린샷 처리 및 캡처 폴더 작성 처리
package util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
public class TestUtils {
/**
* 表示部分をスクリーンショット
*
* @param driver
* @param path
* @param filename
* @throws IOException
*/
public static void screenShot(WebDriver driver, String path, String filename) throws IOException{
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.switchTo().defaultContent();
TakesScreenshot ts = (TakesScreenshot) new Augmenter().augment(driver);
Path from = Paths.get(ts.getScreenshotAs(OutputType.FILE).toURI());
Path to = Paths.get(path + "\\" + filename + ".png");
Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);
}
/**
* キャプチャ用のフォルダを指定の場所に作成
*
* @param dirpath
* @param dirname
* @return
* @throws IOException
*/
public static String mkdir(String dirpath, String dirname) throws IOException{
String path = Paths.get(dirpath, dirname).toString();
if(Files.notExists(Paths.get(dirpath, dirname))) {
Files.createDirectories(Paths.get(dirpath, dirname));
}
return path;
}
}
테스트 케이스 실행
ChromeTest.java를 「실행」->「JUnit 테스트」로부터 실행하면 Chrome이 기동해 데스크탑상에 캡쳐 보존 폴더가 만들어져 캡쳐한 것이 보존된다.

Reference
이 문제에 관하여(Java에서 selenium 배포에서 캡처 저장), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/imuimu/items/8a8ffa1d2aa876f52444텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)