2019-11-20

自动化测试-testng

自动化设计模式

1.自动化测试的设计模式是page-object,由页面层,逻辑层,业务层组成。
2.页面层主要是获取网页元素的,逻辑层主要负责编写业务逻辑,业务层就是测试人员设计的自动化测试用例。
下面是代码的展示
页面层

import org.openqa.selenium.By;

public class LoginPage {
    public static By usernameInput=By.id("username");
    public static By passwordInput=By.id("password");
    public  static By submitBuutton=By.id("submit");
}

逻辑层

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginSteps {
    public static void login(WebDriver driver,String username,String password)   {
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("submit")).click();
        driver.switchTo().alert().accept();

    }
}

业务层

package Login;


import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import steps.LoginSteps;
import tools.ExcelUnit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginTest {
WebDriver driver;
@BeforeMethod
    public void open(){
    System.setProperty("webdriver.firefox.marionette","C:\\Windows\\SysWOW64\\geckodriver.exe");
    driver=new FirefoxDriver();
    driver.get("");

}
public   Object[][] testdata(String file,String sheetName){
    List<String> list=new ArrayList<String>();
    Workbook workbook= ExcelUnit.getwork(file,sheetName);
    Sheet sheet=workbook.getSheet(sheetName);
    // 总行数需要在其基础上+1
    int total = sheet.getLastRowNum() + 1;
    //获取总列数
    int columns = sheet.getRow(0).getPhysicalNumberOfCells();
    Map<String,String>[][] map=new HashMap[total-1][1];
    //初始化表格中的数据
    if (total>1){
        for (int i = 0; i <total-1 ; i++) {
            map[i][0]=new HashMap();
        }
    }else {
    }
    //获取表格第一行的列名当作key
    for (int c = 0; c <columns; c++) {
        String cellValue = ExcelUnit.getCellValue(sheet, 0,c);
        list.add(cellValue);
    }
    //遍历单元格中的值加入到hashmap中
    for (int r = 1; r <total ; r++) {
        for (int c = 0; c <columns ; c++) {
            String cellValue = ExcelUnit.getCellValue(sheet, r,c);
            map[r-1][0].put(list.get(c),cellValue);
        }
    }
return map;
}
   @DataProvider(name = "data1")
    public Object[][]data(){
       LoginTest loginTest=new LoginTest();


       return loginTest.testdata("D:\\vv.xlsx","logintest");
    }
    @Test(dataProvider = "data1")
    public void login(HashMap<String,String> data)  {
    String username=data.get("username");
    String password=data.get("password");
        LoginSteps.login(driver,username,password );
        String title = driver.getTitle();
        System.out.println(title);
        Assert.assertEquals(title,"我的健康");
    }
    @AfterMethod
    public void close(){
    driver.quit();
    }
}
楼主这里有个问题一直搞不定,为啥文件路径必须是绝对路径,相对路径就不行

封装excelunit类,业务层调取这个类用于执行测试用例所需要的数据

package tools;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ExcelUnit {
    public static Workbook getwork(String filepath,String sheetName) {
        Workbook workbook = null;
        try {
            if (filepath.endsWith(".xls")) {
                File file = new File(filepath);
                InputStream is = new FileInputStream(file);
                workbook = new HSSFWorkbook(is);

            } else if (filepath.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(filepath);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }

    public static String getCellValue(Sheet sheet, int rowNum, int cellNum) {
        Cell cell = sheet.getRow(rowNum).getCell(cellNum);
        String value = ExcelUnit.getCellValue(cell);
        return value;
    }

    private static String getCellValue(Cell cell) {
        String  value = "";
        switch (cell.getCellTypeEnum()) {
            case STRING:
                value = String.valueOf(cell.getRichStringCellValue());
                return value;
            case NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
                return value;
            case BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                return value;

            case FORMULA:
                value = String.valueOf(cell.getCellFormula());
                return value;

            case ERROR:
                value = String.valueOf(cell.getErrorCellValue());
                return value;
            case BLANK:
                return value;
            default:
                System.out.println("未知单元格类型");
                return value;

        }
    }
}

3.好处:这个模式的好处在于后期维护非常的方便,前端界面的代码发生了改动,我们只需改动页面层;后端代码修改了业务逻辑,我们需要改动逻辑层;业务层只需要调取逻辑层的代码实现数据的参数化执行用例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值