Taking Full Page Screenshot in Selenium
Taking full page screenshot in selenium will discuss about how to take full page screenshot image in selenium webdriver while automating any application. As already we know how to take screenshot in selenium webdriver. But the actual issue is, when we take screenshot how much area will it capture. Here we need to discuss 2 things that how the methods which are available with Selenium webdriver will behave in Selenium 2 and Selenium 3 as recently selenium 3 is launched.
As already we know that in selenium 2; no extra drivers needed to launch the FirefoxDriver but for other browsers we need to use driver exe files to launch and work with. While working with the FirefoxDriver and try to take the screenshot then it will capture the entire page including the invisible area of the web page. But if you use other browsers then it will capture only the visible area of the webpage.
But coming to the Selenium 3, to launch any browser we need to use the driver exe files. So, while capturing the screenshot in selenium 3 it will capture only the visible area of the web page in all the browsers including firefox. To overcome this we can use third party utility called AShot. It is a WebDriver screenshot utility and by using this we can capture the entire page screenshot and can capture the individual webelement screenshot also.
For this, we need to download the ashot.jar file and add to the project along with the selenium jar files. We can download the ashot.jar file from here.
Below is the sample code to capture the entire page screenshot for any browser:
import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.shooting.ShootingStrategies; import javax.imageio.ImageIO; import java.io.File; public class TakeFullPageScreenShot { public static void main(String args[]) throws Exception { System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://automationtesting.in/"); Thread.sleep(2000); Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver); ImageIO.write(screenshot.getImage(),"PNG",new File(System.getProperty("user.dir") +"/ErrorScreenshots/FullPageScreenshot.png")); } }
In the above program 20,21 lines will do the magic. It will scroll the page till the end and captures the screenshot. And we can control the scroll speed using viewportPasting method.
This way we can capture the entire page screenshot using ashot.jar file.
Please watch the youtube video for better understanding.