Selenium2 Getting Started assertion failure Automatic screenshot (iv)

Source: Internet
Author: User

After a general Web application error occurs, an exception is thrown. This time can cut a picture down, of course, is very good.

The selenium comes with its own features.

            // Get file            File scrfile= ((takesscreenshot) driver). Getscreenshotas (Outputtype.file);             // move a picture to a specified location            New File (Newfilepath));

Of course, before you need to determine whether the new path is legitimate. The whole code is posted below.

 PackageCommon;ImportJava.io.File;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.apache.commons.io.FileUtils;ImportOrg.openqa.selenium.OutputType;ImportOrg.openqa.selenium.TakesScreenshot;ImportOrg.openqa.selenium.WebDriver; Public classscreenshot { PublicWebdriver Driver;  Publicscreenshot (Webdriver _driver) {driver=_driver; }    Private voidtakescreenshot (String screenpath) {Try {            //Get FileFile scrfile=((takesscreenshot) driver). Getscreenshotas (Outputtype.file); //move a picture to a specified locationFileutils.movefile (Scrfile,NewFile (Screenpath)); }  Catch(IOException e) {e.printstacktrace (); }    }     Public voidTakescreenshot () {SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd (HH_MM_SS) SSS"); String FileName=dateformat.format (NewDate ()); //Create a new file nameString screenname=filename+ ". jpg"; //Create a folderFile dir =NewFile ("Test-output/snapshot"); if(!dir.exists ())        {Dir.mkdirs (); }        //gets the absolute path of the new file nameString screenpath= Dir.getabsolutepath () + "/" +screenname; //         This. Takescreenshot (Screenpath); }}

Call

New Screenshot (Driver);

Selenium2 Getting Started assertion failure Automatic (iv)

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.