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

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

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


<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>

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值