2016-10-04 6 views
0

스크린 샷 문제가 있습니다. 화면을 캡처 할 때는 Visible 화면 만 사용합니다. 전체 페이지를 캡처하고 싶습니다. 아래는 내 코드입니다. 다음은selenium webdriver의 스크린 샷 문제

WebDriver webDriver=getCurrentWebDriver(); 
WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver); 
WebDriverGUIUtility.captureScreenShot(webDriver); 
+0

사용중인 드라이버 (브라우저)는 무엇입니까? –

+0

모질라를 사용하고 있습니다 – naveen

답변

0

는 TestNG의 코드와 셀레늄이 구글 페이지의 스크린 샷

public class ScreenShot { 

    public WebDriver d; 
    Logger log; 
    @Test 
    public void m1() throws Exception 
    { 
     try 
     { 
      d=new FirefoxDriver(); 
      d.manage().window().maximize(); 
      d.get("https://www.google.co.in/?gfe_rd=cr&ei=4caQV6fxNafnugTjpIGADg"); 
      String pagetitle=d.getTitle(); 
      log = Logger.getLogger(FirefoxDriver.class.getName()); 
      log.info("logger is launched.."); 
      log.info("Title name : "+pagetitle); 
      d.findElement(By.id("testing")).sendKeys("test"); 
      d.findElement(By.cssSelector("input.gsfi")).sendKeys("gmail account"); 
     } 
     catch(Exception e) 
     { 
      System.out.println("something happened, look into screenshot.."); 
      screenShot(); 
     } 
    } 
    public void screenShot() throws Exception 
    { 
     Files.deleteIfExists(Paths.get("G:\\"+"Test results.png")); 
     System.out.println("previous pics deleted..."); 
     File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE); 
     FileUtils.copyFile(scrFile,new File("G:\\"+"Test results.png")); 

    } 

을} 걸릴 것입니다

1

당신이 받는다는을 사용하는 경우 다음 당신은 당신의 작업을 수행하는 AShot를 사용할 수 있습니다. 당신이 다음 ashot 항아리를 다운로드 받는다는을 사용하지 않는 경우, (버전

Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(augmentedDriver); 
ImageIO.write(screenshot.getImage(), "PNG", new File("d:\\tmp\\results.png")); 

을하지만, 다음과 같이

<dependency> 
    <groupId>ru.yandex.qatools.ashot</groupId> 
    <artifactId>ashot</artifactId> 
    <version>1.5.2</version> 
</dependency> 

그리고 코드를 사용이를 위해 당신은 당신의 치어 파일에 종속성을 추가 할 필요가 : 1.5.2) 파일을 만들고 빌드 경로에 추가하십시오. 여기에 링크를 당신의 도움이있다 : https://javalibs.com/artifact/ru.yandex.qatools.ashot/ashot

희망, 그것은 당신을 도울 수 있습니다.

+0

안녕하세요 보스 의미는 무엇입니까 ViewportPastingStrategy (1000) – naveen

+0

이것은 생성자입니다. 아마도 그것은 분할 된 스크린 샷을 단일 스크린 샷으로 병합하는 데 사용되며 매개 변수는 scrollTimeout입니다. –

1

@naveen 일반적으로 Chrome 브라우저에서 발생합니다. ChromeDriver는 보이는 부분의 스크린 샷을 찍을 수 있습니다. 따라서 여기서는 Java 스크립트 실행 프로그램을 사용하여 페이지를 스크롤하여 여러 이미지를 가져온 다음 단일 이미지에 결합합니다. FirefoxDriver는 문제없이 전체 화면의 이미지를 찍을 수 있습니다. 여기에 당신이 도움이 here를 찾을 수있는 모든 이미지 파일을 결합하는 예

@Test(enabled=true) 
public void screenShotExample() throws IOException{ 
    //WebDriver driver = new FirefoxDriver(); 
    System.setProperty("webdriver.chrome.driver", "yourpath to chromeDriver\\chromedriver.exe"); 
    WebDriver driver = new ChromeDriver(); 
    driver.get("http://www.w3schools.com/"); 
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); 
    driver.manage().window().maximize(); 
    JavascriptExecutor jexec = (JavascriptExecutor)driver; 
    jexec.executeScript("window.scrollTo(0,0)"); // will scroll to (0,0) position 
    boolean isScrollBarPresent = (boolean)jexec.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight"); 
    long scrollHeight = (long)jexec.executeScript("return document.documentElement.scrollHeight"); 
    long clientHeight = (long)jexec.executeScript("return document.documentElement.clientHeight"); 
    int fileIndex = 1; 
    if(driver instanceof ChromeDriver){ 
     if(isScrollBarPresent){ 
      while(scrollHeight > 0){ 
       File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
       org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg")); 
       jexec.executeScript("window.scrollTo(0,"+clientHeight*fileIndex++ +")"); 
       scrollHeight = scrollHeight - clientHeight; 
      } 
     }else{ 
      File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
      org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg")); 
     } 
    }else{ 
     File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
     org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg")); 
    } 
    // Combine all the .jpg file to single file 

    driver.close(); 
    driver.quit(); 
} 

입니다. 희망이 당신을 도울 것입니다.