2016-10-04 5 views
0

私はスクリーンショットの問題があります。私がスクリーンをキャプチャしているとき、それはVisibleスクリーンだけを取ります。私はページ全体をキャプチャしたい。以下は私のコードです。以下はselenium webdriverのスクリーンショットの問題

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

使用しているドライバ(ブラウザ)はどれですか? –

+0

mozillaを使用しています – naveen

答えて

0

はTestNGのコードとセレンは、Googleのページのスクリーンショット

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

あなたはMavenを使用する場合、あなたはあなたのタスクを達成するためにAShotを使用することができます。あなたはashotジャー(バージョンをダウンロードし、次にMavenを使用していない場合、

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

hiボス意味は何ですかViewportPastStrategy(1000) – naveen

+0

これはコンストラクタです。可能であれば、分割されたスクリーンショットを1つのスクリーンショットにマージするために使用されます。パラメータは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(); 
} 

です。これがあなたを助けることを願っています。