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.