--好的测试人员都会截得一手好图,就跟骨灰级宅男定会吟得一手好诗一般。
截取页面全图,不管页面有多长。
<wbr></wbr>
Java代码
<wbr></wbr>
package com.test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test_ShotScreen {
public static void main(String[] args)throws IOException, InterruptedException{
<wbr></wbr>
String url = "http://www.51.com";
//打开chrome
<wbr><wbr>WebDriver dr = new ChromeDriver();</wbr></wbr>
<wbr><wbr><wbr><wbr>dr.get(url);</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>//这里等待页面加载完成</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>Thread.sleep(5000);</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>//下面代码是得到截图并保存在D盘下</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">File screenShotFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);</span></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>//<span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">FileUtils.copyFile(</span><span style="color:#0C00FF; word-wrap:normal; word-break:normal; line-height:21px">screenShotFile</span><span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">,</span><span style="color:#0002FF; word-wrap:normal; word-break:normal; line-height:21px">new File("D:/test.png")</span><span style="color:#FF0000; word-wrap:normal; word-break:normal; line-height:21px">);</span><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>FileUtils.copyFile(screenShotFile, new File("D:\\AutoScreenCapture\\" + getCurrentDateTime()+ ".jpg")); <wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>dr.quit();</wbr></wbr></wbr></wbr>
}
<wbr></wbr>
public static String getCurrentDateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");//设置日期格式
//System.out.println(df.format(new Date()));
return df.format(new Date());
}
}
--方法
<wbr></wbr>
import java.io.File; <wbr></wbr>
import java.io.IOException; <wbr></wbr>
import org.apache.commons.io.FileUtils; <wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
public void takeScreenShot(String name){ <wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr><wbr></wbr>File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); <wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr><wbr></wbr>try { <wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>FileUtils.copyFile(scrFile, new File("c:\\Learning\\"+ name)); <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>} catch (IOException e) { <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace(); <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>} <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}