利用WebDriver进行网页截图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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.firefox.FirefoxDriver; import java.io.File; import java.io.IOException; /** * Created by zsn on 2015/4/7. */ public class ScreenShotDemo { public static void main(String[] args) { takeScreenShot("http://www.baidu.com"); takeScreenShot("http://www.izsn.cn"); } private static void takeScreenShot(String url){ WebDriver driver = new FirefoxDriver(); //设置窗口最大化 driver.manage().window().maximize(); driver.get(url); TakesScreenshot screenshot = (TakesScreenshot) driver; File file = screenshot.getScreenshotAs(OutputType.FILE); try { //将文件保存在当前目录下screenshot文件夹下 String fileName = "./screenshot/"+java.net.URLEncoder.encode(url,"utf-8")+".jpg"; FileUtils.copyFile(file, new File(fileName)); } catch (IOException e) { e.printStackTrace(); }finally { driver.close(); } } } |
转载请注明:软件测试 » 利用WebDriver进行网页截图