Selenium学习笔记之四:外部化相关测试数据

    技术2024-07-08  73

    在写测试代码时,把locator值直接写到测试代码里面,不具备良好的可读性,同时,后期也不利于代码的维护。如下测试代码:

    selenium.type("userName", "seleniumtest"); selenium.type("password", "seleniumtest");

    如果在某个build中,控件"User Name"的属性"userName"被开发人员重新改变了,这个时候,测试代码也要做相应的改动。如果把该值外部化到一个文件中,我们仅仅需要修改该文件,不需要对代码进行修改;同时,也利用以后的本地化测试。

     

    最简单的,我们可以利用java.util.Properties,把相关的locator都定义在一个UIMap.properties文件中,如下:

    #input button User Name UserName=userName #input button Password Password=password

     

    构建UIMapParser类,提供统一的API来读取locator属性值:

    package test.common; import java.util.Properties; import java.io.InputStream; import java.io.IOException; public class UIMapParser { private static Properties UIMap=null; private UIMapParser(){ } private static synchronized void initialize(){ InputStream ins=Thread.currentThread().getContextClassLoader() .getResourceAsStream(FileConstants.PROPFILE_NAME); if(ins==null){ System.out.println("Missing UIMap.properties file."); return; } UIMap=new Properties(); try{ UIMap.load(ins); }catch(IOException ioe){ System.out.println("Failing to load UIMap.properties file."); throw new RuntimeException(ioe); }finally{ try{ if(ins!=null){ ins.close(); } }catch(Exception ex){ System.out.println("Failing to load UIMap.properties for runtime exception."); throw new RuntimeException(ex); } } } public static synchronized Properties getUIMap() { initialize(); return UIMap; } } class FileConstants{ public final static String PROPFILE_NAME="UIMap.properties"; }

    于是,相应的测试代码可以改为:

    selenium.type(UIMapParser.getUIMap().getProperty("UserName"), "seleniumtest"); selenium.type(UIMapParser.getUIMap().getProperty("Password"), "seleniumtest");

     

    当然,我们也可以用xml来定义一个信息更为丰富的UIMap.xml文件,比如,额外还添加控件所属的页,控件的类型,然后解析构建一个XMLParser类来读取相应的值。

     <?xml version="1.0" encoding="utf-8" ?> <UIMap> <Object ID="User Name"> <Attributes Locator="userName" Page="Main Page" Type="Button"/> </Object> <Object ID="Password"> <Attributes Locator="Password" Page="Main Page" Type="Button"/> </Object> </UIMap>

     

    添加相应的解析xml的代码:

    public static String getLocator(String locatorID){ InputStream ins=Thread.currentThread().getContextClassLoader() .getResourceAsStream(FileConstants.XMLFILE_NAME); if(ins==null){ System.out.println("Missing UIMap.xml file."); return null; } DocumentBuilderFactory fac=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=null; try { builder = fac.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.out.println("Failing to new DocumentBuilder for runtime exception."); throw new RuntimeException(pce); } Document xmlDoc=null; try { xmlDoc = builder.parse(ins); } catch (SAXException se) { System.out.println("Failing to parse xml file for runtime exception."); throw new RuntimeException(se); } catch (IOException ie) { System.out.println("Failing to parse xml file for runtime exception."); throw new RuntimeException(ie); } XPathFactory pathFac=XPathFactory.newInstance(); XPath xpath = pathFac.newXPath(); XPathExpression exp=null; try { exp = xpath.compile("UIMap/Object[@ID='"+locatorID+"']/Attributes"); } catch (XPathExpressionException e) { System.out.println("Failing to get locator for :"+locatorID); } Node node=null; try { node = (Node)exp.evaluate(xmlDoc, XPathConstants.NODE); } catch (XPathExpressionException e) { e.printStackTrace(); }finally{ try{ if(ins!=null){ ins.close(); } }catch(Exception ex){ System.out.println("Failing to load UIMap.xml for runtime exception."); throw new RuntimeException(ex); } } return node.getAttributes().getNamedItem("Locator").getNodeValue(); }

     

     相应的,测试代码变成:

    selenium.type(UIMapParser.getLocator("UserName"), "seleniumtest"); selenium.type(UIMapParser.getLocator("Password"), "seleniumtest");

    最新回复(0)