关键字驱动框架入门教程-10-异常处理
2018-02-08 23:20
666 查看
什么是异常
一个异常其实就是一个事件,特点就是在程序执行过程中出现的,会打断正常的程序设计流程。简单来说,在自动化测试中,任何一个错误,导致你执行用例失败,这个就是一个异常。
错误和异常的区别
一个错误,一般是验证的问题,是无法通过try catch捕获的。异常是可以想象得到的报错,可以手动捕获并处理。一行代码如果发生错误,例如不符合语法规范,那么根本通不过编译,但是一个异常更多是由于没有考虑到特殊场景,而没有去处理。
什么是异常处理
异常处理首先需要通过预先想到可能存在的错误,通过代码块语句,处理这个异常对象,并帮助程序开发人员对这个异常进行一些改变处理,目的是不会中断程序正常运行。
为什么异常处理非常重要
1) 试想一下这个场景,当你得到一个异常而且你想打印出一些自定义的消息,所以别人通过这个自定义消息内容,明白这个是什么意思。
2) 有些时候,我们需要得到某些特定异常的步骤,方便测试人员多测试这块。
3)实际一些,我们在selenium自动化测试中,你可能想处理以下异常:
ElementNotSelectableException,
ElementNotVisibleException,
NoSuchElementException。
Selenium中不同的异常
下面列举一些常见的异常,这些异常在selenium自动化测试中可能遇到。
1.NoSuchElementException: 意思就是当前定位元素方法无法找到元素,很有可能元素定位表达式写错了
2.StaleElementReferenceException: 意思就是这个元素不在当前DOM页面显示
3.TimeoutException:意思就是执行失败,因为在足够多默认时间内没有结束这个操作
4.ElementNotVisibleException: 意思就是,尽管元素在当前DOM页面存在,但是它是不可见的,所以无法产生交互。
5.ElementNotSelectableException:意思就是,可能元素不可用,所以无法选择到。
如何处理异常
在Java中使用try-catch语句进行异常处理。语法格式如下:
Try{
//一些代码
}catch(Exception e) {
//处理异常的代码块
}
多个Catch 语句块:一个try代码块可以跟着多个catch代码块。 try{ //Somecode}catch(ExceptionType1e1){ //Codefor Handling the Exception 1}catch(ExceptionType2e2){ //Codefor Handling the Exception 2 }
原则上是没有catch语句块个数的限定,上面举例写了两个,实际可以写多个。出现异常就会从第一个catch里面去匹配,如果匹配到就走第一个catch的异常处理代码,如果没有,接着匹配第二个catch里面的异常名称,以此类推。
Throw:有些时候,我们希望在代码中显示生成异常。举例,在Selenium自动化测试框架中,我们很多时候都是打印自己写的log,一旦我们捕获到异常,我们需要把这个异常抛出给系统,这样测试用例才会终止执行。Throw关键字用于运行时抛出异常并处理它。
Throws:如果我们在抛出一个异常而且没有处理这个异常,那么我们需要使用throws关键字。掉调用程序知道这个方法可能抛出异常,但是不会处理。
Finally:Finally关键字用来创建代码块去处理一个分支流程,这个finally代码块永远会执行,不管有没有异常发生。
在框架中处理异常
通过前面文章介绍,我们已经知道,我们在driverscript.java这个类中写了一个嵌套for循环,外层for循环来控制用例,内层for循环来控制每个用例下的测试步骤。那么我们怎么在这个执行过程中处理异常呢。我们是这样想的,如果在外层循环遇到执行测试用例失败,那么就在这个时刻处理异常,如果内层循环执行具体某一个步骤出现失败,也处理异常。所以,我们需要定义一个标记,类型是布尔型,如果标记为true,测试用例和测试步骤继续执行,如果遇到标记为false,那么就停下来,不继续执行后面的用例。
例如我在openBrowser()这个方法,这样去写异常处理。
public static void openBrowser(String object) {
// 这里,我们暂时都写死用chrome来进行自动化测试
try{
Log.info("启动chrome浏览器。");
System.setProperty("webdriver.chrome.driver",".\\libs\\chromedriver.exe");
driver = new ChromeDriver();
}catch(Exception e){
Log.info("无法启动浏览器 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
上面意思就是,一般来说bResult这个变量是true,测试用例正常被执行,如果出现异常,那么就从true改成false,这样下面其他用例看到这个标记变了就不会继续执行。
ActionsKeywords.java更新代码如下
package config;
import executionEngine.DriverScript;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import utility.Log;
import static executionEngine.DriverScript.OR;
import java.util.concurrent.TimeUnit;
public class ActionsKeywords {
public static WebDriver driver;
public static void openBrowser(String object) {
// 这里,我们暂时都写死用chrome来进行自动化测试
try{
Log.info("启动chrome浏览器。");
System.setProperty("webdriver.chrome.driver",".\\libs\\chromedriver.exe");
driver = new ChromeDriver();
}catch(Exception e){
Log.info("无法启动浏览器 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void openUrl(String object) {
try{
Log.info("打开测试环境地址");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(Constants.URL);
}catch(Exception e){
Log.info("无法打开测试环境地址 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void click(String object) {
try{
Log.info("点击元素: "+ object);
driver.findElement(By.xpath(OR.getProperty(object))).click();
}catch(Exception e){
Log.error("无法点击元素--- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void inputUsername(String object){
try{
Log.info("在用户名输入框输入文字");
driver.findElement(By.xpath(OR.getProperty(object))).sendKeys(Constants.UserName);
}catch(Exception e){
Log.error("无法输入用户名 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void inputPassword(String object){
try{
Log.info("密码框输入...");
driver.findElement(By.xpath(OR.getProperty(object))).sendKeys(Constants.Password);
}catch(Exception e){
Log.error("密码框无法输入--- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void waitFor(String object) throws Exception{
try{
Log.info("等待五秒");
Thread.sleep(5000);
}catch(Exception e){
Log.error("无法等待 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void closeBrowser(String object){
try{
Log.info("关闭并退出浏览器");
driver.quit();
}catch(Exception e){
Log.error("无法关闭浏览器--- " + e.getMessage());
DriverScript.bResult = false;
}
}
}
ExeclUtils.java更新代码如下
package utility;
import config.Constants;
import executionEngine.DriverScript;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
// 设置Excel文件路径,方便读取到文件
public static void setExcelFile(String Path) throws Exception {
try {
FileInputStream ExcelFile = new FileInputStream(Path);
ExcelWBook = new XSSFWorkbook(ExcelFile);
} catch (Exception e){
Log.error("Class Utils | Method setExcelFile | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
}
}
// 读取Excel文件单元格数据
// 新增sheetname参数,这样就可以去读取Test Steps和Test Cases两个工作表的单元格数据
public static String getCellData(int RowNum, int ColNum, String SheetName ) throws Exception{
try{
ExcelWSheet = ExcelWBook.getSheet(SheetName);
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
return CellData;
}catch (Exception e){
Log.error("Class Utils | Method getCellData | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
return"";
}
}
//得到一共多少行数据
public static int getRowCount(String SheetName){
int iNumber=0;
try {
ExcelWSheet = ExcelWBook.getSheet(SheetName);
iNumber=ExcelWSheet.getLastRowNum()+1;
} catch (Exception e){
Log.error("Class Utils | Method getRowCount | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
}
return iNumber;
}
//得到测试用例的行号
public static int getRowContains(String sTestCaseName, int colNum,String SheetName) throws Exception{
int iRowNum=0;
try {
//ExcelWSheet = ExcelWBook.getSheet(SheetName);
int rowCount = ExcelUtils.getRowCount(SheetName);
for (; iRowNum<rowCount; iRowNum++){
if (ExcelUtils.getCellData(iRowNum,colNum,SheetName).equalsIgnoreCase(sTestCaseName)){
break;
}
}
} catch (Exception e){
Log.error("Class Utils | Method getRowContains | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
}
return iRowNum;
}
//计算一个测试用例有多少个步骤
public static int getTestStepsCount(String SheetName, String sTestCaseID, int iTestCaseStart) throws Exception{
try {
for(int i=iTestCaseStart;i<=ExcelUtils.getRowCount(SheetName);i++){
if(!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))){
int number = i;
return number;
}
}
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int number=ExcelWSheet.getLastRowNum()+1;
return number;
} catch (Exception e){
Log.error("Class Utils | Method getRowContains | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
return 0;
}
}
}
DriverScript.java代码如下
package executionEngine;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import config.ActionsKeywords;
import config.Constants;
import org.apache.log4j.xml.DOMConfigurator;
import utility.ExcelUtils;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* create by Anthony on 2018/1/30
*/
public class DriverScript {
// 声明一个public static的类对象,所以我们可以在main方法范围之外去使用。
public static ActionsKeywords actionsKeywords;
public static String sActionKeyword;
// 下面是返回类型是方法,这里用到反射类
public static Method method[];
// 新建一个Properties对象
public static Properties OR;
public static String sPageObject;
public static int iTestStep;
public static int iTestLastStep;
public static String sTestCaseID;
public static String sRunMode;
public static boolean bResult;
// 这里我们初始化'ActionsKeywords'类的一个对象
public DriverScript() throws NoSuchMethodException, SecurityException{
actionsKeywords = new ActionsKeywords();
method = actionsKeywords.getClass().getMethods();
}
public static void main(String[] args) throws Exception {
// 这样一定要加,否则报log4j初始化的警告
DOMConfigurator.configure("log4j.xml");
ExcelUtils.setExcelFile(Constants.Path_TestData);
// 创建一个文件输入流对象,参数来源外部OR.txt文件
FileInputStream fs = new FileInputStream(Constants.Path_OR);
// 创建一个Properties对象
OR = new Properties(System.getProperties());
// 加载全部对象仓库文件
OR.load(fs);
// 执行用例
DriverScript startEngine = new DriverScript();
startEngine.execute_TestCase();
}
private void execute_TestCase() throws Exception {
//获取测试用例数量
int iTotalTestCases = ExcelUtils.getRowCount(Constants.Sheet_TestCases);
//外层for循环,有多少个测试用例就执行多少次循环
for(int iTestcase=1;iTestcase<=iTotalTestCases;iTestcase++){
//从Test Case表获取测试ID
sTestCaseID = ExcelUtils.getCellData(iTestcase, Constants.Col_TestCaseID, Constants.Sheet_TestCases);
//获取当前测试用例的Run Mode的值
sRunMode = ExcelUtils.getCellData(iTestcase, Constants.Col_RunMode,Constants.Sheet_TestCases);
// Run Mode的值控制用例是否被执行
if (sRunMode.equals("Yes")){
// 只有当Run Mode的单元格数据是Yes,下面代码才会被执行
iTestStep = ExcelUtils.getRowContains(sTestCaseID, Constants.Col_TestCaseID, Constants.Sheet_TestSteps);
iTestLastStep = ExcelUtils.getTestStepsCount(Constants.Sheet_TestSteps, sTestCaseID, iTestStep);
//下面这个for循环的次数就等于测试用例的步骤数
for (;iTestStep <= iTestLastStep;iTestStep++){
sActionKeyword = ExcelUtils.getCellData(iTestStep, Constants.Col_ActionKeyword,Constants.Sheet_TestSteps);
sPageObject = ExcelUtils.getCellData(iTestStep, Constants.Col_PageObject, Constants.Sheet_TestSteps);
execute_Actions();
}
}
}
}
private static void execute_Actions() throws Exception {
for(int i=0;i<method.length;i++){
if(method[i].getName().equals(sActionKeyword)){
method[i].invoke(actionsKeywords,sPageObject);
break;
}
}
}
}
前面几篇提到的用户名和密码输入报错这块,这里把关键字方法调整了下,而且excel的action_keyword这列也调整了,两个地方名称保持一样。
本篇能完整运行的代码已经上传百度云,请点击这里,进去找到第十篇的压缩文档。
一个异常其实就是一个事件,特点就是在程序执行过程中出现的,会打断正常的程序设计流程。简单来说,在自动化测试中,任何一个错误,导致你执行用例失败,这个就是一个异常。
错误和异常的区别
一个错误,一般是验证的问题,是无法通过try catch捕获的。异常是可以想象得到的报错,可以手动捕获并处理。一行代码如果发生错误,例如不符合语法规范,那么根本通不过编译,但是一个异常更多是由于没有考虑到特殊场景,而没有去处理。
什么是异常处理
异常处理首先需要通过预先想到可能存在的错误,通过代码块语句,处理这个异常对象,并帮助程序开发人员对这个异常进行一些改变处理,目的是不会中断程序正常运行。
为什么异常处理非常重要
1) 试想一下这个场景,当你得到一个异常而且你想打印出一些自定义的消息,所以别人通过这个自定义消息内容,明白这个是什么意思。
2) 有些时候,我们需要得到某些特定异常的步骤,方便测试人员多测试这块。
3)实际一些,我们在selenium自动化测试中,你可能想处理以下异常:
ElementNotSelectableException,
ElementNotVisibleException,
NoSuchElementException。
Selenium中不同的异常
下面列举一些常见的异常,这些异常在selenium自动化测试中可能遇到。
1.NoSuchElementException: 意思就是当前定位元素方法无法找到元素,很有可能元素定位表达式写错了
2.StaleElementReferenceException: 意思就是这个元素不在当前DOM页面显示
3.TimeoutException:意思就是执行失败,因为在足够多默认时间内没有结束这个操作
4.ElementNotVisibleException: 意思就是,尽管元素在当前DOM页面存在,但是它是不可见的,所以无法产生交互。
5.ElementNotSelectableException:意思就是,可能元素不可用,所以无法选择到。
如何处理异常
在Java中使用try-catch语句进行异常处理。语法格式如下:
Try{
//一些代码
}catch(Exception e) {
//处理异常的代码块
}
多个Catch 语句块:一个try代码块可以跟着多个catch代码块。 try{ //Somecode}catch(ExceptionType1e1){ //Codefor Handling the Exception 1}catch(ExceptionType2e2){ //Codefor Handling the Exception 2 }
原则上是没有catch语句块个数的限定,上面举例写了两个,实际可以写多个。出现异常就会从第一个catch里面去匹配,如果匹配到就走第一个catch的异常处理代码,如果没有,接着匹配第二个catch里面的异常名称,以此类推。
Throw:有些时候,我们希望在代码中显示生成异常。举例,在Selenium自动化测试框架中,我们很多时候都是打印自己写的log,一旦我们捕获到异常,我们需要把这个异常抛出给系统,这样测试用例才会终止执行。Throw关键字用于运行时抛出异常并处理它。
Throws:如果我们在抛出一个异常而且没有处理这个异常,那么我们需要使用throws关键字。掉调用程序知道这个方法可能抛出异常,但是不会处理。
Finally:Finally关键字用来创建代码块去处理一个分支流程,这个finally代码块永远会执行,不管有没有异常发生。
在框架中处理异常
通过前面文章介绍,我们已经知道,我们在driverscript.java这个类中写了一个嵌套for循环,外层for循环来控制用例,内层for循环来控制每个用例下的测试步骤。那么我们怎么在这个执行过程中处理异常呢。我们是这样想的,如果在外层循环遇到执行测试用例失败,那么就在这个时刻处理异常,如果内层循环执行具体某一个步骤出现失败,也处理异常。所以,我们需要定义一个标记,类型是布尔型,如果标记为true,测试用例和测试步骤继续执行,如果遇到标记为false,那么就停下来,不继续执行后面的用例。
例如我在openBrowser()这个方法,这样去写异常处理。
public static void openBrowser(String object) {
// 这里,我们暂时都写死用chrome来进行自动化测试
try{
Log.info("启动chrome浏览器。");
System.setProperty("webdriver.chrome.driver",".\\libs\\chromedriver.exe");
driver = new ChromeDriver();
}catch(Exception e){
Log.info("无法启动浏览器 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
上面意思就是,一般来说bResult这个变量是true,测试用例正常被执行,如果出现异常,那么就从true改成false,这样下面其他用例看到这个标记变了就不会继续执行。
ActionsKeywords.java更新代码如下
package config;
import executionEngine.DriverScript;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import utility.Log;
import static executionEngine.DriverScript.OR;
import java.util.concurrent.TimeUnit;
public class ActionsKeywords {
public static WebDriver driver;
public static void openBrowser(String object) {
// 这里,我们暂时都写死用chrome来进行自动化测试
try{
Log.info("启动chrome浏览器。");
System.setProperty("webdriver.chrome.driver",".\\libs\\chromedriver.exe");
driver = new ChromeDriver();
}catch(Exception e){
Log.info("无法启动浏览器 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void openUrl(String object) {
try{
Log.info("打开测试环境地址");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(Constants.URL);
}catch(Exception e){
Log.info("无法打开测试环境地址 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void click(String object) {
try{
Log.info("点击元素: "+ object);
driver.findElement(By.xpath(OR.getProperty(object))).click();
}catch(Exception e){
Log.error("无法点击元素--- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void inputUsername(String object){
try{
Log.info("在用户名输入框输入文字");
driver.findElement(By.xpath(OR.getProperty(object))).sendKeys(Constants.UserName);
}catch(Exception e){
Log.error("无法输入用户名 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void inputPassword(String object){
try{
Log.info("密码框输入...");
driver.findElement(By.xpath(OR.getProperty(object))).sendKeys(Constants.Password);
}catch(Exception e){
Log.error("密码框无法输入--- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void waitFor(String object) throws Exception{
try{
Log.info("等待五秒");
Thread.sleep(5000);
}catch(Exception e){
Log.error("无法等待 --- " + e.getMessage());
DriverScript.bResult = false;
}
}
public static void closeBrowser(String object){
try{
Log.info("关闭并退出浏览器");
driver.quit();
}catch(Exception e){
Log.error("无法关闭浏览器--- " + e.getMessage());
DriverScript.bResult = false;
}
}
}
ExeclUtils.java更新代码如下
package utility;
import config.Constants;
import executionEngine.DriverScript;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
// 设置Excel文件路径,方便读取到文件
public static void setExcelFile(String Path) throws Exception {
try {
FileInputStream ExcelFile = new FileInputStream(Path);
ExcelWBook = new XSSFWorkbook(ExcelFile);
} catch (Exception e){
Log.error("Class Utils | Method setExcelFile | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
}
}
// 读取Excel文件单元格数据
// 新增sheetname参数,这样就可以去读取Test Steps和Test Cases两个工作表的单元格数据
public static String getCellData(int RowNum, int ColNum, String SheetName ) throws Exception{
try{
ExcelWSheet = ExcelWBook.getSheet(SheetName);
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
return CellData;
}catch (Exception e){
Log.error("Class Utils | Method getCellData | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
return"";
}
}
//得到一共多少行数据
public static int getRowCount(String SheetName){
int iNumber=0;
try {
ExcelWSheet = ExcelWBook.getSheet(SheetName);
iNumber=ExcelWSheet.getLastRowNum()+1;
} catch (Exception e){
Log.error("Class Utils | Method getRowCount | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
}
return iNumber;
}
//得到测试用例的行号
public static int getRowContains(String sTestCaseName, int colNum,String SheetName) throws Exception{
int iRowNum=0;
try {
//ExcelWSheet = ExcelWBook.getSheet(SheetName);
int rowCount = ExcelUtils.getRowCount(SheetName);
for (; iRowNum<rowCount; iRowNum++){
if (ExcelUtils.getCellData(iRowNum,colNum,SheetName).equalsIgnoreCase(sTestCaseName)){
break;
}
}
} catch (Exception e){
Log.error("Class Utils | Method getRowContains | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
}
return iRowNum;
}
//计算一个测试用例有多少个步骤
public static int getTestStepsCount(String SheetName, String sTestCaseID, int iTestCaseStart) throws Exception{
try {
for(int i=iTestCaseStart;i<=ExcelUtils.getRowCount(SheetName);i++){
if(!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))){
int number = i;
return number;
}
}
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int number=ExcelWSheet.getLastRowNum()+1;
return number;
} catch (Exception e){
Log.error("Class Utils | Method getRowContains | Exception desc : "+e.getMessage());
DriverScript.bResult = false;
return 0;
}
}
}
DriverScript.java代码如下
package executionEngine;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import config.ActionsKeywords;
import config.Constants;
import org.apache.log4j.xml.DOMConfigurator;
import utility.ExcelUtils;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* create by Anthony on 2018/1/30
*/
public class DriverScript {
// 声明一个public static的类对象,所以我们可以在main方法范围之外去使用。
public static ActionsKeywords actionsKeywords;
public static String sActionKeyword;
// 下面是返回类型是方法,这里用到反射类
public static Method method[];
// 新建一个Properties对象
public static Properties OR;
public static String sPageObject;
public static int iTestStep;
public static int iTestLastStep;
public static String sTestCaseID;
public static String sRunMode;
public static boolean bResult;
// 这里我们初始化'ActionsKeywords'类的一个对象
public DriverScript() throws NoSuchMethodException, SecurityException{
actionsKeywords = new ActionsKeywords();
method = actionsKeywords.getClass().getMethods();
}
public static void main(String[] args) throws Exception {
// 这样一定要加,否则报log4j初始化的警告
DOMConfigurator.configure("log4j.xml");
ExcelUtils.setExcelFile(Constants.Path_TestData);
// 创建一个文件输入流对象,参数来源外部OR.txt文件
FileInputStream fs = new FileInputStream(Constants.Path_OR);
// 创建一个Properties对象
OR = new Properties(System.getProperties());
// 加载全部对象仓库文件
OR.load(fs);
// 执行用例
DriverScript startEngine = new DriverScript();
startEngine.execute_TestCase();
}
private void execute_TestCase() throws Exception {
//获取测试用例数量
int iTotalTestCases = ExcelUtils.getRowCount(Constants.Sheet_TestCases);
//外层for循环,有多少个测试用例就执行多少次循环
for(int iTestcase=1;iTestcase<=iTotalTestCases;iTestcase++){
//从Test Case表获取测试ID
sTestCaseID = ExcelUtils.getCellData(iTestcase, Constants.Col_TestCaseID, Constants.Sheet_TestCases);
//获取当前测试用例的Run Mode的值
sRunMode = ExcelUtils.getCellData(iTestcase, Constants.Col_RunMode,Constants.Sheet_TestCases);
// Run Mode的值控制用例是否被执行
if (sRunMode.equals("Yes")){
// 只有当Run Mode的单元格数据是Yes,下面代码才会被执行
iTestStep = ExcelUtils.getRowContains(sTestCaseID, Constants.Col_TestCaseID, Constants.Sheet_TestSteps);
iTestLastStep = ExcelUtils.getTestStepsCount(Constants.Sheet_TestSteps, sTestCaseID, iTestStep);
//下面这个for循环的次数就等于测试用例的步骤数
for (;iTestStep <= iTestLastStep;iTestStep++){
sActionKeyword = ExcelUtils.getCellData(iTestStep, Constants.Col_ActionKeyword,Constants.Sheet_TestSteps);
sPageObject = ExcelUtils.getCellData(iTestStep, Constants.Col_PageObject, Constants.Sheet_TestSteps);
execute_Actions();
}
}
}
}
private static void execute_Actions() throws Exception {
for(int i=0;i<method.length;i++){
if(method[i].getName().equals(sActionKeyword)){
method[i].invoke(actionsKeywords,sPageObject);
break;
}
}
}
}
前面几篇提到的用户名和密码输入报错这块,这里把关键字方法调整了下,而且excel的action_keyword这列也调整了,两个地方名称保持一样。
本篇能完整运行的代码已经上传百度云,请点击这里,进去找到第十篇的压缩文档。
相关文章推荐
- 关键字驱动框架入门教程-1-介绍
- 关键字驱动框架入门教程-8-测试套件执行引擎
- 关键字驱动框架入门教程-9-用log4j输出日志
- 关键字驱动框架入门教程-13-项目打包交付给手工测试人员
- 关键字驱动框架入门教程-5-用Java反射机制优化
- 关键字驱动框架入门教程-7-对象仓库
- 关键字驱动框架入门教程-12-数据驱动技术
- 关键字驱动框架入门教程-11-测试结果报告
- 关键字驱动框架入门教程-2-写关键字框架的步骤
- 关键字驱动框架入门教程-6-建立常数变量
- 关键字驱动框架入门教程-3-识别并设计实现动作关键字
- 关键字驱动框架入门教程-4-搭建数据引擎-用Apache POI去读取数据
- Struts1.x系列教程(10):Validator验证框架入门
- Struts1.x系列教程(10):Validator验证框架入门
- 区块链技术与以太坊开源加密货币开发框架入门到精通视频教程附源码与讲义 10课
- Struts1.x系列教程(10):Validator验证框架入门
- Struts1.x系列教程(10):Validator验证框架入门
- linux驱动摸索 --驱动框架初始化(结合韦东山视频教程)
- 微信公众平台入门开发教程.Net(C#)框架
- Python 入门教程 10 ---- Student Becomes the Teacher