Screen shot issue in selenium webdriver

前端 未结 3 838
执念已碎
执念已碎 2020-12-22 04:06

I have a screen shot issue. When I am capturing a screen, it takes only the Visible screen. I want to capture the entire page. Here below is my code.

WebDriv         


        
相关标签:
3条回答
  • @naveen, generally it happens with Chrome Browser. ChromeDriver is able to take screen shot of visible part. So the concept here is scroll through the page using Java script executor and take multiple images, then combining them to a single image. FirefoxDriver is able to take image of whole screen without issue. Here is an example

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

    To combine all image file you will find some help here. Hope this will help you.

    0 讨论(0)
  • 2020-12-22 04:18

    Below is selenium with testng code to take 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"));
    
        }
    

    }

    0 讨论(0)
  • 2020-12-22 04:28

    If you use maven then you may use AShot to accomplish your task. For this you need to add dependency in your pom file:

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

    And use the code snippet as follows:

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

    But, if you aren't using maven then download ashot jar (version: 1.5.2) file and add it to your build path. Here's the link your help: https://javalibs.com/artifact/ru.yandex.qatools.ashot/ashot

    Hope, that might help you.

    0 讨论(0)
提交回复
热议问题