Webdriver for specific Web area screenshots in the _java

Source: Internet
Author: User

Students who have used Webdriver know that Webdriver can take screenshots of the pages in the browser. For example:

Public byte[] Takescreenshot () throws IOException {
 takesscreenshot takesscreenshot = (takesscreenshot) driver;
 Return Takesscreenshot.getscreenshotas (outputtype.bytes);
}


The resulting picture is the entire page. But sometimes we don't need the whole page, just some specific webelement to avoid some distractions.

Although Webdriver itself does not provide such an API, but we can do it on our own, that is, in a full-screen screenshot of the Cut, as follows:

Public BufferedImage createelementimage (webelement webelement) throws IOException
 {
 //Get webelement location and size. Point
 location = Webelement.getlocation ();
 Dimension size = Webelement.getsize ();
 Create a Full-screen screenshot.
 BufferedImage originalimage =
  Imageio.read (New Bytearrayinputstream (Takescreenshot ()));
 Intercepts the webelement of the location of the image.
 bufferedimage croppedimage = originalimage.getsubimage (
  location.getx (),
  localtion.gety (),
  Size.getwidth (),
  size.getheight ());
 return croppedimage;
}


Among them ImageIO and BufferedImage from Javax.imageio and Java.awt.image respectively.

This can be a good way to cut the size of screenshots, but also to avoid some uncertainties (such as date and time, etc.), is the test to save the screenshot in a good manner.

Note: If the test Web page contains an IFRAME, you need to compute the location of the webelement in a more complex way, as detailed in the calculation of the position of an element in a Web page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.