您的位置:首页 > 其它

WebDriver与文件系统

2016-03-24 14:45 288 查看
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");
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: