分享

WebDriver拾级而上·之十二 截图selenium

 小猪窝969 2015-11-05

--好的测试人员都会截得一手好图,就跟骨灰级宅男定会吟得一手好诗一般。

截取页面全图,不管页面有多长。


 

Java代码


 

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{

 

String url = "http://www.51.com";

//打开chrome

   WebDriver dr = new ChromeDriver();

        dr.get(url);

        

        

        //这里等待页面加载完成

        Thread.sleep(5000);

        //下面代码是得到截图并保存在D盘下

        File screenShotFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);

        //FileUtils.copyFile(screenShotFile, new File("D:/test.png"));  

        FileUtils.copyFile(screenShotFile, new File("D:\\AutoScreenCapture\\" + getCurrentDateTime()+ ".jpg"));  

        

        dr.quit();

}

 

public static String getCurrentDateTime(){

SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");//设置日期格式

//System.out.println(df.format(new Date()));

return df.format(new Date());

}

}


--方法

 

import java.io.File;  

import java.io.IOException;  

import org.apache.commons.io.FileUtils;  

      

public void takeScreenShot(String name){  

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  

        try {  

                FileUtils.copyFile(scrFile, new File("c:\\Learning\\"+ name));  

            } catch (IOException e) {  

                e.printStackTrace();  

            }  

}  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多