前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium学习笔记之外部化相关测试数据---xml

Selenium学习笔记之外部化相关测试数据---xml

作者头像
流柯
发布2018-08-30 17:08:31
3000
发布2018-08-30 17:08:31
举报
文章被收录于专栏:流柯技术学院流柯技术学院

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

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <UIMap>
 3   <Object ID="User Name">
 4     <Attributes Locator="userName" Page="Main Page" Type="Button"/>
 5   </Object>
 6   
 7   <Object ID="Password">
 8     <Attributes Locator="Password" Page="Main Page" Type="Button"/>
 9   </Object>
10 </UIMap>

相应的解析xml的代码:

 1     public static String getLocator(String locatorID){
 2         InputStream ins=Thread.currentThread().getContextClassLoader()
 3             .getResourceAsStream(FileConstants.XMLFILE_NAME);
 4         if(ins==null){
 5             System.out.println("Missing UIMap.xml file.");
 6             return null;
 7         }
 8         
 9         DocumentBuilderFactory fac=DocumentBuilderFactory.newInstance();
10         DocumentBuilder builder=null;
11         try {
12             builder = fac.newDocumentBuilder();
13         } catch (ParserConfigurationException pce) {
14             System.out.println("Failing to new DocumentBuilder for runtime exception.");
15             throw new RuntimeException(pce);
16         }
17         
18         Document xmlDoc=null;
19         try {
20             xmlDoc = builder.parse(ins);
21         } catch (SAXException se) {
22             System.out.println("Failing to parse xml file for runtime exception.");
23             throw new RuntimeException(se);
24         } catch (IOException ie) {
25             System.out.println("Failing to parse xml file for runtime exception.");
26             throw new RuntimeException(ie);
27         }
28         
29         XPathFactory pathFac=XPathFactory.newInstance();
30         XPath xpath = pathFac.newXPath();
31         
32         XPathExpression exp=null;
33         try {
34             exp = xpath.compile("UIMap/Object[@ID='"+locatorID+"']/Attributes");
35         } catch (XPathExpressionException e) {
36             System.out.println("Failing to get locator for :"+locatorID);    
37         }
38         Node node=null;
39         try {
40             node = (Node)exp.evaluate(xmlDoc, XPathConstants.NODE);
41         } catch (XPathExpressionException e) {
42             
43             e.printStackTrace();
44         }finally{
45             try{
46                 if(ins!=null){
47                     ins.close();
48                 }
49             }catch(Exception ex){
50                 System.out.println("Failing to load UIMap.xml for runtime exception.");
51                 throw new RuntimeException(ex);
52             }
53         }
54         
55         return node.getAttributes().getNamedItem("Locator").getNodeValue();
56     }

测试代码:

1 selenium.type(UIMapParser.getLocator("UserName"), "seleniumtest");
2 selenium.type(UIMapParser.getLocator("Password"), "seleniumtest");
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-01-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档