码迷,mamicode.com
首页 > 其他好文 > 详细

Robot framework--内置库xml学习(一)

时间:2017-11-06 21:12:35      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:imp   ati   也有   collect   roc   否则   roi   first   学习   

Robot framework--内置库xml学习(一)

学习XML内置库,我认为需要掌握以下几个知识点:

      第一:内置库的概念?有哪些内置库,大概都有什么关键字?有区分版本吗?跟RF版本有关么?为什么内置库有些需要import,有些不需要import?

      第二:XML内置库使用的是python的哪个标准库?对这个标准库需要有哪些基本的了解?

      第三:内置库是怎么构建起来的?基本关键字是否能灵活的使用?

      第四:有时候可能需要稍微修改下内置库,比如增加一些关键字等,该怎么修改?

1、内置库的概念

  内置库在官网上称为standard library,即标准库;其他比如:seleniumlibrary、androidlibrary等,官网称为external library,即外部库,第三方库。

技术分享

  (1)对于标准库,这些库直接绑定在robot framework内,在python安装目录下\lib\site-packages\robot\libraries下可以看到,无需在下载。技术分享

  (2)对于外部库,需要根据个人或者公司需要,下载之后安装导入才能使用的。

  对于标准库,又分两类,类似于builtin库是robot framework自动加载到内存的,安装后按下F5就能直接使用,不需要import;而xml库需要在再次import才能正常使用。因为BuiltIn library 提供了很多常用的关键字,比如Should Be EqualConvert To Integer等,所以RF就把这个常用的库自动加载到了内存。

      不同版本的RF,支持不同的内置库而且相同的内置库里的关键字可能也是不一样的,以RF3.0(使用命令robot --version查看RF版本)为例,3.0是目前最新的RF的版本,支持很多的内置库,查看python安装目录下\Lib\site-packages\robot下的py文件,可以看到:

   技术分享

   基本官网写的10个标准库都能在这里面找到相应的py文件。BuiltIn,Collections,DateTime,Dialogs,Process,OperatingSystem,Remote(没有关键字,暂时不算在内),Screenshot,String,Telnet,XML.这11个库,有些是在RF2.0的时候就已经有了的,最晚的DateTime,Process,XML是在RF2.8之后才内置的,也就是说如果当前使用的是RF2.8之前的版本,内置库是无法直接import XML就是使用的,需要下载安装才能使用,这点需要注意下,不同的RF版本,相同的标准库之间也是会细微的区别,这需要仔细的去查看保准库内每个版本的使用文档。

技术分享

 2、11个标准库的简介

技术分享

    这个表的来源是来自官网的,官网的用户手册文档已经描述的非常详细了。学习的时候可以详细的查看官网的相关文档。

3、XML内置库的学习

    从内置库的XML的源码可以看出,RF使用的是ETree来对xml进行解析的,部分源码如下:

 1 import copy
 2 import re
 3 import os
 4 
 5 try:
 6     from lxml import etree as lxml_etree
 7 except ImportError:
 8     lxml_etree = None
 9 
10 from robot.api import logger
11 from robot.libraries.BuiltIn import BuiltIn
12 from robot.utils import (asserts, ET, ETSource, is_string, is_truthy,
13                          plural_or_not as s)
14 from robot.version import get_version
15 
16 
17 should_be_equal = asserts.assert_equal
18 should_match = BuiltIn().should_match
19 
20 
21 class XML(object):
22  ROBOT_LIBRARY_SCOPE = GLOBAL
23     ROBOT_LIBRARY_VERSION = get_version()
24     _xml_declaration = re.compile(^<\?xml .*\?>)
25   def __init__(self, use_lxml=False):
26     use_lxml = is_truthy(use_lxml)
27         if use_lxml and lxml_etree:
28             self.etree = lxml_etree
29             self.modern_etree = True
30             self.lxml_etree = True
31         else:
32             self.etree = ET
33             self.modern_etree = ET.VERSION >= 1.3
34             self.lxml_etree = False
35         if use_lxml and not lxml_etree:
36             logger.warn(XML library reverted to use standard ElementTree 
37                         because lxml module is not installed.)
38 
39 
40   def parse_xml(self, source, keep_clark_notation=False):
41    with ETSource(source) as source:
42             tree = self.etree.parse(source)
43         if self.lxml_etree:
44             strip = (lxml_etree.Comment, lxml_etree.ProcessingInstruction)
45             lxml_etree.strip_elements(tree, *strip, **dict(with_tail=False))
46         root = tree.getroot()
47         if not is_truthy(keep_clark_notation):
48             NameSpaceStripper().strip(root)
49         return root

   python提供了几个标准库都可以对xml进行解析,之前我使用的是DOM,基于RF使用的是ETree,便开始学习了下ETree的开发文档。学习对XML文件的操作,那肯定也得对XML本身有最基本的了解,比如XML的用途,树结构,节点类型(DOM),带命名空间的xml。下面是部分的知识点的总结:

    xml是一种可扩展的标记语言。要求标记需要成对的出现(有时候会进行简写<b/>)。一个典型的xml文档如下所示:

 1 <example>
 2   <first id="1">text</first>
 3   <second id="2">
 4     <child/>
 5   </second>
 6   <third>
 7     <child>more text</child>
 8     <second id="child"/>
 9     <child><grandchild/></child>
10   </third>
11 </example>

 

     A. 整个xml文档是一个文档节点,属于根节点,比如上述文档的<example>节点就是一个根节点,一个xml文件只能有一个根节点,否则解析的时候胡报错的

     B.每个 XML 标签是一个元素节点,比如<first> 和<second>, <third>都属于元素节点,却属于<example>的子节点。

     C.attribute值:表示节点元素的属性值,比如first 有一个属性id,属性值为1;second也有id属性,属性值为2,而third没有属性。

     D.Text值:表示元素中的文本内容。比如:first 的text值就为1;second没有,third也没有;

     一个xml还包含其他的内容:比如处理指令和一些注释;在python的etree标准库解析的过程中,是直接把这二个给剔除掉了。有兴趣的可以根据官网给出的开发文档,把常用的一些方法都敲一遍,主要的还是使用2个类 Element Objects和ElementTree Objects。

4、xml官网学习

 1 XML
 2 
 3 Library version:    3.0.2
 4 Library scope:    global
 5 Named arguments:    supported
 6 Introduction
 7 
 8 Robot Framework test library for verifying and modifying XML documents.
 9 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 Pythons ElementTree XML API.
10 The library has the following main usages:
11 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).
12 Getting text or attributes of elements (e.g. Get Element Text and Get Element Attribute).
13 Directly verifying text, attributes, or whole elements (e.g Element Text Should Be and Elements Should Be Equal).
14 Modifying XML and saving it (e.g. Set Element Text, Add Element and Save XML).
15 Table of contents
16 Parsing XML
17 Using lxml
18 Example
19 Finding elements with xpath
20 Element attributes
21 Handling XML namespaces
22 Boolean arguments
23 Shortcuts
24 Keywords

   

Robot framework--内置库xml学习(一)

标签:imp   ati   也有   collect   roc   否则   roi   first   学习   

原文地址:http://www.cnblogs.com/feifei-cyj/p/7795001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!