Go: Webdriver Take a screenshot

Source: Internet
Author: User

Example: Open Baidu home page, do

01 packagecom.example.tests; 
02 importjava.io.File; 
03 importorg.apache.commons.io.FileUtils; 
04 importorg.junit.*; 
05 importorg.openqa.selenium.*; 
06 importorg.openqa.selenium.ie.InternetExplorerDriver; 
07 publicclassSelenium2 { 
08     @Test
09     publicvoidtestTakesScreenshot() { 
10         WebDriver driver = newInternetExplorerDriver(); 
11         driver.get("http://www.baidu.com"); 
12         try
13             File srcFile = ((TakesScreenshot)driver). 
14                     getScreenshotAs(OutputType.FILE); 
15             FileUtils.copyFile 
16             (srcFile,newFile("d:\\screenshot.png")); 
17         catch(Exception e) { 
18             e.printStackTrace(); 
19         }  
20           driver.close(); 
21         
22 }

The Takesscreenshot interface provides the Getscreenshotas () method to capture the screen. In the example above, we specify that Outputtype.file be passed as a parameter to the Getscreenshoas () method, telling it to return the intercepted screen as a file.

If you are using Remotewebdriver (), the method should be as follows

First start selenium Java-jar Selenium-server-standalone-2.25.0.jar

View Source print?
01 packagecom.example.tests; 
02 importjava.io.File; 
03 importjava.io.IOException; 
04 importjava.net.MalformedURLException; 
05 importjava.net.URL; 
06 importorg.apache.commons.io.FileUtils; 
07 importorg.junit.*; 
08 importorg.openqa.selenium.*; 
09 importorg.openqa.selenium.remote.*; 
10 publicclassSelenium2 { 
11     @Test
12     publicvoidtestRemoteWebDriverScreenShot() { 
13         //指定使用的浏览器 
14         DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); 
15         WebDriver driver = null
16         try
17             driver = newRemoteWebDriver( //我使用localhost来测试 
18                     newURL("http://localhost:4444/wd/hub"), capability); 
19         catch(MalformedURLException e) { 
20             e.printStackTrace(); 
21         
22         driver.get("http://www.sina.com.cn"); 
23         //对远程系统进行 
24         driver = newAugmenter().augment(driver);  
25         File scrFile =   
26           ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
27         try
28             FileUtils.copyFile(scrFile, newFile("D:\\screenshot.png")); 
29         catch(IOException e) { 
30             e.printStackTrace(); 
31         
32     
33 }

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.