Example: Open Baidu home page, do
01 |
package com.example.tests; |
03 |
import org.apache.commons.io.FileUtils; |
05 |
import org.openqa.selenium.*; |
06 |
import org.openqa.selenium.ie.InternetExplorerDriver; |
07 |
public class Selenium2 { |
09 |
public void testTakesScreenshot() { |
10 |
WebDriver driver = new InternetExplorerDriver(); |
11 |
driver.get( "http://www.baidu.com" ); |
13 |
File srcFile = ((TakesScreenshot)driver). |
14 |
getScreenshotAs(OutputType.FILE); |
16 |
(srcFile, new File( "d:\\screenshot.png" )); |
17 |
} catch (Exception e) { |
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 |
package com.example.tests; |
03 |
import java.io.IOException; |
04 |
import java.net.MalformedURLException; |
06 |
import org.apache.commons.io.FileUtils; |
08 |
import org.openqa.selenium.*; |
09 |
import org.openqa.selenium.remote.*; |
10 |
public class Selenium2 { |
12 |
public void testRemoteWebDriverScreenShot() { |
14 |
DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); |
15 |
WebDriver driver = null ; |
17 |
driver = new RemoteWebDriver( //我使用localhost来测试 |
18 |
new URL( "http://localhost:4444/wd/hub" ), capability); |
19 |
} catch (MalformedURLException e) { |
22 |
driver.get( "http://www.sina.com.cn" ); |
24 |
driver = new Augmenter().augment(driver); |
26 |
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
28 |
FileUtils.copyFile(scrFile, new File( "D:\\screenshot.png" )); |
29 |
} catch (IOException e) { |