Tuesday, 17 May 2016

Get entire page screenshot in selenium


 - Download the ashot.jar file

package linkedIn_Interview;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.OutputType.*;

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.screentaker.ViewportPastingStrategy;
public class Question1 {

    static WebDriver d;
    @BeforeMethod
    public void setUp()
    {
        System.setProperty("webdriver.chrome.driver", "D:\\selenium_NEW_PRACTICE\\jars\\chromedriver_win32\\chromedriver.exe");
        d=new ChromeDriver();
        d.manage().window().maximize();
    }
    @Test
    public void verifyLinkedInExam() throws InterruptedException, IOException
    {       
        d.get("http://www.ia.ca");
        getScreenshot();  //add this method at where u want take screenshot
        //take the screenshot of the entire home page and save it to a png file
        //Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(d);
        //ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\selenium_NEW_PRACTICE\\screenshots\\home"+System.currentTimeMillis()+".png"));
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
           @Test
    public void getScreenshot()
    {
        //take the screenshot of the entire home page and save it to a png file
                Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(d);
                try
                {
                    ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\selenium_NEW_PRACTICE\\screenshots\\home"+System.currentTimeMillis()+".png"));
                }
                catch(IOException e)
                {
                    System.out.println(e.getMessage());
                   
                }
    }
   
    @AfterMethod
    public void tearDown()
    {
        d.close();
        System.out.println("closing bowser : ");
    }
    }

No comments:

Post a Comment