UI 자동화 테스트를 실행하는 동안 검사가 실패하면 인쇄 된 로그가 문제를 효과적으로 찾는 데 도움이되지 않는다는 것을 알게됩니다. 당시 실패 장면을 재현하지 못한 순간의 스크린 샷이 필요합니다. 그런 다음 오류의 원인을 문제 해결해야합니다.
이 요구 사항에 따라 Selenium의 스크린 샷 기능을 사용할 수 있습니다.
구현 코드는 다음과 같습니다.
import java.io.file; import java.io.ioexception; import org.apache.commons.io.fileutils; import org.apache.commons.lang3.time.dateutils; import org.openqa.selenium.outputtype; import org.openqa.selenium.takesscreenshot; org.openqa.selenium.webdriver; import org.openqa.selenium.interactions.actions; public static void takescreeshot (String screenpath, webdriver chrome) {try {// outputtype.file을 getscreenshotas () 메서드로 매개 변수로 지정하여 화면을 파일로 반환합니다. 파일 scrfile = ((takesscreenshot) chrome) .getScreenshotas (outputtype.file); // 키 코드, 스크린 샷을 실행하여 기본적으로 스크린 샷을 임시 디렉토리 파일 utils.copyFile (scrfile, new File (ScreenPath))에 저장합니다. // fileUtils 도구 클래스의 CopyFile () 메소드를 사용하여 getScreenshotas ()가 반환 한 파일 객체를 저장합니다. } catch (ioexception e) {System.out.println ( "스크린 샷 오류 :" + screenpath); System.out.println ( "이 오류는 스크린 샷에서 볼 수 있습니다 :"+screenpath); } catch (Exception E) {// todo : handing exception}} public static void takesCreenshot (WebDriver Chrome, String imgname) {String Screename = imgname+dateUtils.millis_per_day+". jpg"; 문자열 filestring = "d : // selenium // schoolpalerp_qtp // image"; if (! // 존재하지 않으면 새 디렉토리를 만듭니다} 파일 dir = 새 파일 (filestring); if (! dir.exists ()) dir.mkdirs (); 문자열 screenpath = dir.getabsolutepath () + "//" + Screenname; TakesCreeshot (ScreenPath, Chrome); }Selenium의 UI 자동화 테스트의 스크린 샷 기능에 대한 위의 예제 코드는 내가 공유하는 모든 컨텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.