Selenium webdriver--Implementation screenshot function

Source: Internet
Author: User
Tags testng

Method

1  Public Static voidSnapshot (takesscreenshot drivername, String filename)2   {3       //This method would take the screen shot, require-parameters, one is driver name, and another is file name4       5String Currentpath = System.getproperty ("User.dir");//get Current Work folder6 System.out.println (currentpath);7File Scrfile =Drivername.getscreenshotas (outputtype.file);8         //Now you can do whatever need to do with it, for example copy somewhere9         Try {TenSystem.out.println ("Save snapshot Path is:" +currentpath+ "/" +filename); OneFileutils.copyfile (Scrfile,NewFile (currentpath+ "\ \" +filename)); A}Catch(IOException e) { -             //TODO auto-generated Catch block -System.out.println ("Can ' t Save screenshot"); the e.printstacktrace (); -         }  -         finally -         { +             -System.out.println ("Screen shot finished"); +         } A}

1. Use selenium to open Baidu;

2. Enter Selenium keyword

3. Search,

The specific code is as follows:

1 ImportJava.io.File;2 Importjava.io.IOException;3 4 Importorg.apache.commons.io.FileUtils;5 Importorg.openqa.selenium.By;6 ImportOrg.openqa.selenium.OutputType;7 ImportOrg.openqa.selenium.TakesScreenshot;8 ImportOrg.openqa.selenium.WebDriver;9 Importorg.openqa.selenium.WebElement;Ten ImportOrg.openqa.selenium.firefox.FirefoxDriver; One ImportOrg.testng.annotations.AfterClass; A ImportOrg.testng.annotations.BeforeClass; - Importorg.testng.annotations.Test; -  the  Public classTestsavescreen { -      -     PrivateWebdriver driver; - @BeforeClass +            Public voidBeforeclass () { -Driver =Newfirefoxdriver (); +Driver.get ("https://www.baidu.com/"); ASnapshot (Takesscreenshot) driver, "Open_baidu.png"); at           } - @Test -          Public voidTest () { -Webelement searchinput= driver.findelement (by.id ("kw"))); -Searchinput.sendkeys ("Selenium"); inWebelement searchbutton= driver.findelement (by.id ("su")); - Searchbutton.click (); toSnapshot (Takesscreenshot) driver, "Search_output.png"); +           } -  the @AfterClass *          Public voidAfterclass () { $ driver.quit ();Panax Notoginseng         } -          the          Public Static voidSnapshot (takesscreenshot drivername, String filename) +           { A               //This method would take the screen shot, require-parameters, one is driver name, and another is file name the                +String Currentpath = System.getproperty ("User.dir");//get Current Work folder - System.out.println (currentpath); $File Scrfile =Drivername.getscreenshotas (outputtype.file); $                 //Now you can do whatever need to do with it, for example copy somewhere -                 Try { -System.out.println ("Save snapshot Path is:" +currentpath+ "/" +filename); theFileutils.copyfile (Scrfile,NewFile (currentpath+ "\ \" +filename)); -}Catch(IOException e) {Wuyi                     //TODO auto-generated Catch block theSystem.out.println ("Can ' t Save screenshot"); - e.printstacktrace (); Wu                 }  -                 finally About                 { $                     -System.out.println ("Screen shot finished"); -                 } -           } A}

The effect is as follows:

Selenium webdriver--Implementation function

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.