数据专栏

智能大数据搬运工,你想要的我们都有

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

「深度学习福利」大神带你进阶工程师,立即查看>>>
单例目前有6中实现方式
1、饿汉式 /** * 饿汉式 * 随着类加载便初始化了,基于类加载器,避免的多线程问题,如果不使用这个类会造成资源浪费,占用内存 */ public class SingletonHungry { private static SingletonHungry instacne = new SingletonHungry(); private SingletonHungry(){ } public static SingletonHungry getInstacne() { return instacne; } }
2、懒汉式 /** * 懒汉式 * * 用户第一次调用初始化,节约资源,不用不初始化,在多线程模式下失效 * * */ public class SingletonLazy { private static SingletonLazy instacne; private SingletonLazy(){ } public static SingletonLazy getInstacne() { if(instacne == null) { instacne = new SingletonLazy(); } return instacne; } }
3、懒汉式,获取方法加同步锁 /** * 懒汉式,获取方法加同步锁 * * 为了解决多线程并发问题,这里在获取单例方法前加上了同步,这样在每次获取的时候都需要进行同步,在高并发下这种开销是巨大的 * 而且大多数情况下我们并不会用到同步,量级太重,因此这种方式基本不建议使用 * * */ public class SingletonLazySyn { private static SingletonLazySyn instacne; private SingletonLazySyn(){ } public static synchronized SingletonLazySyn getInstacne() { if(instacne == null) { instacne = new SingletonLazySyn(); } return instacne; } }
4、懒汉式,双检查模式 /** * 懒汉式,双检查模式 * * 双重检查,第一次空是为了解决不必要的同步问题,只有在实例为空的情况下才回去检查,第二次判空是为了实例为空时才创建实例 * * volatile修饰的变量在使用时都会先刷新,也就是将其他线程的工作内存中的数据先同步回主内存,对于执行引擎看不到不一致 * 情况,所以对于多线程来说,不存在不一致问题,是一种轻量级的同步机制 * volatile可以防止指令重排序优化 * * volatile变量的运算在并发下并一样不安全 * */ public class SingletonLazyDCL { private static volatile SingletonLazyDCL instacne; private SingletonLazyDCL(){ } public static SingletonLazyDCL getInstacne() { if(instacne == null) { synchronized(SingletonLazyDCL.class) { if(instacne == null) { instacne = new SingletonLazyDCL(); } } } return instacne; } }

5、静态内部类实现单例 /** * 静态内部类实现单例 * 静态内部类实现单例模式,第一次SingletonStaticInner被加载的时候不会初始化sInstance,只有当getInstance第一次 * 被调用的时候java虚拟机才会加载SingletonHolder * * 一个内部类并不会在其外部类被虚拟机加载的时候被同时加载 * 这种方式保证了懒加载的特性,又保证了多线程并发的问题 */ public class SingletonStaticInner { private SingletonStaticInner(){ } public static SingletonStaticInner getInstance() { return SingletonHolder.sInstance; } private static class SingletonHolder { private static final SingletonStaticInner sInstance = new SingletonStaticInner(); } }
6、枚举单例 /** * 枚举单例 * * 默认枚举的实例创建是线程安全的,并且在任何情况下都是单例。 */ public enum SingletonEnum { INSTANCE; public void doSomeThing() { } }

项目管理
2018-08-19 00:05:00
「深度学习福利」大神带你进阶工程师,立即查看>>> 编译Magic Lantern时,遇到了 /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 错误,但是安装 glibc 却提示已经 already installed and latest version
其实这是因为只能依赖32位的glibc,我使用的系统是Centos7 x64,所以只要安装glibc.32就行了。 yum install glibc.i686
如果还遇到了类似的问题,都可以试着安装下32位的依赖解决问题。
项目管理
2018-08-18 23:28:00
「深度学习福利」大神带你进阶工程师,立即查看>>>
1 说明
本系列博客将详细概述一个透明加密系统的设计及实现过程,其中涉及大量的windows内核知识,本系列博客将从基本的背景开始,详细介绍透明加密系统的发展情况及应用背景,然后介绍相关的内核开发知识,透明加密系统的关键技术解读,透明加密系统的整体设计及实现,以及基本的功能测试、性能测试及兼容性测试。
2 一些问题 博客涉及的程序都是个人学生时代的作品,只是作为分享及参考,不具有实用性; 本人不是Windows操作系统专家,涉及到的操作系统底层的相关知识如果存在问题,欢迎各位大神指导; 有感兴趣的欢迎一起研究,一起完善内核模块,提升技术;
3 系列博客目录 透明加密系统设计及实现-绪论 透明加密系统设计及实现-WINDOWS内核分析及编程方法 透明加密系统设计及实现-透明加密的关键技术
项目管理
2018-08-18 22:47:00
「深度学习福利」大神带你进阶工程师,立即查看>>>
注意:本系列文章内容,均为作者结合自身经验,给出的建议性方案,作为实际应用的参考!如有不严谨或错误之处,敬请指出,不胜感激!
Axure不等于产品经理,它只是一个能够辅助产品经理工作的工具;
Axure不等于交互设计,Axure能够帮助我们直观呈现产品需求,便于沟通;
Axure不等于低保真原型,它也能够在产品论证阶段,通过交互模拟实现对功能模块的可用性测试以及用户体验测试。
Axure不等于高保真原型,Axure在产品经理手中应该有各种不同的应用;
Axure并不像大多数人想象中的样子。
从这一篇开始,我将结合一个虚拟出来的APP产品,对Axure的具体应用,给大家做详细的展示。
让大家能够知道,在产品经理的工作中,Axure的主要应用和不可忽视的作用。
不过,既然是一个APP产品,我们总要先有这个产品的结构信息,才能够在此基础上进行原型以及其他重要图表的绘制。
下面这张图就是使用Xmind整理出来的一个产品信息结构图。(因为是一个虚拟出来的产品,仅作为后面内容的基础,所以并没有过多的考虑功能是否完整,请勿在这方面过于追究。)
我将结合这张图,逐渐的深入,结合各种姿势、技巧,来满足大家的需求。
特别说明:这张图是思维导图软件Xmind中的逻辑图(向右),用这样的图表来呈现产品信息结构,结构上比较清晰,也比较适合用户从左至右、从上至下的阅读习惯。
当我们完成这样的一张图表之后,其实已经可以进行一次产品需求的沟通,例如:与决策层沟通在产品目标定位上是否匹配,是否与商业需求以及市场需求相一致;与开发部门探讨产品研发是否有障碍或难度,以及对开发周期进行粗略的评估。
当沟通得到确认之后,我们就可以着手进行下一步。
在前文中,我通过思维导图整理了一个资讯类APP的产品结构。
不过,在这篇文章中,我将这个结构进行了修正和细化,呈现了完整的产品信息结构。
大家可以对比两次的图表看一下修正与细化的内容。
通过对比,大家能够看出,思维导图软件在我们梳理产品结构的工作中,能够让我们非常容易的进行结构的修改。
这样能够大大的降低之后产品原型的错误率。
这里我把这个使用Xmind制作的思维导图源文件与大家分享,下载地址: https://pan.baidu.com/s/1C6qdXxb2erLOwP7pImIv5Q
注意:转载的文章如果直接点击不能下载,请尝试将下载地址复制到浏览器打开。
在确定了产品信息结构图之后,这里我在思维导图软件中把页面功能与元素收起,这样我们就能看到产品的框架结构。
不过,在实际工作中,我们一般的顺序是先完成产品框架结构,再在此基础上完成产品信息结构。
也就是先做好提纲,再进行细化。
下图中带有向右绿色箭头的分支主题都是页面。
当我们完成了产品信息结构图,确认了产品结构、功能、元素、关联都没有问题之后,接下来,我们就可以依据着这张图表,进行原型页面的创建、页面布局的规划以及页面功能的组织与元素的添加。
下面这张图,就是对照着产品信息结构图创建的原型页面结构。
通过这样的方式所创建的原型页面,在结构上,能够与产品框架结构保持一致,具有清晰的层级关系(图中编号表示层级),并且方便我们进行下一步的工作。
我们开始使用Axure构建页面布局,并且添加每个页面的功能、元素。
我们从第一步开始。
第一步,界定原型的尺寸。
原型的尺寸一般参考设备的逻辑分辨率(注意不是物理分辨率)。
一般来说有以下几种主流逻辑分辨率。
Android:宽度360*高度640
iPhone6/7:宽度375*高度667
iPhone6/7 Plus:宽度414*高度736
那么,我们制作原型时就可以使用这些分辨率,设定原型尺寸。
不过,如果想在手机上全屏显示,建议大家使用Axure Share APP,在应用商店中就有下载。通过F6的发布功能,上传至官方服务器,同步到手机本地后浏览。
这里需要注意,在发布之前,要在【移动设备】设置中勾选【包含视口标签】。
在确定了分辨率之后,我们先在页面中,通过创建全局辅助线,帮助我们确边界。
在页面中,点击鼠标右键,在【栅格和辅助线】的选项中,我们选择创建辅助线。
然后,我们按下图进行设置,创建全局辅助线,这个辅助线能够在所有页面中显示。
设置完辅助线之后,接下来我们进行第二步。
第二步,制作母版。
母版的内容,是多个页面中共有的内容。
这里我们要做的是顶部的状态栏。
在母版功能模块,我们添加母版,并且在母版名称上点击鼠标右键,选择【添加到页面中】。
然后,按下图设置,将母版添加至除了【加载页loading】和【引导页guide】的所有页面中。
母版设置完毕之后,就可以继续进行下一步。
第三步,创建页面。
这里我先把一级页面根据之前做好的产品信息结构图搭建出来。
一般来说,移动端的页面布局都是垂直布局,方便用户浏览。
不过,并不是所有产品都这样。
例如,移动营业厅的APP,业务分类界面中采用的是左右布局,左侧是分类,右侧是相应的内容。
我们应该根据需求,来界定布局,这样才能方便用户使用。 加载页(loading)
引导页(guide)
注:引导页是三张可切换图片,可以放在一个动态面板的三个状态中,通过交互进行切换。 首页(home)
注:这里以首页为例创建页面,把页面功能、元素与思维导图一一对应,之后页面也一样创建。另外,文章列表内容较长,可以放在动态面板中,并在属性中设置【自动显示垂直滚动条】,这样在移动端浏览就可以通过手指上下滑动内容。下面的页面同样处理。 精选页(select)
话题页(topic)
收藏页(favorite)
到这里,一级页面我们就创建完毕了。其他的页面,我们也这样逐一创建出来。
我把这个产品原型所有的页面都进行了梳理,并绘制了线框图。
在绘制过程中,对页面的结构做了一些优化调整和补充。
并且,对思维导图也做了一些细微的调整。
新的思维导图下载地址: http://downloads.iaxure.com/PM_APP_V1.2.xmind
注意:如果点击不能下载,请将网址复制到浏览器中打开。
当完成了这些工作之后,我在页面管理模块中,新增了一个Flow文件夹和一个page_flow页面(见上图)。
接下来,我使用了Axure RP 8中的页面快照元件。页面快照元件能够呈现指定页面的缩略图。
我在page_flow页面中,放入了一个页面快照元件,并且双击指定了要显示缩略图的页面。
这里,我将页面快照元件的宽高设置成了180*320。并且,为了美观,在属性中取消了缩略图四周的填充。
接下来,通过Ctrl+D,把这个元件进行复制,并且双击指向不同的页面。
这样,就把这个原型的所有页面的缩略图都放在了同一个页面中。
并且,通过连接线,将这些页面进行关联,形成了一张完整的页面流程图。
这张图,能够体现原型的整体结构、页面关系以及用户访问路径。
添加连接线,我们需要在顶部功能菜单中,点击【连接】图标。
连接线样式的设置,则是在顶部快捷功能菜单中或者【样式】模块中进行设置。
通过以上步骤,我们就结合页面快照元件以及连接线,完成了页面流程图的绘制。
我们已经完成了产品结构和页面流程、关系的处理。
这一篇,我们继续深入,从业务流程的角度,来看一下Axure能够帮助我们做什么?
首先,在产品经理书写产品需求文档时,会包含用例说明部分。
那么,什么是用例(Use Case)呢?
用例其实是对系统功能的描述,可以理解为使用场景。
在这里,我使用Axure的流程图元件,做了一个用例图,包含了当前这个产品所有的用例内容。
为了避免有些同学对英文比较晕,包含(incloud)和扩展(extend)的关系,我使用中文做了标注。另外,还有一种泛化的关系,使用的是白色的三角箭头(如:搜索文章)。
这三种用例间的关系,我是这么进行理解的。
包含:是指一个使用场景(基用例)的过程中,必须包含另外一个使用场景(子用例);子用例可以单独存在,但如果缺少子用例,基用例则不完整。以生活场景举例,就像炒米饭的整个操作中要包含做熟米饭的操作,做熟米饭可以单独存在,炒米饭如果确少做熟米饭的操作则不完整(没有米饭可炒)。
扩展:是指一个使用场景(基用例)之后的多种可选操作(扩展用例),扩展用例为基用例添加新的行为,但扩展用例不影响基用例的操作。还是以生活场景举例,比如我们去健身,进入场馆(基用例)的这个行为之后,我们不止一种的行为可以选择,例如可以跑步(扩展用例)、也可以打羽毛球(扩展用例)。
泛化:是指子用例与父用例类似,但具有特别的行为。同样以生活场景举例,比如我们在工作时要找上级审批文件,对于审批这个行为(父用例),各种审批流程(子用例)都比较类似,但也有其特别之处,例如:请假审批、加班审批以及离职审批等。
本人用例图知识比较匮乏,以上内容如有问题,欢迎指正交流,我会进行更正。
接下来,我们可以基于用例图,进行用例说明的撰写。
在撰写用例说明的过程中,我们也可以通过Axure绘制业务流程图进行辅助说明。
这里的业务流程图,我结合页面快照元件完成。
以用户注册为例:
用户注册的主要事件流程是:打开首页--打开功能菜单--点击登录打开登录面板--切换进入注册面板--填写注册内容--提交注册
我们之前绘制的线框图,没有绘制功能菜单和注册面板。
在这里我们先通过在动态面板“MenuPanel”中放置元件组成功能菜单,并将面板隐藏。
再将登录面板的内容放入动态面板“Login_Register_Panel”中,并且添加一个新的状态“Register”,在这个状态中创建注册面板内容。
完成以上两步之后,接下来我们在一个新的页面“case01_register”中,绘制业务流程图。
第一步,根据操作流程,放入一个页面快照元件,调整成合适的尺寸,然后双击指向“home”页,并且在页面快照属性中,取消四周的填充。
第二步,将这个页面快照元件Ctrl+D进行复制,复制出来的页面快照元件在属性中添加动作,显示动态面板“MenuPanel”。这样,我们就得到了一个显示了功能菜单的首页缩略图。
第三步,再次复制快照元件,添加第三个页面“login_register”的缩略图。
第四步,再复制页面快照元件,添加第三个页面“login_register”的缩略图,并设置动作,切换面板状态为“Register”。这样就得到了第四个缩略图。
第五步,我们点中每个页面快照元件,在快捷功能区的更多功能中,找到连接点工具,为每个页面快照在用户操作的位置添加连接点,并通过连接线对页面快照元件进行连接,表达整个操作流程。
第六步,在这里我们还可以通过给页面快照元件添加动作,制作出带有不同的注册错误提示的缩略图,表达注册过程中的对用户输入内容进行验证的场景。
我继续给大家分享一些Axure的应用场景和使用方法。
1.标记与便签
当我们的线框图没有添加交互效果,又需要让沟通对象能够理解图中的每一个功能,这个时候,我们往往需要添加标注进行说明。下面就是一种添加标注,进行功能说明的方法。
我们可以通过页面快照元件,获取到页面的缩略图,并且在页面快照上添加不同编号的标记。然后,对应着标记,在缩略图的旁放入便签,进行文字说明。而且,标记和便签的颜色也可以相对应,方便用户查看。
另外,我们还可以通过旋转标记元件,让它表示页面间的跳转。例如:左侧的红色水滴形状标记元件,旋转了90度,元件中的文字“2”对应着右侧编号为“2”的缩略图。这样我们就能够知道,当我们点击标记所指向的一个列表项时,能够打开另外一个界面,继续进行浏览。
2、连线与便签
与上面的使用方法类似,我们还可以结合连线与便签元件,实现功能的说明。
这种方式能够比较直接 的对功能进行标注说明,不过因为连线不宜过多,所以,比较适合单个界面的功能说明。
3.页面区域快照
页面快照元件,可以通过属性的设置,将页面某一区域的内容呈现为缩略图,并且能够调整显示的比例。实现对界面中某一功能模块单独放大展示,并进行说明。
上图中,放大的区域也是一个页面快照元件,不过,在属性中,没有勾选【适应比例】。并且,将原有尺寸进行了120%的放大处理。
这里需要注意,显示页面区域快照,需要通过{偏移量}的调整,让页面快照元件从某一个位置开始加载页面缩略图的内容。例如上图中,第一个文章列表项,x轴坐标为0,y轴坐标为133,在属性的设置中,x轴无需偏移,但是y轴就需要向上移动133个像素的距离。所以,偏移量的设置中y轴的数值为“-133”(负数为向上)。
4.页面快照与元件说明
Axure软件自带了元件说明的功能。我们也可以将一个界面或功能模块的说明写在页面说明或者元件说明中。
这里以元件说明为例。
比如,我们对更换手机号码功能的使用场景进行说明,也就是用例说明。
首先,我们需要在说明功能中【自定义字段】。
【自定义字段】的设置,在说明模块的左上角(见下图)。设置好自定义字段之后,我们就可以点中页面快照元件,进行说明的添加。
通过这种方式添加的说明,在预览的时候能够在界面中通过点击说明的图标,将说明内容显示出来。
另外,通过这种方式添加的说明,我们还可以使用Axure自带的【生成Word说明书】的功能,将其生成到Word文档中。
如果,我们在Axure中,对每一个功能的使用情形都做了规范的说明(即用例说明),那么这些内容就可以通过【生成Word说明书】的功能生成文档后,作为产品需求文档的组成部分。
以上,就是关于产品经理对Axure应用的最后一部分内容。在这些内容中,我主要演示了绘制产品线框图的方法以及应用场景。在实际工作中,我们还可以在线框图的基础之上,对整个原型或者部分主要功能进行交互或者视觉效果的添加。这样产出的高保真原型,可以让用户或者沟通对象进行真实的视觉和操作体验,验证功能的可用性、易用性以及界面的友好性等。
最后,我把这一系列文章最终的线框图和思维导图,也分享给大家。
下载地址:
线框图: https://pan.baidu.com/s/1E8TfyGoGaJywF9sMRkAleQ
思维导图: https://pan.baidu.com/s/1bXdm5v1xu58Shu583dv-Hg
注意:如果不能直接点击下载,请讲以上网址复制到浏览器地址栏中进行下载。
希望我的分享能够给大家带来切实的帮助。
项目管理
2018-08-15 16:11:00
「深度学习福利」大神带你进阶工程师,立即查看>>>
mysql
mysql: https://dev.mysql.com/doc/refman/5.7/en/
mysql-connector: https://dev.mysql.com/doc/connector-j/5.1/en/
mybatis
mybatis: http://www.mybatis.org/mybatis-3/zh/java-api.html
mybatis-spring: http://www.mybatis.org/spring/zh/transactions.html
项目管理
2018-08-15 16:03:00
「深度学习福利」大神带你进阶工程师,立即查看>>>
XML
Library version: 3.0.4
Library scope: global
Named arguments: supported
Introduction
Robot Framework test library for verifying and modifying XML documents.
As the name implies, XML is a test library for verifying contents of XML files. In practice it is a pretty thin wrapper on top of Python's ElementTree XML API .
The library has the following main usages: Parsing an XML file, or a string containing XML, into an XML element structure and finding certain elements from it for for further analysis (e.g. Parse XML and Get Element keywords). Getting text or attributes of elements (e.g. Get Element Text and Get Element Attribute ). Directly verifying text, attributes, or whole elements (e.g Element Text Should Be and Elements Should Be Equal ). Modifying XML and saving it (e.g. Set Element Text , Add Element and Save XML ).
Table of contents Parsing XML Using lxml Example Finding elements with xpath Element attributes Handling XML namespaces Boolean arguments Shortcuts Keywords
Parsing XML
XML can be parsed into an element structure using Parse XML keyword. It accepts both paths to XML files and strings that contain XML. The keyword returns the root element of the structure, which then contains other elements as its children and their children. Possible comments and processing instructions in the source XML are removed.
XML is not validated during parsing even if has a schema defined. How possible doctype elements are handled otherwise depends on the used XML module and on the platform. The standard ElementTree strips doctypes altogether but when using lxml they are preserved when XML is saved. With IronPython parsing XML with a doctype is not supported at all.
The element structure returned by Parse XML , as well as elements returned by keywords such as Get Element , can be used as the source argument with other keywords. In addition to an already parsed XML structure, other keywords also accept paths to XML files and strings containing XML similarly as Parse XML . Notice that keywords that modify XML do not write those changes back to disk even if the source would be given as a path to a file. Changes must always saved explicitly using Save XML keyword.
When the source is given as a path to a file, the forward slash character ( / ) can be used as the path separator regardless the operating system. On Windows also the backslash works, but it the test data it needs to be escaped by doubling it ( \\ ). Using the built-in variable ${/} naturally works too.
Using lxml
By default this library uses Python's standard ElementTree module for parsing XML, but it can be configured to use lxml module instead when importing the library. The resulting element structure has same API regardless which module is used for parsing.
The main benefits of using lxml is that it supports richer xpath syntax than the standard ElementTree and enables using Evaluate Xpath keyword. It also preserves the doctype and possible namespace prefixes saving XML.
The lxml support is new in Robot Framework 2.8.5.
Example
The following simple example demonstrates parsing XML and verifying its contents both using keywords in this library and in BuiltIn and Collections libraries. How to use xpath expressions to find elements and what attributes the returned elements contain are discussed, with more examples, in Finding elements with xpath and Element attributes sections.
In this example, as well as in many other examples in this documentation, ${XML} refers to the following example XML document. In practice ${XML} could either be a path to an XML file or it could contain the XML itself. text more text

Text with bold and italics.

${root} = Parse XML ${XML}
Should Be Equal ${root.tag} example
${first} = Get Element ${root} first
Should Be Equal ${first.text} text
Dictionary Should Contain Key ${first.attrib} id
Element Text Should Be
Element Attribute Should Be Element Attribute Should Be Element Attribute Should Be
${first}
${first} ${root} ${XML}
text
id id id
1 1 1
xpath=first xpath=first
Notice that in the example three last lines are equivalent. Which one to use in practice depends on which other elements you need to get or verify. If you only need to do one verification, using the last line alone would suffice. If more verifications are needed, parsing the XML with Parse XML only once would be more efficient.
Finding elements with xpath
ElementTree, and thus also this library, supports finding elements using xpath expressions. ElementTree does not, however, support the full xpath syntax, and what is supported depends on its version. ElementTree 1.3 that is distributed with Python 2.7 supports richer syntax than earlier versions.
The supported xpath syntax is explained below and ElementTree documentation provides more details. In the examples ${XML} refers to the same XML structure as in the earlier example.
If lxml support is enabled when importing the library, the whole xpath 1.0 standard is supported. That includes everything listed below but also lot of other useful constructs.
Tag names
When just a single tag name is used, xpath matches all direct child elements that have that tag name.
${elem} = Get Element ${XML} third
Should Be Equal
@{children} = Length Should Be
${elem.tag}
Get Elements ${children}
third
${elem} 2
child
Paths
Paths are created by combining tag names with a forward slash ( / ). For example, parent/child matches all child elements under parent element. Notice that if there are multiple parent elements that all have child elements, parent/child xpath will match all these child elements.
${elem} = Get Element ${XML} second/child
Should Be Equal
${elem} = Should Be Equal
${elem.tag}
Get Element ${elem.tag}
child
${XML} grandchild
third/child/grandchild
Wildcards
An asterisk ( * ) can be used in paths instead of a tag name to denote any element.
@{children} = Length Should Be
Get Elements ${children}
${XML} 3
*/child
Current element
The current element is denoted with a dot ( . ). Normally the current element is implicit and does not need to be included in the xpath.
Parent element
The parent element of another element is denoted with two dots ( .. ). Notice that it is not possible to refer to the parent of the current element. This syntax is supported only in ElementTree 1.3 (i.e. Python/Jython 2.7 and newer).
${elem} = Should Be Equal
Get Element ${elem.tag}
${XML} third
*/second/..
Search all sub elements
Two forward slashes ( // ) mean that all sub elements, not only the direct children, are searched. If the search is started from the current element, an explicit dot is required.
@{elements} = Get Elements ${XML} .//second
Length Should Be
${b} = Should Be Equal
${elements}
Get Element ${b.text}
2
${XML} bold
html//b
Predicates
Predicates allow selecting elements using also other criteria than tag names, for example, attributes or position. They are specified after the normal tag name or path using syntax path[predicate] . The path can have wildcards and other special syntax explained above.
What predicates ElementTree supports is explained in the table below. Notice that predicates in general are supported only in ElementTree 1.3 (i.e. Python/Jython 2.7 and newer).
Predicate Matches Example
@attrib Elements with attribute attrib . second[ @id ]
@attrib="value" Elements with attribute attrib having value value . *[@id="2"]
position
tag
Elements at the specified position. Position can be an integer (starting from 1), expression last() , or relative expression like last() - 1 .
Elements with a child element named tag .
third/child[1]
third/child[grandchild]
Predicates can also be stacked like path[predicate1][predicate2] . A limitation is that possible position predicate must always be first.
Element attributes
All keywords returning elements, such as Parse XML , and Get Element , return ElementTree's Element objects . These elements can be used as inputs for other keywords, but they also contain several useful attributes that can be accessed directly using the extended variable syntax.
The attributes that are both useful and convenient to use in the test data are explained below. Also other attributes, including methods, can be accessed, but that is typically better to do in custom libraries than directly in the test data.
The examples use the same ${XML} structure as the earlier examples.
tag
The tag of the element.
${root} = Should Be Equal
Parse XML ${root.tag}
${XML} example
text
The text that the element contains or Python None if the element has no text. Notice that the text does not contain texts of possible child elements nor text after or between children. Notice also that in XML whitespace is significant, so the text contains also possible indentation and newlines. To get also text of the possible children, optionally whitespace normalized, use Get Element Text keyword.
${1st} = Get Element ${XML} first
Should Be Equal ${1st.text} text
${2nd} = Get Element ${XML} second/child
Should Be Equal
${p} = Should Be Equal
${2nd.text}
Get Element ${p.text}
${NONE}
${XML} \n${SPACE*6}Text with${SPACE}
html/p
tail
The text after the element before the next opening or closing tag. Python None if the element has no tail. Similarly as with text , also tail contains possible indentation and newlines.
${b} = Should Be Equal
Get Element ${b.tail}
${XML} ${SPACE}and${SPACE}
html/p/b
attrib
A Python dictionary containing attributes of the element.
${2nd} = Get Element ${XML} second
Should Be Equal
${3rd} = Should Be Empty
${2nd.attrib['id']}
Get Element ${3rd.attrib}
2
${XML}
third
Handling XML namespaces
ElementTree and lxml handle possible namespaces in XML documents by adding the namespace URI to tag names in so called Clark Notation. That is inconvenient especially with xpaths, and by default this library strips those namespaces away and moves them to xmlns attribute instead. That can be avoided by passing keep_clark_notation argument to Parse XML keyword. Alternatively Parse XML supports stripping namespace information altogether by using strip_namespaces argument. The pros and cons of different approaches are discussed in more detail below.
How ElementTree handles namespaces
If an XML document has namespaces, ElementTree adds namespace information to tag names in Clark Notation (e.g. {http://ns.uri}tag ) and removes original xmlns attributes. This is done both with default namespaces and with namespaces with a prefix. How it works in practice is illustrated by the following example, where ${NS} variable contains this XML document:
${root} = Parse XML ${NS} keep_clark_notation=yes
Should Be Equal
Element Should Exist Should Be Empty
${root.tag}
${root} ${root.attrib}
{http://www.w3.org/1999/XSL/Transform}stylesheet
{http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html
As you can see, including the namespace URI in tag names makes xpaths really long and complex.
If you save the XML, ElementTree moves namespace information back to xmlns attributes. Unfortunately it does not restore the original prefixes:
The resulting output is semantically same as the original, but mangling prefixes like this may still not be desirable. Notice also that the actual output depends slightly on ElementTree version.
Default namespace handling
Because the way ElementTree handles namespaces makes xpaths so complicated, this library, by default, strips namespaces from tag names and moves that information back to xmlns attributes. How this works in practice is shown by the example below, where ${NS} variable contains the same XML document as in the previous example.
${root} = Parse XML ${NS}
Should Be Equal
Element Should Exist Element Attribute Should Be Element Attribute Should Be
${root.tag}
${root} ${root} ${root}
stylesheet
template/html xmlns xmlns
http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/xhtml
xpath=template/html
Now that tags do not contain namespace information, xpaths are simple again.
A minor limitation of this approach is that namespace prefixes are lost. As a result the saved output is not exactly same as the original one in this case either:
Also this output is semantically same as the original. If the original XML had only default namespaces, the output would also look identical.
Namespaces when using lxml
This library handles namespaces same way both when using lxml and when not using it. There are, however, differences how lxml internally handles namespaces compared to the standard ElementTree. The main difference is that lxml stores information about namespace prefixes and they are thus preserved if XML is saved. Another visible difference is that lxml includes namespace information in child elements got with Get Element if the parent element has namespaces.
Stripping namespaces altogether
Because namespaces often add unnecessary complexity, Parse XML supports stripping them altogether by using strip_namespaces=True . When this option is enabled, namespaces are not shown anywhere nor are they included if XML is saved.
Attribute namespaces
Attributes in XML documents are, by default, in the same namespaces as the element they belong to. It is possible to use different namespaces by using prefixes, but this is pretty rare.
If an attribute has a namespace prefix, ElementTree will replace it with Clark Notation the same way it handles elements. Because stripping namespaces from attributes could cause attribute conflicts, this library does not handle attribute namespaces at all. Thus the following example works the same way regardless how namespaces are handled.
${root} = Element Attribute Should Be Element Attribute Should Be
Parse XML ${root} ${root}
id {http://my.ns}id
1 2
Boolean arguments
Some keywords accept arguments that are handled as Boolean values true or false. If such an argument is given as a string, it is considered false if it is either an empty string or case-insensitively equal to false , none or no . Other strings are considered true regardless their value, and other argument types are tested using the same rules as in Python .
True examples:
Parse XML ${XML} keep_clark_notation=True # Strings are generally true.
Parse XML
Parse XML Parse XML
${XML}
${XML} ${XML}
keep_clark_notation=yes
keep_clark_notation=${TRUE} keep_clark_notation=${42}
# Same as the above.
# Python True is true. # Numbers other than 0 are true.
False examples:
Parse XML ${XML} keep_clark_notation=False # String false is false.
Parse XML
Parse XML Parse XML
${XML}
${XML} ${XML}
keep_clark_notation=no
keep_clark_notation=${EMPTY} keep_clark_notation=${FALSE}
# Also string no is false.
# Empty string is false. # Python False is false.
Prior to Robot Framework 2.9, all non-empty strings, including false and no , were considered to be true. Considering none false is new in Robot Framework 3.0.3.
Importing
Arguments Documentation
use_lxml=False
Import library with optionally lxml mode enabled.
By default this library uses Python's standard ElementTree module for parsing XML. If use_lxml argument is given a true value (see Boolean arguments ), the library will use lxml module instead. See Using lxml section for benefits provided by lxml.
Using lxml requires that the lxml module is installed on the system. If lxml mode is enabled but the module is not installed, this library will emit a warning and revert back to using the standard ElementTree.
The support for lxml is new in Robot Framework 2.8.5.
Shortcuts
Add Element · Clear Element · Copy Element · Element Attribute Should Be · Element Attribute Should Match · Element Should Exist · Element Should Not Exist · Element Should Not Have Attribute · Element Text Should Be · Element Text Should Match · Element To String · Elements Should Be Equal · Elements Should Match · Evaluate Xpath · Get Child Elements · Get Element · Get Element Attribute · Get Element Attributes · Get Element Count · Get Element Text · Get Elements · Get Elements Texts · Log Element · Parse Xml · Remove Element · Remove Element Attribute · Remove Element Attributes · Remove Elements · Remove Elements Attribute · Remove Elements Attributes · Save Xml · Set Element Attribute · Set Element Tag · Set Element Text · Set Elements Attribute · Set Elements Tag · Set Elements Text
Keywords
Keyword Arguments Documentation
Add Element source, element,index=None, xpath=. Adds a child element to the specified element.
The element to whom to add the new element is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
The element to add can be specified as a path to an XML file or as a string containing XML, or it can be an already parsed XML element. The element is copied before adding so modifying either the original or the added element has no effect on the other . The element is added as the last child by default, but a custom index can be used to alter the position. Indices start from zero (0 = first position, 1 = second position, etc.), and negative numbers refer to positions at the end (-1 = second last position, -2 = third last, etc.).
Examples using ${XML} structure from Example :
Add Element ${XML}
Add Element
Add Element ${new} = Elements Should Be Equal
${XML}
${XML} Get Element ${new}
${XML}
xpath=new
index=1 new
xpath=new
Use Remove Element or Remove Elements to remove elements.
Clear Element source, xpath=.,clear_tail=False Clears the contents of the specified element.
The element to clear is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
Clearing the element means removing its text, attributes, and children. Element's tail text is not removed by default, but that can be changed by giving clear_tail a true value (see Boolean arguments ). See Element attributes section for more information about tail in general.
Examples using ${XML} structure from Example :
Clear Element ${XML} xpath=first
${first} = Get Element ${XML} xpath=first
Elements Should Be Equal ${first}
Clear Element
Element Text Should Be Clear Element Elements Should Be Equal
${XML}
${XML} ${XML} ${XML}
xpath=html/p/b
Text with italics.
clear_tail=yes
xpath=html/p
normalize_whitespace=yes
Use Remove Element to remove the whole element.
Copy Element source, xpath=. Returns a copy of the specified element.
The element to copy is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
If the copy or the original element is modified afterwards, the changes have no effect on the other.
Examples using ${XML} structure from Example :
${elem} = Get Element ${XML} xpath=first
${copy1} = Copy Element ${elem}
${copy2} = Copy Element ${XML} xpath=first
Set Element Text ${XML} new text xpath=first
Set Element Attribute ${copy1} id new
Elements Should Be Equal
Elements Should Be Equal Elements Should Be Equal
${elem}
${copy1} ${copy2}
new text
text text
Element Attribute Should Be source, name, expected,xpath=., message=None Verifies that the specified attribute is expected .
The element whose attribute is verified is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
The keyword passes if the attribute name of the element is equal to the expected value, and otherwise it fails. The default error message can be overridden with the message argument.
To test that the element does not have a certain attribute, Python None (i.e. variable ${NONE} ) can be used as the expected value. A cleaner alternative is using Element Should Not Have Attribute .
Examples using ${XML} structure from Example :
Element Attribute Should Be Element Attribute Should Be
${XML} ${XML}
id id
1 ${NONE}
xpath=first
See also Element Attribute Should Match and Get Element Attribute .
Element Attribute Should Match source, name, pattern,xpath=., message=None Verifies that the specified attribute matches expected .
This keyword works exactly like Element Attribute Should Be except that the expected value can be given as a pattern that the attribute of the element must match.
Pattern matching is similar as matching files in a shell, and it is always case-sensitive. In the pattern, '*' matches anything and '?' matches any single character.
Examples using ${XML} structure from Example :
Element Attribute Should Match Element Attribute Should Match
${XML} ${XML}
id id
? c*d
xpath=first xpath=third/second
Element Should Exist source, xpath=.,message=None Verifies that one or more element match the given xpath .
Arguments source and xpath have exactly the same semantics as with Get Elements keyword. Keyword passes if the xpath matches one or more elements in the source . The default error message can be overridden with the message argument.
See also Element Should Not Exist as well as Get Element Count that this keyword uses internally.
Element Should Not Exist source, xpath=.,message=None Verifies that no element match the given xpath .
Arguments source and xpath have exactly the same semantics as with Get Elements keyword. Keyword fails if the xpath matches any element in the source . The default error message can be overridden with the message argument.
See also Element Should Exist as well as Get Element Count that this keyword uses internally.
Element Should Not Have Attribute source, name, xpath=.,message=None Verifies that the specified element does not have attribute name .
The element whose attribute is verified is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
The keyword fails if the specified element has attribute name . The default error message can be overridden with the message argument.
Examples using ${XML} structure from Example :
Element Should Not Have Attribute Element Should Not Have Attribute
${XML} ${XML}
id xxx
xpath=first
See also Get Element Attribute , Get Element Attributes , Element Text Should Be and Element Text Should Match .
Element Text Should Be source, expected, xpath=.,normalize_whitespace=False,message=None Verifies that the text of the specified element is expected .
The element whose text is verified is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
The text to verify is got from the specified element using the same logic as with Get Element Text . This includes optional whitespace normalization using the normalize_whitespace option.
The keyword passes if the text of the element is equal to the expected value, and otherwise it fails. The default error message can be overridden with the message argument. Use Element Text Should Match to verify the text against a pattern instead of an exact value.
Examples using ${XML} structure from Example :
Element Text Should Be ${XML} text xpath=first
Element Text Should Be
${paragraph} = Element Text Should Be
${XML}
Get Element ${paragraph}
${EMPTY}
${XML} Text with bold and italics.
xpath=second/child
xpath=html/p normalize_whitespace=yes
Element Text Should Match source, pattern, xpath=.,normalize_whitespace=False,message=None Verifies that the text of the specified element matches expected .
This keyword works exactly like Element Text Should Be except that the expected value can be given as a pattern that the text of the element must match.
Pattern matching is similar as matching files in a shell, and it is always case-sensitive. In the pattern, '*' matches anything and '?' matches any single character.
Examples using ${XML} structure from Example :
Element Text Should Match ${paragraph} = Element Text Should Match
${XML} Get Element ${paragraph}
t??? ${XML} Text with * and *.
xpath=first xpath=html/p normalize_whitespace=yes
Element To String source, xpath=.,encoding=None Returns the string representation of the specified element.
The element to convert to a string is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
By default the string is returned as Unicode. If encoding argument is given any value, the string is returned as bytes in the specified encoding. The resulting string never contains the XML declaration.
See also Log Element and Save XML .
Elements Should Be Equal source, expected,exclude_children=False,normalize_whitespace=False Verifies that the given source element is equal to expected .
Both source and expected can be given as a path to an XML file, as a string containing XML, or as an already parsed XML element structure. See introduction for more information about parsing XML in general.
The keyword passes if the source element and expected element are equal. This includes testing the tag names, texts, and attributes of the elements. By default also child elements are verified the same way, but this can be disabled by setting exclude_children to a true value (see Boolean arguments ).
All texts inside the given elements are verified, but possible text outside them is not. By default texts must match exactly, but setting normalize_whitespace to a true value makes text verification independent on newlines, tabs, and the amount of spaces. For more details about handling text see Get Element Text keyword and discussion about elements' text and tail attributes in the introduction .
Examples using ${XML} structure from Example :
${first} = Get Element ${XML} first
Elements Should Be Equal
${p} = Elements Should Be Equal Elements Should Be Equal
${first}
Get Element ${p} ${p}
text
${XML}

Text with bold and italics.

Text with


html/p normalize_whitespace=yes exclude
normalize
The last example may look a bit strange because the

element only has text Text with . The reason is that rest of the text inside

actually belongs to the child elements. This includes the . at the end that is the tail text of the element.
See also Elements Should Match .

Elements Should Match source, expected,exclude_children=False,normalize_whitespace=False Verifies that the given source element matches expected .
This keyword works exactly like Elements Should Be Equal except that texts and attribute values in the expected value can be given as patterns.
Pattern matching is similar as matching files in a shell, and it is always case-sensitive. In the pattern, '*' matches anything and '?' matches any single character.
Examples using ${XML} structure from Example :
${first} = Elements Should Match
Get Element ${first}
${XML} *
first
See Elements Should Be Equal for more examples.
Evaluate Xpath source, expression,context=. Evaluates the given xpath expression and returns results.
The element in which context the expression is executed is specified using source and context arguments. They have exactly the same semantics as source and xpath arguments have with Get Element keyword.
The xpath expression to evaluate is given as expression argument. The result of the evaluation is returned as-is.
Examples using ${XML} structure from Example :
${count} = Evaluate Xpath ${XML} count(third/*)
Should Be Equal ${count} ${3}
${text} =
Should Be Equal ${bold} = Should Be Equal
Evaluate Xpath
${text} Evaluate Xpath ${bold}
${XML}
child ${XML} ${True}
string(descendant::second[last()]/@id)
boolean(preceding-sibling::*[1] = 'bold')
context=html/p/i
This keyword works only if lxml mode is taken into use when importing the library. New in Robot Framework 2.8.5.
Get Child Elements source, xpath=. Returns the child elements of the specified element as a list.
The element whose children to return is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
All the direct child elements of the specified element are returned. If the element has no children, an empty list is returned.
Examples using ${XML} structure from Example :
${children} = Get Child Elements ${XML}
Length Should Be
${children} = Should Be Empty
${children}
Get Child Elements ${children}
4
${XML}
xpath=first
Get Element source, xpath=. Returns an element in the source matching the xpath .
The source can be a path to an XML file, a string containing XML, or an already parsed XML element. The xpath specifies which element to find. See the introduction for more details about both the possible sources and the supported xpath syntax.
The keyword fails if more, or less, than one element matches the xpath . Use Get Elements if you want all matching elements to be returned.
Examples using ${XML} structure from Example :
${element} = ${child} =
Get Element Get Element
${XML} ${element}
second child
Parse XML is recommended for parsing XML when the whole structure is needed. It must be used if there is a need to configure how XML namespaces are handled.
Many other keywords use this keyword internally, and keywords modifying XML are typically documented to both to modify the given source and to return it. Modifying the source does not apply if the source is given as a string. The XML structure parsed based on the string and then modified is nevertheless returned.
Get Element Attribute source, name, xpath=.,default=None Returns the named attribute of the specified element.
The element whose attribute to return is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
The value of the attribute name of the specified element is returned. If the element does not have such element, the default value is returned instead.
Examples using ${XML} structure from Example :
${attribute} = Should Be Equal ${attribute} = Should Be Equal
Get Element Attribute ${attribute} Get Element Attribute ${attribute}
${XML} 1 ${XML} value
id xx
xpath=first xpath=first
default=value
See also Get Element Attributes , Element Attribute Should Be , Element Attribute Should Match and Element Should Not Have Attribute .
Get Element Attributes source, xpath=. Returns all attributes of the specified element.
The element whose attributes to return is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
Attributes are returned as a Python dictionary. It is a copy of the original attributes so modifying it has no effect on the XML structure.
Examples using ${XML} structure from Example :
${attributes} = Get Element Attributes ${XML} first
Dictionary Should Contain Key
${attributes} = Should Be Empty
${attributes}
Get Element Attributes ${attributes}
id
${XML}
third
Use Get Element Attribute to get the value of a single attribute.
Get Element Count source, xpath=. Returns and logs how many elements the given xpath matches.
Arguments source and xpath have exactly the same semantics as with Get Elements keyword that this keyword uses internally.
See also Element Should Exist and Element Should Not Exist .
Get Element Text source, xpath=.,normalize_whitespace=False Returns all text of the element, possibly whitespace normalized.
The element whose text to return is specified using source and xpath . They have exactly the same semantics as with Get Element keyword.
This keyword returns all the text of the specified element, including all the text its children and grandchildren contain. If the element has no text, an empty string is returned. The returned text is thus not always the same as the text attribute of the element.
By default all whitespace, including newlines and indentation, inside the element is returned as-is. If normalize_whitespace is given a true value (see Boolean arguments ), then leading and trailing whitespace is stripped, newlines and tabs converted to spaces, and multiple spaces collapsed into one. This is especially useful when dealing with HTML data.
Examples using ${XML} structure from Example :
${text} = Get Element Text ${XML} first
Should Be Equal ${text} text
${text} = Get Element Text ${XML} second/child
Should Be Empty ${text}
${paragraph} =
${text} = Should Be Equal
Get Element
Get Element Text ${text}
${XML}
${paragraph} Text with bold and italics.
html/p
normalize_whitespace=yes
See also Get Elements Texts , Element Text Should Be and Element Text Should Match .
Get Elements source, xpath Returns a list of elements in the source matching the xpath .
The source can be a path to an XML file, a string containing XML, or an already parsed XML element. The xpath specifies which element to find. See the introduction for more details.
Elements matching the xpath are returned as a list. If no elements match, an empty list is returned. Use Get Element if you want to get exactly one match.
Examples using ${XML} structure from Example :
${children} = Get Elements ${XML} third/child
Length Should Be
${children} = Should Be Empty
${children}
Get Elements ${children}
2
${XML}
first/child
Get Elements Texts source, xpath,normalize_whitespace=False Returns text of all elements matching xpath as a list.
The elements whose text to return is specified using source and xpath . They have exactly the same semantics as with Get Elements keyword.
The text of the matched elements is returned using the same logic as with Get Element Text . This includes optional whitespace normalization using the normalize_whitespace option.
Examples using ${XML} structure from Example :
@{texts} = Get Elements Texts ${XML} third/child
Length Should Be
Should Be Equal Should Be Equal
${texts}
@{texts}[0] @{texts}[1]
2
more text ${EMPTY}
Log Element source, level=INFO, xpath=. Logs the string representation of the specified element.
The element specified with source and xpath is first converted into a string using Element To String keyword internally. The resulting string is then logged using the given level .
The logged string is also returned.
Parse Xml source,keep_clark_notation=False,strip_namespaces=False Parses the given XML file or string into an element structure.
The source can either be a path to an XML file or a string containing XML. In both cases the XML is parsed into ElementTree element structure and the root element is returned. Possible comments and processing instructions in the source XML are removed.
As discussed in Handling XML namespaces section, this keyword, by default, removes namespace information ElementTree has added to tag names and moves it into xmlns attributes. This typically eases handling XML documents with namespaces considerably. If you do not want that to happen, or want to avoid the small overhead of going through the element structure when your XML does not have namespaces, you can disable this feature by giving keep_clark_notation argument a true value (see Boolean arguments ).
If you want to strip namespace information altogether so that it is not included even if XML is saved, you can give a true value to strip_namespaces argument. This functionality is new in Robot Framework 3.0.2.
Examples:
${root} = ${xml} = ${xml} =
Parse XML Parse XML Parse XML
${CURDIR}/test.xml ${CURDIR}/test.xml
keep_clark_notation=True strip_namespaces=True
Use Get Element keyword if you want to get a certain element and not the whole structure. See Parsing XML section for more details and examples.
Remove Element source, xpath=,remove_tail=False Removes the element matching xpath from the source structure.
The element to remove from the source is specified with xpath using the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
The keyword fails if xpath does not match exactly one element. Use Remove Elements to remove all matched elements.
Element's tail text is not removed by default, but that can be changed by giving remove_tail a true value (see Boolean arguments ). See Element attributes section for more information about tail in general.
Examples using ${XML} structure from Example :
Remove Element Element Should Not Exist Remove Element Element Text Should Be
${XML} ${XML} ${XML} ${XML}
xpath=second xpath=second xpath=html/p/b Text with italics.
remove_tail=yes xpath=html/p
normalize_whitespace=yes
Remove Element Attribute source, name, xpath=. Removes attribute name from the specified element.
The element whose attribute to remove is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
It is not a failure to remove a non-existing attribute. Use Remove Element Attributes to remove all attributes and Set Element Attribute to set them.
Examples using ${XML} structure from Example :
Remove Element Attribute Element Should Not Have Attribute
${XML} ${XML}
id id
xpath=first xpath=first
Can only remove an attribute from a single element. Use Remove Elements Attribute to remove an attribute of multiple elements in one call.
Remove Element Attributes source, xpath=. Removes all attributes from the specified element.
The element whose attributes to remove is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
Use Remove Element Attribute to remove a single attribute and Set Element Attribute to set them.
Examples using ${XML} structure from Example :
Remove Element Attributes Element Should Not Have Attribute
${XML} ${XML}
xpath=first id
xpath=first
Can only remove attributes from a single element. Use Remove Elements Attributes to remove all attributes of multiple elements in one call.
Remove Elements source, xpath=,remove_tail=False Removes all elements matching xpath from the source structure.
The elements to remove from the source are specified with xpath using the same semantics as with Get Elements keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
It is not a failure if xpath matches no elements. Use Remove Element to remove exactly one element.
Element's tail text is not removed by default, but that can be changed by using remove_tail argument similarly as with Remove Element .
Examples using ${XML} structure from Example :
Remove Elements ${XML} xpath=*/child
Element Should Not Exist
Element Should Not Exist
${XML}
${XML}
xpath=second/child
xpath=third/child
Remove Elements Attribute source, name, xpath=. Removes attribute name from the specified elements.
Like Remove Element Attribute but removes the attribute of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Remove Elements Attributes source, xpath=. Removes all attributes from the specified elements.
Like Remove Element Attributes but removes all attributes of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Save Xml source, path,encoding=UTF-8 Saves the given element to the specified file.
The element to save is specified with source using the same semantics as with Get Element keyword.
The file where the element is saved is denoted with path and the encoding to use with encoding . The resulting file always contains the XML declaration.
The resulting XML file may not be exactly the same as the original: Comments and processing instructions are always stripped. Possible doctype and namespace prefixes are only preserved when using lxml . Other small differences are possible depending on the ElementTree or lxml version.
Use Element To String if you just need a string representation of the element.
Set Element Attribute source, name, value,xpath=. Sets attribute name of the specified element to value .
The element whose attribute to set is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
It is possible to both set new attributes and to overwrite existing. Use Remove Element Attribute or Remove Element Attributes for removing them.
Examples using ${XML} structure from Example :
Set Element Attribute Element Attribute Should Be Set Element Attribute Element Attribute Should Be
${XML} ${XML} ${XML} ${XML}
attr attr id id
value value new new
xpath=first xpath=first
Can only set an attribute of a single element. Use Set Elements Attribute to set an attribute of multiple elements in one call.
Set Element Tag source, tag, xpath=. Sets the tag of the specified element.
The element whose tag to set is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
Examples using ${XML} structure from Example :
Set Element Tag ${XML} newTag
Should Be Equal ${XML.tag} newTag
Set Element Tag
Element Should Exist Element Should Not Exist
${XML}
${XML} ${XML}
xxx
second/xxx second/child
xpath=second/child
Can only set the tag of a single element. Use Set Elements Tag to set the tag of multiple elements in one call.
Set Element Text source, text=None,tail=None, xpath=. Sets text and/or tail text of the specified element.
The element whose text to set is specified using source and xpath . They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.
Element's text and tail text are changed only if new text and/or tail values are given. See Element attributes section for more information about text and tail in general.
Examples using ${XML} structure from Example :
Set Element Text ${XML} new text xpath=first
Element Text Should Be ${XML} new text xpath=first
Set Element Text
Element Text Should Be Set Element Text Element Text Should Be
${XML}
${XML} ${XML} ${XML}
tail=&
Text with bold&italics. slanted Text with bold&slanted!!
xpath=html/p/b
xpath=html/p !! xpath=html/p
normalize_whitespace=yes xpath=html/p/i normalize_whitespace=yes
Can only set the text/tail of a single element. Use Set Elements Text to set the text/tail of multiple elements in one call.
Set Elements Attribute source, name, value,xpath=. Sets attribute name of the specified elements to value .
Like Set Element Attribute but sets the attribute of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Set Elements Tag
Set Elements Text
source, tag, xpath=.
source, text=None,tail=None, xpath=.
Sets the tag of the specified elements.
Like Set Element Tag but sets the tag of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Sets text and/or tail text of the specified elements.
Like Set Element Text but sets the text or tail of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Altogether 37 keywords.
Generated by Libdoc on 2018-04-25 23:41:29.

XML
图书馆版本: 3.0.4
图书馆范围: 全球
命名参数: 支持的
介绍
Robot Framework测试库,用于验证和修改XML文档。
顾名思义, XML 是用于验证XML文件内容的测试库。实际上,它是Python的 ElementTree XML API 之上的一个非常薄的包装器。
该库具有以下主要用途: 将XML文件或包含XML的字符串解析为XML元素结构,并从中查找某些元素以供进一步分析(例如, Parse XML 和 Get Element 关键字)。 获取元素的文本或属性(例如, 获取元素文本 和 获取元素属性 )。 直接验证文本,属性或整个元素(例如 元素文本应该 和 元素应该相等 )。 修改XML并保存它(例如, 设置元素文本 , 添加元素 和 保存XML )。
目录 解析XML 使用lxml 例 使用xpath查找元素 元素属性 处理XML名称空间 布尔参数 快捷键 关键词
解析XML
可以使用 Parse XML 关键字将XML解析为元素结构。它接受XML文件的路径和包含XML的字符串。关键字返回结构的根元素,然后包含其他元素作为其子元素及其子元素。将删除源XML中可能的注释和处理指令。
即使已定义架构,在解析期间也不验证XML。如何处理doctype元素,否则取决于使用的XML模块和平台。标准的ElementTree完全剥离了doctypes,但是当 使用lxml时, 它们会在保存XML时保留。使用IronPython时,根本不支持使用doctype解析XML。
Parse XML 返回的元素结构以及 Get Element source 等关键字返回的 元素 可以用作其他关键字的参数。除了已经解析的XML结构之外,其他关键字也接受XML文件的路径和包含XML的字符串,类似于 Parse XML 。请注意,修改XML的关键字不会将这些更改写回磁盘,即使源是作为文件的路径提供的。必须始终使用 Save XML 关键字显式 保存 更改。
当源作为文件的路径给出时,正斜杠字符( / )可以用作路径分隔符,而不管操作系统如何。在Windows上也可以使用反斜杠,但它需要通过加倍( \\ )来转义它所需的测试数据。使用内置变量也很 ${/} 自然。
使用lxml
默认情况下,此库使用Python的标准 ElementTree 模块来解析XML,但可以将其配置为在 导入 库时使用 lxml 模块。无论使用哪个模块进行解析,结果元素结构都具有相同的API。
使用lxml的主要好处是它支持比标准ElementTree更丰富的xpath语法,并允许使用 Evaluate Xpath 关键字。它还保留了保存XML的doctype和可能的名称空间前缀。
lxml支持是Robot Framework 2.8.5中的新增功能。

下面的简单示例演示了如何使用此库中的关键字以及 BuiltIn 和 Collections 库来解析XML并验证其内容。如何使用xpath表达式来查找元素以及返回的元素包含哪些属性,以及更多示例,在“ 使用xpath 和 元素属性 查找元素” 部分中进行讨论。
在此示例中,以及本文档中的许多其他示例中,请 ${XML} 参考以下示例XML文档。实际上, ${XML} 它可以是XML文件的路径,也可以包含XML本身。 <实例> text <子/> <第三> 更多文字 <子> <孙子/>

粗体斜体的文字。

$ {root} = 解析XML $ {} XML
应该是平等的 $ {} root.tag
$ {first} = 获取元素 $ {}根 第一
应该是平等的 $ {} first.text 文本
字典应该包含密钥 $ {} first.attrib ID
元素文本应该是
元素属性应该是 元素属性应该是 元素属性应该是
$ {}第一
$ {}第一 $ {}根 $ {} XML
文本
ID ID ID
1 1 1
的xpath =第一 的xpath =第一
请注意,在示例中,最后三行是等效的。在实践中使用哪一个取决于您需要获取或验证的其他元素。如果您只需要进行一次验证,仅使用最后一行就足够了。如果需要更多验证,只使用 Parse XML解析XML 一次会更有效。
使用xpath查找元素
ElementTree以及此库也支持使用xpath表达式查找元素。但是,ElementTree不支持完整的xpath语法,支持的内容取决于其版本。随Python 2.7一起发布的ElementTree 1.3支持比早期版本更丰富的语法。
支持的xpath语法如下所述, ElementTree文档 提供了更多详细信息。在示例中, ${XML} 引用与先前示例中相同的XML结构。
如果在 导入 库时启用了lxml支持,则支持整个 xpath 1.0标准 。这包括下面列出的所有内容,但也包括许多其他有用的结构。
标记名称
如果仅使用单个标记名称,则xpath将匹配具有该标记名称的所有直接子元素。
$ {elem} = 获取元素 $ {} XML 第三
应该是平等的
@ {children} = 应该是长度
$ {} elem.tag
获取元素 $ {}儿童
第三
$ {} ELEM 2
儿童
路径
通过将标记名称与正斜杠( / )组合来创建路径。例如, parent/child 匹配 child 元素下的所有元素 parent 。请注意,如果有多个 parent 元素都具有 child 元素,则 parent/child xpath将匹配所有这些 child 元素。
$ {elem} = 获取元素 $ {} XML 第二/儿童
应该是平等的
$ {elem} = 应该是平等的
$ {} elem.tag
获取元素 $ {} elem.tag
儿童
$ {} XML 孙子
第三个/孩子/孙子
通配符
* 可以在路径中使用星号()而不是标记名称来表示任何元素。
@ {children} = 应该是长度
获取元素 $ {}儿童
$ {} XML 3
*/儿童
当前元素
当前元素用点( . )表示。通常,当前元素是隐式的,不需要包含在xpath中。
父元素
另一个元素的父元素用两个点( .. )表示。请注意,无法引用当前元素的父元素。仅在ElementTree 1.3(即Python / Jython 2.7及更高版本)中支持此语法。
$ {elem} = 应该是平等的
获取元素 $ {} elem.tag
$ {} XML 第三
*/第二/..
搜索所有子元素
两个正斜杠( // )表示搜索所有子元素,而不仅仅是直接子元素。如果从当前元素开始搜索,则需要显式点。
@ {elements} = 获取元素 $ {} XML 。//第二
应该是长度
$ {b} = 应该是平等的
$ {}元素
获取元素 $ {} b.text
2
$ {} XML 胆大
HTML // B
谓词
谓词允许使用除标签名称之外的其他标准来选择元素,例如,属性或位置。它们使用语法在普通标记名称或路径之后指定 path[predicate] 。该路径可以具有上面解释的通配符和其他特殊语法。
ElementTree支持的谓词在下表中说明。请注意,通常只在ElementTree 1.3中支持谓词(即Python / Jython 2.7和更新版本)。
谓词 火柴 例
@attrib 具有属性的元素 attrib 。 第二[@id]
@属性=“值” 具有属性 attrib 值的元素 value 。 * [@ ID = “2”]
位置
标签
指定位置的元素。位置可以是整数(从1开始),表达式 last() 或相对表达式 last() - 1 。
带有子元素的元素 tag 。
第三/子[1]
第三/儿童[孙子]
谓词也可以堆叠起来 path[predicate1][predicate2] 。一个限制是可能的位置谓词必须始终是第一位的。
元素属性
返回元素的所有关键字(例如 Parse XML 和 Get Element )都返回ElementTree的 Element对象 。这些元素可以用作其他关键字的输入,但它们还包含几个可以使用扩展变量语法直接访问的有用属性。
下面解释了在测试数据中使用既有用又方便的属性。还可以访问其他属性(包括方法),但这通常在自定义库中比直接在测试数据中更好。
这些示例使用与 ${XML} 前面示例相同的结构。
标签
元素的标记。
$ {root} = 应该是平等的
解析XML $ {} root.tag
$ {} XML 例
文本
元素包含的文本或者 None 元素没有文本的Python 。请注意,文本 不 包含可能的子元素的文本 ,也不 包含子项之间或之间的文本。另请注意,在XML空格中很重要,因此文本还包含缩进和换行符。要获取可能子项的文本(可选择空格标准化),请使用“ 获取元素文本” 关键字。
$ {1st} = 获取元素 $ {} XML 第一
应该是平等的 $ {} 1st.text 文本
$ {2nd} = 获取元素 $ {} XML 第二/儿童
应该是平等的
$ {p} = 应该是平等的
$ {} 2nd.text
获取元素 $ {} p.text
$ {无}
$ {} XML \ n $ {SPACE * 6}带$ {SPACE}的文字
HTML / P
尾巴
在下一个打开或关闭标记之前的元素之后的文本。Python None 如果元素没有尾部。与此类似 text ,还 tail 包含可能的缩进和换行符。
$ {b} = 应该是平等的
获取元素 $ {} b.tail
$ {} XML $ {SPACE}和$ {空白}
HTML / P / B
ATTRIB
包含元素属性的Python字典。
$ {2nd} = 获取元素 $ {} XML 第二
应该是平等的
$ {3rd} = 应该是空的
$ {2nd.attrib [ '身份证']}
获取元素 $ {} 3rd.attrib
2
$ {} XML
第三
处理XML名称空间
ElementTree和lxml通过将名称空间URI添加到所谓的Clark Notation中的标记名称来处理XML文档中可能的名称空间。这对于xpaths来说是不方便的,默认情况下,这个库会剥离这些名称空间并将它们移动到 xmlns 属性。通过将 keep_clark_notation 参数传递给 Parse XML 关键字可以避免这种情况。或者, Parse XML 支持使用 strip_namespaces 参数完全剥离命名空间信息。下面更详细地讨论不同方法的优缺点。
ElementTree如何处理名称空间
如果XML文档具有名称空间,ElementTree会将名称空间信息添加到 Clark Notation中的 标记名称(例如 {http://ns.uri}tag )并删除原始 xmlns 属性。使用默认命名空间和带前缀的命名空间都可以完成此操作。以下示例说明了它在实践中的工作原理,其中 ${NS} 变量包含此XML文档:
$ {root} = 解析XML $ {} NS keep_clark_notation = YES
应该是平等的
元素应该存在 应该是空的
$ {} root.tag
$ {}根 $ {} root.attrib
{http://www.w3.org/1999/XSL/Transform}stylesheet
{http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html
正如您所看到的,在标记名称中包含名称空间URI会使xpath变得非常冗长和复杂。
如果保存XML,ElementTree会将名称空间信息移回 xmlns 属性。不幸的是它没有恢复原始前缀:
结果输出在语义上与原始输出相同,但是这样的修改前缀可能仍然不可取。另请注意,实际输出略微取决于ElementTree版本。
默认命名空间处理
由于ElementTree处理命名空间的方式使xpath变得如此复杂,因此默认情况下,此库从标记名称中删除命名空间并将该信息移回 xmlns 属性。下面的示例显示了它在实践中的工作原理,其中 ${NS} 变量包含与上一示例中相同的XML文档。
$ {root} = 解析XML $ {} NS
应该是平等的
元素应该存在 元素属性应该是 元素属性应该是
$ {} root.tag
$ {}根 $ {}根 $ {}根
样式表
模板/ HTML XMLNS XMLNS
http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/xhtml
的xpath =模板/ HTML
现在标签不包含名称空间信息,xpath再次简单。
此方法的一个小限制是名称空间前缀丢失。因此,在这种情况下,保存的输出与原始输出不完全相同: