WebDriver与文件系统

1.屏幕截屏操作:其接口函数是TakesScreenshot。该功能是在运行测试用例的过程中,需要验证某个元素的状态或者显示的数值时,可以将屏幕截取下来进行对比;或者在异常或者错误发生的时候将屏幕截取并保存起来,供后续分析和调式所用。

package test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import common.StartFireFox;

public class TestFile {
    public static void main(String[] args) {
        WebDriver driver = StartFireFox.start();
        driver.get("http://www.baidu.com");
        //截取当前页面。截取的图片以文件形式返回
        File srcFile=  ((TakesScreenshot) driver) .getScreenshotAs(OutputType.FILE);
 
        try {   //保存截取的图片
                FileUtils.copyFile(srcFile,new File("selenium2/screenshot.png") );     
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     
        driver.quit();
        System.out.println("close firefox browser");
    }
}

 

posted @ 2016-03-24 14:45  夏之末  阅读(146)  评论(0编辑  收藏  举报