在这里给大家分享几个Selenium的自动化测试框架
Selenium自动化框架(Java)第一篇:
以excel维护测试数据,进行数据驱动测试
Common模块中主要存放基础操作方法(excel二次封装的读写方法、参数化的配置文本信息、Selenium基础操作方法),简单演示下:
excel二次封装的读写方法
/*
* 写入测试用例执行结果
*/
public void addCaseResult(String excelPath,String caseid,String result)
throws BiffException, IOException, RowsExceededException, WriteException{
File file = new File(excelPath);
if(file.exists()){
wb = Workbook.getWorkbook(file);
wwb = Workbook.createWorkbook(file, wb);
sheet = wwb.getSheet("caseResult");
}else{
wwb = Workbook.createWorkbook(new File(excelPath));
sheet = wwb.createSheet("caseResult", 0);
}
Label a1 = new Label(0,resultLineNum,caseid);
Label a2 = new Label(1,resultLineNum,result);
sheet.addCell(a1);
sheet.addCell(a2);
wwb.write();
wwb.close();
resultLineNum++;
}
/*
* 读取excel数据
*/
public String[][] readExcel(String testFilePath) throws BiffException, IOException, SQLException{
File file = new File(testFilePath);
WorkbookSettings ws = new WorkbookSettings();
ws.setCellValidationDisabled(true);
Workbook wwb = Workbook.getWorkbook(file,ws);
Sheet sheet = wwb.getSheet(0);
int rows = sheet.getRows();
int columns = sheet.getColumns();
// System.out.println(rows + "and" + columns);
String[][] cases = new String[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Cell cell = sheet.getCell(j, i);
cases[i][j] = cell.getContents();
}
}
wwb.close();
return cases;
}
参数化的配置文本信息
public final static String chromeDiverName = "webdriver.chrome.driver";
public final static String chromeDiverpath = "C://Program Files (x86)//Google//Chrome//Application//chromedriver.exe";
// 为了解决多种WebDriver的问题,使用前需要调用SysMethod中的方法初始化
public static WebDriver driver;
// 指定各种类型的账号密码
public final static String chanDaoName = "admin";
public final static String chanDaoPwd = "123456";
// 各用例EXCEL路径
public final static String caseExcelPath = "src/main/resources/chanDaoCases.xls";
public final static String loginCases = "登录";
public final static String productAddCases = "添加产品";
public final static String productEditCases = "编辑产品";
public final static String productDeleteCases = "删除产品";
Selenium基础操作方法
/*
* 使用ChromeDriver测试
*/
public void useChromeDriver(){
System.setProperty(SysConfig.chromeDiverName, SysConfig.chromeDiverpath);
SysConfig.driver = new ChromeDriver();
}
/*
* 打开Url地址
*/
public void openURL(String url){
SysConfig.driver.get(url);
SysConfig.driver.manage().window().maximize();
SysConfig.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
sleep(500);
}
/*
* 判断页面元素是否存在
*/
public boolean isElementPresent(By Locator){
try{
SysConfig.driver.findElement(Locator);
return true;
}catch(NoSuchElementException e){
return false;
}
}
/*
* 判断是否有异常弹窗
*/
public boolean isAlertPresent(){
try{
SysConfig.driver.switchTo().alert();
return true;
}catch(NoAlertPresentException e){
return false;
}
}
/*
* 关闭告警及获取文本框信息
*/
public String closeAlertAndGetAlertText(){
boolean accept_next_alert = true;
try{
Alert alert = SysConfig.driver.switchTo().alert();
String alertText = alert.getText();
if(accept_next_alert){
alert.accept();
}else {
alert.dismiss();
}
return alertText;
}finally{
accept_next_alert = true;
}
}
/*
* 获取告警信息
*/
public String getAlertText(){
try{
Alert alert = SysConfig.driver.switchTo().alert();
String text = alert.getText();
return text;
}catch(NoAlertPresentException e){
log.error(this,e);
return "";
}
}
Module模块中主要存放一个大功能模块对应的小功能组合,以禅道的登录模块举例:
/*
* 禅道 登录测试
* Create By 虫下(古月随笔) 2016-03-30
*/
public class Login {
private Log log = LogFactory.getLog(Login.class);
private SysMethod sysMethod = new SysMethod();
/*
* 打开登录页面
*/
public void openLoginPage(){
sysMethod.openURL(SysConfig.chanDaoLoginUrl);
//智能等待10秒,等待页面缓冲完成
SysConfig.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
/*
* 设置用户名
*/
public void setLoginName(String loginName){
try{
//查找用户名编辑框中的id
if(ElementExist(By.name("account"))){
WebElement ln= SysConfig.driver.findElement(By.name("account"));
ln.sendKeys(loginName);
log.info("设置用户名:" + loginName);
}
else
{
log.info("用户名框中没有name元素::account");
}
}catch(NoSuchElementException e){
log.error(this,e);
}
}
/*
* 清除用户名
*/
public void clearLoginName(){
try{
if(ElementExist(By.name("account"))){
WebElement ln= SysConfig.driver.findElement(By.name("account"));
ln.clear();
log.info("清除用户名");
}
else
{
log.info("用户名框中没有name元素::account");
}
}catch(NoSuchElementException e){
log.error(this,e);
}
}
/*
* 设置密码
*/
public void setPwd(String password){
try{
if(ElementExist(By.name("password"))){
WebElement pwd = SysConfig.driver.findElement(By.name("password"));
pwd.sendKeys(password);
log.info("设置密码:" + password);
}
else
{
log.info("密码框中没有name元素::password");
}
}catch(NoSuchElementException e){
log.error(this,e);
}
}
/*
* 清除密码
*/
public void clearPwd(){
try{
if(ElementExist(By.name("password"))){
WebElement pwd = SysConfig.driver.findElement(By.name("password"));
pwd.clear();
log.info("清除密码");
}
else
{
log.info("密码框中没有name元素::password");
}
}catch(NoSuchElementException e){
log.error(this,e);
}
}
/*
* 点击登录按钮
*/
public void clickLoginBtn(){
try{
WebElement btn = SysConfig.driver.findElement(By.id("submit"));
btn.click();
log.info("按下登录按钮");
}catch(NoSuchElementException e){
log.error(this,e);
}
}
}
Module.result模块主要针对大功能块的执行结果进行判断,同样以禅道登录模块举例
/*
* Create By 虫下(古月随笔) 2016-03-30
*/
public class LoginResult {
/*
* 跳转首页URL判断
*/
public String loginUrlResult(){
String urlWanted = SysConfig.chanDaoHomePage;
String res = "";
String url = SysConfig.driver.getCurrentUrl();
if(!urlWanted.equalsIgnoreCase(url))
res = "登录失败,期望URL:" + urlWanted + "实际:" + url;
return res;
}
/*
* 错误消息判断
*/
public String errMsgResult(String errorMessageWanted,String errorMessage){
String res = "";
if(!errorMessage.contains(errorMessageWanted))
res = "提示信息与预期不符,期望:" + errorMessageWanted + "。实际:" + errorMessage;
return res;
}
}
cases模块主要是再次封装子功能模块,按照测试用例编写方式组合成一个完整的测试用例,同样以禅道登录举例
Java代码实现:
/*
* Create By 虫下(古月随笔) 2016-03-30
*/
public class LoginCases {
//初始化登录模块
private Login login = new Login();
//初始化登录判断模块
private LoginResult loginRes = new LoginResult();
//初始化基础方法
private SysMethod sysMethod = new SysMethod();
private Log log = LogFactory.getLog(LoginCases.class);
private Excel excel = new Excel();
//获取excel用例路径
private String casePath = SysConfig.caseExcelPath;
private String loginCasePath = SysConfig.loginCases;
/*
* 打开登录页面
*/
public void openLoginPage(){
sysMethod.openURL(SysConfig.chanDaoLoginUrl);
//智能等待10秒,等待页面缓冲完成
SysConfig.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
/*
* 登录操作
*/
public void login(String loginName,String pwd){
try{
openLoginPage();
login.clearLoginName();
login.setLoginName(loginName);
login.clearPwd();
login.setPwd(pwd);
login.clickLoginBtn();
sysMethod.sleep(1000);
if(sysMethod.isAlertPresent()){
String alertText = sysMethod.closeAlertAndGetAlertText();
System.out.println("弹出的告警信息为" + alertText);
}
else {
System.out.println("无告警弹窗!!");
}
}catch(Exception e){
log.error(this, e);
}
}
}
Run模块主要是调用Case模块中的对应方法
/*
* 运行
* Create By 虫下(古月随笔) 2016-03-30
*/
public class Run {
public static void main(String[] args){
Log log = LogFactory.getLog(Run.class);
SysMethod sysMethod = new SysMethod();
sysMethod.useChromeDriver();
LoginCases loginCases = new LoginCases();
loginCases.runLoginCases();
log.info("用例执行完毕");
}
}
最后建议,使用Java建项目工程的时候采用Maven项目工程,维护Jar相当方便