在自动化测试过程中,关键节点截屏是很有用的一招,能直观看到整个过程及结果。
本篇以某财BBS每天签到领铜钱为例,在以下8个关键节点进行截屏:登录前、登录后、个人信息页面铜钱数量、签到子版块、今日签到帖子、回帖关键字、回帖后、个人信息页面铜钱数量。
经多次测试,全部过程20s左右能够完成。
截屏部分代码如下:
public static int t = 1;
public static String getDateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
return df.format(new Date());
}
public static void ScreenShot(WebDriver dr, String dir){
File screenShot = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShot, new File(dir+getDateTime()+"_"+t+".jpg"));
++t;
} catch (IOException e) {
e.printStackTrace();
}
}
截屏的图片保存在dir路径下,随着时间增长,会产生越来越多的文件,所以一开始的时候判断下文件数量,过多的话删除。
File file = new File(dir);
File[] fs = file.listFiles();
if(fs.length >= 80){
for(File f: fs){
if(f.getName().contains("jpg"))
f.delete();
}
}
完整代码如下:
package com.company;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import java.util.Calendar;
import java.util.regex.*;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;
public class AutoLoginReplyScreenshot {
public static int t = 1;
public static String getDateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
return df.format(new Date());
}
public static void ScreenShot(WebDriver dr, String dir){
File screenShot = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShot, new File(dir+getDateTime()+"_"+t+".jpg"));
++t;
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
System.out.println(AutoLoginReplyScreenshot.getDateTime());
WebDriver driver = new FirefoxDriver();
String loginUrl = "https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php";
String basicURL ="http://bbs.wacai.com/portal.php";
String dir = "/Users/Sophie/IdeaProjects/BBSAutoLoginReplyScreenShot/ScreenShot/";
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(loginUrl);
driver.manage().window().maximize();
//delete all pics if there are more than 80
File file = new File(dir);
File[] fs = file.listFiles();
if(fs.length >= 80){
for(File f: fs){
if(f.getName().contains("jpg"))
f.delete();
}
}
/**
* 1st screen shot: login page
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
driver.findElement(By.id("account")).clear();
driver.findElement(By.id("account")).sendKeys("******");
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys("******");
driver.findElement(By.id("login-btn")).click();
driver.manage().window().maximize();
/**
*2nd screen shot: after login
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
String a = driver.findElement(By.cssSelector(".name")).getAttribute("href");
driver.get(a);
String pi = driver.findElement(By.xpath("//div[@class=‘userHead container‘]/div[@class=‘tabs w_tab‘]/nav/ul/li[5]/a")).getAttribute("href");
driver.get(pi);
/**
* 3rd screen shot: personal info
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
driver.get(basicURL);
//get the bbs portal page, find the specific forum href
a = driver.findElement(By.linkText("签到有礼")).getAttribute("href");
driver.get(a);
/**
* 4th screen shot: check-in forum
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
//Thread title
Calendar c = Calendar.getInstance();
int m = c.get(Calendar.MONTH)+1;
int d = c.get(Calendar.DAY_OF_MONTH);
String dd = ""+d;
if (d < 10)
dd = "0"+dd;
String threadTitle = "签到有礼"+m+"."+dd+"每天签到得铜钱";
//find today‘s check-in thread
a = driver.findElement(By.partialLinkText(threadTitle)).getAttribute("href");
driver.get(a);
/**
* 5th screen shot: check-in thread
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
//using regrex to find the content we use to reply
Pattern p = Pattern.compile("回帖内容必须为.+</font>非此内容将收回铜钱奖励");
Matcher r = p.matcher(driver.getPageSource().toString());
StringBuffer key = new StringBuffer();
while(r.find()){
key.append(r.group());
}
//using xpath locate the textarea and submit button
driver.findElement(By.xpath("//textarea[@id=‘fastpostmessage‘]")).sendKeys(key.substring(key.indexOf(">")+1, key.indexOf("</")-1));
driver.manage().window().maximize();
/**
* 6th screen shot: key words
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
driver.findElement(By.xpath("//button[@id=‘fastpostsubmit‘]")).click();
/**
* 7th screen shot: after reply
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
//go to personal info page
driver.get(pi);
/**
* 8th screen shot: check the coin amount
*/
AutoLoginReplyScreenshot.ScreenShot(driver, dir);
driver.close();
System.out.println(AutoLoginReplyScreenshot.getDateTime());
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-14 09:29:51