selenium webdriver中的屏幕截图问题

时间:2016-10-04 04:52:02

标签: selenium

我有一个屏幕截图问题。当我捕获屏幕时,它只需要可见屏幕。我想捕获整个页面。以下是我的代码。

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

3 个答案:

答案 0 :(得分:2)

如果您使用maven,那么您可以使用AShot完成您的任务。为此,您需要在pom文件中添加依赖项:

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

并使用以下代码段:

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

但是,如果您没有使用maven,请下载ashot jar(版本:1.5.2)文件并将其添加到您的构建路径中。这是您的帮助链接: https://javalibs.com/artifact/ru.yandex.qatools.ashot/ashot

希望,这可能对你有帮助。

答案 1 :(得分:1)

@naveen,通常是Chrome浏览器。 ChromeDriver可以拍摄可见部分的屏幕截图。因此,这里的概念是使用Java脚本执行器滚动页面并拍摄多个图像,然后将它们组合到单个图像中。 FirefoxDriver可以毫无问题地拍摄整个屏幕的图像。这是一个例子

@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();
}

要合并所有图片文件,您会找到一些帮助here。希望这会对你有所帮助。

答案 2 :(得分:-1)

下面是带有testng代码的selenium,需要google page screenshot

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"));

    }

}