• UI自动化测试框架之Selenium关键字驱动


    一、原理及特点 1. 关键字驱动测试是数据驱动测试的一种改进类型 2. 主要关键字包括三类:被操作对象(Item)、操作(Operation)和值(value),用面向对象形式可将其表现为Item.Operation(Value) 3. 将测试逻辑按照这些关键字进行分解,形成数据文件。 4. 用关键字的形式将测试逻辑封装在数据文件中,测试工具只要能够解释这些关键字即可对其应用自动化 二、准备 使用工具:eclipse 用到的第三方jar包:poi.jar(操作excel);selenium.jar 理解难点:java反射机制;逐步分层 三、框架构思 1、编写脚本 首先我们来写一个登陆开源中国的脚本 public class Login_Script { public static WebDriver driver=null; public static void main(String []agrs) throws InterruptedException{ // 启动火狐浏览器 driver= new FirefoxDriver(); // 最大化 driver.manage().window().maximize(); // 打开开源中国网址 driver.get("http://www.oschina.net/"); // 点击登录 driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[1]")).click(); // 输入用户名 driver.findElement(By.xpath("//*[@id='f_email']")).sendKeys("XXXXXXB"); // 输入密码 driver.findElement(By.xpath("//*[@id='f_pwd']")).sendKeys("XXXXXXXA"); // 点击登录按钮 // driver.findElement(By.xpath("//*[@id='login_osc']/table/tbody/tr[7]/td/input")).click(); // Thread.sleep(30); // 点击退出按钮 driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[3]")).click(); // 关闭浏览器 driver.quit(); } } 2、脚本分析 这是登陆的场景 操作步骤 第一步:启动浏览器 第二步:输入网址 第四步:点击登录 第五步:输入用户名 第六步:输入密码 第七步:点击登录按钮 第八步:点击退出 第九步:关闭浏览器 3、使用excel 建立一个excel 在java中创建一个操作excel的类 ,主要实现是对excel的读和写,主要代码如下: public class ExcelUtils { public static HSSFSheet ExcelSheet; public static HSSFWorkbook ExcelBook; public static HSSFRow Row; public static HSSFCell Cell; public static void setExcelFile(String Path,String SheetName) throws Exception{ FileInputStream ExcelFile=new FileInputStream(Path); ExcelBook=new HSSFWorkbook(ExcelFile); ExcelSheet=ExcelBook.getSheet(SheetName); } public static void setCellData(String Result, int RowNum, int ColNum,String Path) throws Exception{ Row = ExcelSheet.getRow(RowNum); Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL); if (Cell == null) { Cell = Row.createCell(ColNum); Cell.setCellValue(Result); } else { Cell.setCellValue(Result); } FileOutputStream fileOut = new FileOutputStream(Path); ExcelBook.write(fileOut); fileOut.flush(); fileOut.close(); } public static String getCellDate(int RowNum,int CloNum){ Cell=ExcelSheet.getRow(RowNum).getCell(CloNum); String cellData=Cell.getStringCellValue(); return cellData; } } 4、新建一个ActionKeyWords类 public class ActionKeyWords { public static WebDriver driver=null; // 启动浏览器并最大化 public static void OpenBrowser (){ driver= new FirefoxDriver(); driver.manage().window().maximize(); } // 打开开源中国网址 public static void Navigate (){ driver.get("http://www.oschina.net/"); } // 点击登录 public static void Login_Click (){ driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[1]")).click(); } // 输入用户名 public static void Input_Name (){ driver.findElement(By.xpath("//*[@id='f_email']")).sendKeys("XXXXXXA"); } // 输入密码 public static void Input_Password (){ driver.findElement(By.xpath("//*[@id='f_pwd']")).sendKeys("XXXXXXB"); } // 点击登录按钮 public static void Login_Button (){ driver.findElement(By.xpath("//*[@id='login_osc']/table/tbody/tr[7]/td/input")).click(); } // 点击退出按钮 public static void Logout_Click (){ driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[3]")).click(); } // 关闭浏览器 public static void CloseBrowser (){ driver.quit(); } } 5、修改Login_Script脚本. public class Login_Script { public static void main(String []agrs) throws Exception{ ExcelUtils.setExcelFile("D:\data\TestData.xls", "steps"); ActionKeyWords actionKeyWords= new ActionKeyWords(); String Keywords=null; for(int RowNum=1;RowNum<=ExcelUtils.getLastRowNums();RowNum++){ Keywords=ExcelUtils.getCellDate(RowNum, 3); if(Keywords.trim().equals("OpenBrowser")){ actionKeyWords.OpenBrowser(); }else if(Keywords.trim().equals("Navigate")){ actionKeyWords.Navigate(); }else if(Keywords.trim().equals("Login_Click")){ actionKeyWords.Login_Click(); }else if(Keywords.trim().equals("Input_Name")){ actionKeyWords.Input_Name(); }else if(Keywords.trim().equals("Input_Password")){ actionKeyWords.Input_Password(); }else if(Keywords.trim().equals("Login_Button")){ actionKeyWords.Login_Button(); }else if(Keywords.trim().equals("Logout_Click")){ actionKeyWords.Logout_Click(); }else if(Keywords.trim().equals("CloseBrowser")){ actionKeyWords.CloseBrowser(); } } } } 这样代码的框架就基本已经搭建起来了,代码结构如下: 四、结构优化 1、优化Login_Script 类中的代码 注:这里用到了反射机制 public class Login_Script { public static ActionKeyWords actionKeyWords; public static String Keywords=null; public static Method[] method; public Login_Script(){ actionKeyWords= new ActionKeyWords(); method=actionKeyWords.getClass().getMethods(); } public static void main(String []agrs) throws Exception{ ExcelUtils.setExcelFile("D:\data\TestData.xls", "steps"); new Login_Script(); for(int RowNum=1;RowNum<=ExcelUtils.getLastRowNums();RowNum++){ Keywords=ExcelUtils.getCellDate(RowNum, 3); login_action(); } } public static void login_action(){ for(int i=0;i
  • 相关阅读:
    Thinking in Java Reading Note(9.接口)
    Thinking in java Reading Note(8.多态)
    Thinking in Java Reading Note(7.复用类)
    SQL必知必会
    Thinking in Java Reading Note(5.初始化与清理)
    Thinking in Java Reading Note(2.一切都是对象)
    鸟哥的Linux私房菜笔记(1.基础)
    Thinking in Java Reading Note(1.对象导论)
    CoreJava2 Reading Note(2:I/O)
    CoreJava2 Reading Note(1:Stream)
  • 原文地址:https://www.cnblogs.com/dengnapianhuahai/p/5748542.html
Copyright © 2020-2023  润新知