8.3.5 开发公共接口
具体步骤如下。
(1)由于框架运行在不同的浏览器,因此需要创建一个驱动公共类,实例化不同的浏览器:
public WebDriver getWebDriverClient() { //从配置文件获取浏览器类型 String browserType = DriverProp.getProperty("BrowserCoreType"); if (webdriver == null) { if (browserType.equals("Firefox")) { try { //启动Firefox webdriver = new FirefoxDriver(); return webdriver; }catch(Exception e) { logger.info("启动异常:"+e.getMessage()); } } if (browserType.equals("Chrome")) { try{ //启动Chrome System.setProperty("webdriver.chrome.driver", DriverProp.getProperty("DriverPath")); webdriver=new ChromeDriver(); return webdriver; }catch(Exception e) { logger.info("启动异常:"+e.getMessage()); } } if (browserType.equals("IE")) { try { //启动IE System.setProperty("webdriver.ie.driver", DriverProp.getProperty("DriverPath")); DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); dc.setCapability( InternetExplorerDriver .INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); //IE默认启动保护模式,关闭保护模式, dc.setCapability("ignoreProtectedModeSettings", true); webdriver = new InternetExplorerDriver(dc); webdriver.manage().window().maximize(); return webdriver; }catch(Exception e) { logger.info("启动异常:"+e.getMessage()); } } } return webdriver; } |
(2)由于测试工程师在脚本开发中会用到WebDriver API,需要对WebDriver API的基本方法进行二次封装,便于测试工程师开发脚本。
(3)脚本中会使用到不同类型的数据库操作,包含MySQL、Oracle、SqlServer等数据库操作,需要一组统一的方法封装各种数据库的增加、删除、修改和查询操作,具体实现此处不再描述。
(4)在脚本开发中需要从Excel中获取数据或者回填数据,可以使用下面3种方法来处理Excel的数据。
GetItem(String fileName)。从Excel中获取相应字段的数据,fileName为字段名称:
public String GetItem(String fieldName){ int row=0; try { row = hashmap.get(fieldName); } catch (Exception e) { driver.handleFailure("获取数据字段失败:"+fieldName); e.printStackTrace(); } return excelUtil .ReadExcel(testCasePath,sheetName,col,row); } SetItem(String fileName,String setData)。回填数据到字段名为fileName的Excel: publicvoidSetItem(String fieldName,String setData){ int row=0; try { row = hashmap.get(fieldName); } catch (Exception e) { driver.handleFailure("回填数据字段为:"+fieldName); e.printStackTrace(); } excelUtil.UpdateExcel(testCasePath, testCasePath, sheetName, col, row, setData); } isExist(String FileName)。判断Excel中是否有fileName这个字段名称: publicboolean isExist(String fieldName){ return hashmap.containsKey(fieldName); } |
(5)编写测试脚本需要继承的基础类,其中包含该组件的数据。因为业务不同,需重写CommonMethod()方法,因此在本文实例中,并未具体实现CommonMethod()定义业务组件的操作。代码如下所示:
public class Action { String testCasePath; String sheetName; public oTestData otestdata; public MyWebDriver MyDriver; public Page page; /** * 在构造函数中初始化测试数据、驱动程序、公共方法等实例 * @param driver 驱动程序 * @param testCasePath 测试数据路径 * @param sheetName Excel中测试组件名称 * @param dataList 测试数据 * @return */ public Action(AutoTestUtil driver,String testCasePath, String sheetName, int dataList) { this.testCasePath = testCasePath; this.sheetName = sheetName; HashMap<String, Integer> hm = getRowWithTestData (testCasePath, sheetName); //实例化WebDriver对象 MyDriver = new MyWebDriver(driver); page= new Page(MyDriver); //实例化测试数据 otestdata = new oTestData (testCasePath,sheetName,dataList,hm); } /** * 获取数据字段所在行的位置,返回 * @param testCasePath * @param sheetName * @return */ public HashMap<String, Integer> getRowWithTestData (String testCasePath,String sheetName){ ExcelUtil excelUtil = new ExcelUtil(); String testData=""; int rows=0; HashMap<String, Integer> hm= new HashMap<String, Integer>(); do { //根据Excel定义的业务流程,获取测试数据 testData= excelUtil.ReadExcel (testCasePath, sheetName, 0, rows); hm.put(testData, rows); rows=rows+1; } while ( !excelUtil.isEmpty(testCasePath, sheetName, 0, rows) ); return hm; } /** *需要组件脚本重写该方法 * @return */ public void CommonMethod() { } } |
版权声明:51Testing软件测试网获电子工业出版社和作者授权连载本书部分章节。
任何个人或单位未获得明确的书面许可,不得对本文内容复制、转载或进行镜像,否则将追究法律责任。