我们也可以用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");

Selenium学习笔记之外部化相关测试数据---xml的更多相关文章

  1. java学习笔记12--国际化

    java学习笔记12--国际化 国际化的操作就是指一个程序可以同时适应多门语言,即:如果现在程序者是中国人,则会以中文为显示文字,如果现在程序的使用者是英国人,则会以英语为显示的文字,也就是说可以通过 ...

  2. CNN学习笔记:池化层

    CNN学习笔记:池化层 池化 池化(Pooling)是卷积神经网络中另一个重要的概念,它实际上是一种形式的降采样.有多种不同形式的非线性池化函数,而其中“最大池化(Max pooling)”是最为常见 ...

  3. amazeui学习笔记--css(布局相关1)--网格Grid

    amazeui学习笔记--css(布局相关1)--网格Grid 一.总结 基本使用 1.div+class布局:amaze里面采取的就是div+class的布局方式  <div class=&q ...

  4. amazeui学习笔记--css(布局相关3)--辅助类Utility

    amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...

  5. amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid

    amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid 一.总结 1.与grid区别:网格中:am-g + am-u-xx-n 等分网格中只有一个: am-avg-sm-4(在u ...

  6. stm32寄存器版学习笔记03 外部中断

    stm32的每个I/O口都可以作为中断输入,要把I/O口设置为外部中断输入,必须将I/O口设置为上拉/下拉输入 或 浮空输入(但浮空的时候外部一定要带上拉或下拉电阻,否则可能导致 中断不停的触发),干 ...

  7. Selenium 学习笔记(一)

    selenium 学习整理 初学者,如果有不当得地方请指出,非常感谢. 准备事项: 1. Python 安装包 安装Python,并勾选添加环境变量. 安装完成后,打开dos窗口,输入python,看 ...

  8. STM32学习笔记(九) 外部中断,待机模式和事件唤醒

    学会知识只需要不段的积累和提高,但是如何将知识系统的讲解出来就需要深入的认知和系统的了解.外部中断和事件学习难度并不高,不过涉及到STM32的电源控制部分,还是值得认真了解的,在本文中我将以实际代码为 ...

  9. selenium 学习笔记 ---新手学习记录(1) 问题总结

    说明:每次学习各种语言时,环境搭建访问国外网址最头疼了,现在只要是工具下载好放到自己网盘,可以随时用. 1.首先工具准备,selenium需要用到的 下载地址 访问密码 ff8f 2.我选择的语言时j ...

随机推荐

  1. 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析

    许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...

  2. Sublime Text2 Jsformat自定义使用之代码折叠方式修改

    将代码括号的折叠方式从 function abc(){ } 变成 function abc() { } 打开 Setting-user,把setting-default里的文本全部复制过来. 然后 将 ...

  3. 【原创】.NET读写Excel工具Spire.Xls使用(5)重量级的Excel图表功能

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...

  4. php分享(三十六)mysql中关联表更新

    一:关联不同的表更新 1: 通过where关联更新 update student s, city c set s.province_name = c.province_name, s.city_nam ...

  5. tomcat server容器解读

    1. server的实例类为:org.apache.catalina.core.StandardServer为顶层容器. 2.二级容器GlobalNamingResources,设置认证用户信息. & ...

  6. angularjs学习总结 详细教程(转载)

    1 前言 前端技术的发展是如此之快,各种优秀技术.优秀框架的出现简直让人目不暇接,紧跟时代潮流,学习掌握新知识自然是不敢怠慢. AngularJS是google在维护,其在国外已经十分火热,可是国内的 ...

  7. C++二维码相关库编译

    一.瞎想 坐在地铁上闲来无聊,突然想到了二维码,顺手就百度了下相关的资料,目前C++二维码相关的库不多,也就zbar(开源中国上下了半天也没下载下来).zxing,不过这两个库据说都是解析二维码的,不 ...

  8. libqxt编译

    一.说明 编译环境:win10.qt5.6.1-1.vs2013和libqxt源码(从git上下载) libqxt:libqxt 关于libqxt的说明,请到libqxt的官网阅读,说着看图1,图1是 ...

  9. LeetCode - Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  10. 【转载】Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...