码迷,mamicode.com
首页 > Web开发 > 详细

Selenium Web测试基础类及方法

时间:2015-07-20 20:56:02      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

package testNG;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.crypto.Data;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

 
@SuppressWarnings("unused")

public class BaseClass {
    /***
     * 基础类,包括方法:获取基础数据、获取测试数据、返回当前时间、写入测试结果文件、截图、打开结果文件、去除空格换行及制表符的字符串
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */
    
    
    //读取测试URL、测试数据文件路径和结果文件路径
    public String getBaseData(String testInfo, String baseDataPath) throws IOException{
        /**
         * FunName:        getBaseData
         * Description :   获取测试URL、测试数据文件路径和结果文件路径
         * @param:                 String testInfo
         * @return String:  返回基础数据字符串;
         *@Create Date:   2015-06-29
         **/
        String testString =  null;
        FileInputStream file = new FileInputStream(baseDataPath);
        @SuppressWarnings("resource")
        XSSFWorkbook wb = new XSSFWorkbook(file);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow rowinf = sheet.getRow(1);
        if(testInfo == "inPath"){
            Cell cell = rowinf.getCell(0);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        else if(testInfo == "outPath"){
            Cell cell = rowinf.getCell(1);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        else if(testInfo == "baseURL"){
            Cell cell = rowinf.getCell(2);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        else if(testInfo == "firefoxPath"){
            Cell cell = rowinf.getCell(3);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        else if(testInfo == "screenShotDir"){
            Cell cell = rowinf.getCell(4);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        //验证码截图
        else if(testInfo == "snapshotDir"){
            Cell cell = rowinf.getCell(5);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        else if(testInfo == "xiaoWeiIssuerURL"){
            Cell cell = rowinf.getCell(6);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            testString= cell.getStringCellValue();
        }
        return testString;
    }

    
    //读取信息,被测试方法调用,参数为方法名、列名colNm、文件路径inPath
    public String getData_xlsx(String funNm, String colNm, String inPath) throws FileNotFoundException, IOException{
        /**
          * FunName:        getData_xlsx
          * Description :   读取信息,供被测试方法调用
          * @param:                 String funNm, String colNm, String inPath
          * @return String:  返回测试数据;
          * @Create Date:   2015-06-29
          **/
        String str1 = null;
        FileInputStream file = new FileInputStream(inPath);
        @SuppressWarnings("resource")
        XSSFWorkbook wb = new XSSFWorkbook(file);
        XSSFSheet sheet = null;
        //根据方法名,读取对应的sheet
        if (funNm == "Login"){
            sheet = wb.getSheetAt(1);
        } else if (funNm == "QueryApp"){
            sheet = wb.getSheetAt(2);
        }
        XSSFRow row1 = sheet.getRow(1);
        //根据不同的列名,读取对应的列
        if(colNm == "orgz"){
            Cell cell = row1.getCell(0);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "userId"){
            Cell cell = row1.getCell(1);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "pwd"){
            Cell cell = row1.getCell(2);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "appNo"){
            Cell cell = row1.getCell(0);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "holName"){
            Cell cell = row1.getCell(1);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "cerType"){
            Cell cell = row1.getCell(2);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "cerNo"){
            Cell cell = row1.getCell(3);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "xiaoWeiUserId"){
            Cell cell = row1.getCell(3);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        else if(colNm == "xiaoWeiUserPwd"){
            Cell cell = row1.getCell(4);
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            str1= cell.getStringCellValue();
            }
        return str1;
    }
    //写入测试结果文件,参数为方法名、是否通过、文件路径和执行时间
    public void outCome_xlsx(String funNm, String outCome, String outPath,String date) throws IOException
    {
        /**
          * FunName:        outCome_xlsx
          * Description :   写入测试结果
          * @param:                 String funNm, String outCome, String outPath,String date
          * @return void:        无返回数据;
          * @Author:        Bingo
          * @Create Date:   2015-06-29
          **/
        FileInputStream file = new FileInputStream(outPath);
        @SuppressWarnings("resource")
        XSSFWorkbook wb = new XSSFWorkbook(file);
        XSSFSheet sheet = wb.getSheetAt(0);
        int trLength = sheet.getLastRowNum();  
        XSSFRow newrow = sheet.createRow((short)(trLength+1));
        newrow.createCell(0).setCellValue(funNm);  
        newrow.createCell(1).setCellValue(outCome);
        newrow.createCell(2).setCellValue(date);
        FileOutputStream fout = new FileOutputStream(outPath);
        wb.write(fout);
        file.close();
        fout.close();
 
    }
    
    public void openOutCome(String outPath){
        /**
          * FunName:        openOutCome
          * Description :   打开测试结果文件
          * @param:                 文件URL
          * @return void:        无返回数据;
          * @Author:        Bingo
          * @Create Date:   2015-06-30
          **/
        Desktop desk=Desktop.getDesktop();  
        try  
        {  
            File file1=new File(outPath);//创建一个java文件系统  
            if (!file1.canWrite())//判断文件是否被占用
            {
                desk.open(file1); //调用open(File f)方法打开文件  
            }else {
                FileInputStream in=new FileInputStream(file1);
                in.close();
                desk.open(file1);
            }
        }catch(Exception e)  
        {  
            System.out.println(e.toString());  
        }
        
    }
    
    public void screenShot(String screenOutDir,WebDriver driver,String funNm) throws InterruptedException{
        /**
          * FunName:        screenShot
          * Description :   截图
          * @param:                 文件URL
          * @return void:        无返回数据;
          * @Author:        Bingo
          * @Create Date:   2015-06-30
          **/
        //Thread.sleep(3000);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss-SSS");
        String time = sdf.format(new Date());
        
        String fileName = funNm+time +".png";
        try {
             File source_file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  // 执行屏幕截图
             
             FileUtils.copyFile(source_file, new File(screenOutDir + "/" + fileName));  // 另存到我们需要保存的目录,例如screenshot\20150706 165210-333.png
        } catch (IOException e) {
             e.printStackTrace();
          }        
    }
    
    public  static String replaceBlank(String str) {
        /**
         * 返回STR中去除空格、换行制表符的内容
         *
         */
        
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
    //获取当前系统时间
    public String nowDate(){
        String nowDate = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        nowDate =df.format(new Date()).toString();
        return nowDate;
    }
    
    //判断字符串是否为纯数字
    public boolean isDigitStr(String s){
        for(int i =0; i<s.length()-1;i++){
            if(!Character.isDigit(s.charAt(i))){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) throws IOException, InterruptedException {
        // TODO 自动生成的方法存根
         BaseClass baseClass = new BaseClass();
        String baseDatePath = "about:blank";
        String inPath = baseClass.getBaseData("inPath",baseDatePath);
        String outPath = baseClass.getBaseData("outPath",baseDatePath);
        String baseURL = baseClass.getBaseData("baseURL",baseDatePath);
        String firefoxPath = baseClass.getBaseData("firefoxPath",baseDatePath);
        String orgz = baseClass.getData_xlsx("Login", "orgz", inPath);
        String userId = baseClass.getData_xlsx("Login", "userId", inPath);
        String pwd = baseClass.getData_xlsx("Login", "pwd", inPath);
        String appNo = baseClass.getData_xlsx("QueryApp", "appNo", inPath);
        String holName = baseClass.getData_xlsx("QueryApp", "holName", inPath);
        String cerType = baseClass.getData_xlsx("QueryApp", "cerType", inPath);
        String cerNo = baseClass.getData_xlsx("QueryApp", "cerNo", inPath);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date =df.format(new Date());
        baseClass.outCome_xlsx("测试方法", "是否通过", outPath, date);
        System.out.println("inPath:"+inPath+"\noutPath:"+outPath+"\nbaseURL:"+baseURL+"\nfirefoxPath"+firefoxPath);
        System.out.println("\norgz:"+orgz+"\nuserId:"+userId+"\npwdL:"+pwd+"\nappNo"+appNo);
        System.out.println("\nholName:"+holName+"\ncerType:"+cerType+"\ncerNo:"+cerNo);
        baseClass.openOutCome(outPath);
        WebDriver driver = new FirefoxDriver();
        Thread.sleep(3000);
        String screenShotDir = baseClass.getBaseData("screenShotDir",baseDatePath);
        Thread.sleep(3000);
        baseClass.screenShot(screenShotDir, driver,"testFunNm");
        driver.close();
        
    }
    
}

Selenium Web测试基础类及方法

标签:

原文地址:http://www.cnblogs.com/tuanfuyao/p/4662194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!