博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenium学习笔记之外部化相关测试数据---xml
阅读量:6882 次
发布时间:2019-06-27

本文共 2539 字,大约阅读时间需要 8 分钟。

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

1 
2
3
4
5
6 7
8
9
10

相应的解析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");

 

转载地址:http://rgjbl.baihongyu.com/

你可能感兴趣的文章
Android应用程序模拟手机按键
查看>>
变革之心——读后感
查看>>
CMake 教程
查看>>
Linux编辑器vi使用方法详细介绍
查看>>
【Win 10 应用开发】打印UI元素
查看>>
Effective C++:规定12:不要忘了复制的对象时,它的每一个组成部分
查看>>
SSD固态硬盘检测工具AS SSD参数
查看>>
CentOS7.0 安装JAVA周围环境
查看>>
关于windows系统里locale、code page、ANSI编码的问题
查看>>
JQUERY prop与attr差额
查看>>
多媒体流媒体
查看>>
iOS当该装置是水平屏,frame和bounds分别
查看>>
Java这点事
查看>>
快速设计表单样式
查看>>
jquery选择器(原创)<二>
查看>>
C++虚函数virtual,纯虚函数pure virtual和Java抽象函数abstract,接口interface与抽象类abstract class的比较...
查看>>
Android实例-操作摄像头(XE8+小米2)
查看>>
虚拟化(一):虚拟化和vmware产品描述
查看>>
dp related problems (update continuously)
查看>>
git使用总结
查看>>