
In this Blog, we are
going to learn how we can take a full-page screenshot in Selenium Using Third-party utility
known as ‘aShot’.
Before Going Further
Let's Understand What is aShot.
aShot is a third party web
driver screenshot utility used for:
Ø Takes a screenshot of a WebElement on different platforms
(i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser)
Ø Decorates screenshots
Ø Provides flexible screenshot comparison
Please Visit aShot official page for more details.
Below Steps, we need to
follow to use aShot in our Project.
Ø Download aShot Jar or Maven dependency using this link.
The dependency that I am using for implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot --> | |
<dependency> | |
<groupId>ru.yandex.qatools.ashot</groupId> | |
<artifactId>ashot</artifactId> | |
<version>1.5.3</version> | |
</dependency> |
Ø Please refer below code snippet for implementation details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package practiceTest; | |
import java.io.File; | |
import java.util.concurrent.TimeUnit; | |
import javax.imageio.ImageIO; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import ru.yandex.qatools.ashot.AShot; | |
import ru.yandex.qatools.ashot.Screenshot; | |
import ru.yandex.qatools.ashot.shooting.ShootingStrategies; | |
public class FullPageScreenshot | |
{ | |
public static void main(String args[]) throws Exception{ | |
System.setProperty("webdriver.chrome.driver","D://Chrome Driver 2.38//chromedriver_win32//chromedriver.exe"); | |
WebDriver driver = new ChromeDriver(); | |
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); | |
driver.manage().window().maximize(); | |
driver.get("https://www.amazon.com"); | |
Thread.sleep(2000); | |
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver); | |
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D://Screen//FullPageScreenshot.png")); | |
driver.quit(); | |
} | |
} |
After executing the above code result will look something like:
Below is the code
explanation:
ShootingStrategiesà It is a built-in strategy for different use cases. In case
there are no suitable strategies it is possible to build it using existing
strategies.
viewportPasting àIt will scroll viewport while shooting this
1000 (Configurable
value)àscrollTimeout time between viewportPasting
scrolls in milliseconds.
takeScreenshot àTakes the screenshot of the whole page.
ImageIOà A class containing static convenience methods for locating ImageReaders
and ImageWriters, and performing simple encoding and decoding.
WriteàWrites an image using an arbitrary ImageWriter that supports
the given format to a File. If there is already a File present, its contents
are discarded.
D://Screen//FullPageScreenshot.pngàPath where we want to Store Screenshot
Happy Learning J
Hello,
ReplyI am using following code;
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
I am getting exception;
java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.toString(Ljava/io/InputStream;Ljava/nio/charset/Charset;)Ljava/lang/String;
I have added following required dependency;
commons-io
commons-io
2.6
Can you please help me to resolve this? Thanks
I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it.. Page Unresponsive
Reply