“天街小雨润如酥,草色遥看近却无。最是一年春好处,绝胜烟柳满皇都。”读一首古诗,心情也随之平静下来

一个完整的maven配置selenium webdriver工程实例(四)

上一篇 / 下一篇  2013-10-23 11:22:24 / 个人分类:测试工具

本文是一个完整的使用maven配置的selenium webdriver工程,主要实现了自动化测试发送邮件的功能。

第一部分:错误处理ErrorHandle

package errorhandle;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import mail.Mail;

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;

public class ErrorHandle {

private String subject = "";
private String testcase = "";

//定义变量,存储时间,用于图片的存储
private Date date = null;
private SimpleDateFormat sdateFormat = null;

public ErrorHandle(){

subject = "xx平台,用户登录选择企业,遍历测试";
testcase = "本用例的目的:在不知道有多少企业的情况下,遍历链接测试,保证用户所属的每个企业都可以正常进入,可用于**同一类型链接是否到达正常页面<br>"+
"本用例的主要测试点为:<br>"+
"1.用户登录xx平台 <br>" +
"2.进入该用户所属的每个企业<br>"+
"3.退出登录 <br>";


//初始时间变量,并且设置时间格式
date = new Date();
sdateFormat=new SimpleDateFormat("yyyyMMddhhmmssSS");
//System.out.println(sdf.format(date.getTime()));
}

public boolean ErrorHandleMsg(Mail mail,WebDriver driver,String errorMsg){

String bodyText = driver.findElement(By.cssSelector("BODY")).getText();
if(bodyText.matches("^[\\s\\S]*HTTP 500 -内部服务器错误[\\s\\S]*$")){

//设置邮件标题
mail.setSubject(subject+",测试结果:FAIL-500");

//设置邮件内容
mail.setContent(testcase+
"===========================================================<br>"+
"<b><font color=\"red\">"+errorMsg+
"<br>"+
"页面报错500,可能服务器宕机,请检查原因,当前页面返回的url链接为:<br>"
+driver.getCurrentUrl()+
"<br>当前页面的内容为:<br>"+
driver.getPageSource()+
"</font></b>");

try {
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File pic = new File(System.getProperty("user.dir")+"/screenshot/choose/500-"+sdateFormat.format(date.getTime())+".png");
FileUtils.copyFile(screenShotFile, pic);
mail.addAttachfile(pic.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//发送邮件
mail.send();

System.out.println("!!!errorHandle方法中页面报错500,请检查服务器!!!");

// returnFlag = false;

}else if(bodyText.matches("^[\\s\\S]*404[\\s\\S]*$")){
System.out.println("errorHandle方法中出现失败,开始发送提醒邮件");
//设置邮件标题
mail.setSubject(subject+",测试结果:FAIL-404");

//设置邮件内容
mail.setContent(testcase+
"===========================================================<br>"+
"<b><font color=\"red\">"+errorMsg+
"<br>"+
"<b><font color=\"red\">页面报错404,可能页面丢失,请检查原因,当前页面返回的url链接为:<br>"
+driver.getCurrentUrl()
+"<br>当前页面的内容为:<br>"
+driver.getPageSource()
+"</font></b>");

try {
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File pic = new File(System.getProperty("user.dir")+"/screenshot/choose/404-"+sdateFormat.format(date.getTime())+".png");
FileUtils.copyFile(screenShotFile, pic);
mail.addAttachfile(pic.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//发送邮件
mail.send();

System.out.println("!!!errorHandle方法中页面报错404,可能页面丢失,请检查原因!!!");

}else{
// System.out.println("errorHandle方法中页面报错404,开始发送提醒邮件");
//设置邮件标题
mail.setSubject(subject+",测试结果:FAIL");

//设置邮件内容
mail.setContent(testcase+
"===========================================================<br>"+
"<b><font color=\"red\">"+errorMsg+
"<br>"+
"<b><font color=\"red\">请检查,当前页面返回的url链接为:<br>"
+driver.getCurrentUrl()+
"<br>当前页面的title为:<br>"
+driver.getTitle()+
"<br>当前页面的内容为:<br>"
+driver.getPageSource()+
"</font></b>");

try {
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File pic = new File(System.getProperty("user.dir")+"/screenshot/choose/others-"+sdateFormat.format(date.getTime())+".png");
FileUtils.copyFile(screenShotFile, pic);
mail.addAttachfile(pic.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//发送邮件
mail.send();

System.out.println("!!!errorHandle方法中出现500和404以外的问题,请检查!!!");
}

return false;
}
}


TAG: 软件测试 实例 maven Maven Selenium selenium webdriver 自动化测试

 

评分:0

我来说两句

Open Toolbar