RobotFramework之XML

XML

Library version: 3.0.4
Library scope: global
Named arguments: supported

Introduction

Robot Framework test library for verifying and modifying XML documents.html

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.python

The library has the following main usages:shell

Table of contents

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.express

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.app

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.less

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.ide

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.

<example>
  <first id="1">text</first>
  <second id="2">
    <child/>
  </second>
  <third>
    <child>more text</child>
    <second id="child"/>
    <child><grandchild/></child>
  </third>
  <html>
    <p>
      Text with <b>bold</b> and <i>italics</i>.
    </p>
  </html>
</example>
${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 ${first} text    
Element Attribute Should Be ${first} id 1  
Element Attribute Should Be ${root} id 1 xpath=first
Element Attribute Should Be ${XML} id 1 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 ${elem.tag} third  
@{children} = Get Elements ${elem} child
Length Should Be ${children} 2  

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 childelements, parent/child xpath will match all these child elements.

${elem} = Get Element ${XML} second/child
Should Be Equal ${elem.tag} child  
${elem} = Get Element ${XML} third/child/grandchild
Should Be Equal ${elem.tag} grandchild  

Wildcards

An asterisk (*) can be used in paths instead of a tag name to denote any element.

@{children} = Get Elements ${XML} */child
Length Should Be ${children} 3  

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} = Get Element ${XML} */second/..
Should Be Equal ${elem.tag} third  

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 ${elements} 2  
${b} = Get Element ${XML} html//b
Should Be Equal ${b.text} bold  

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 Elements at the specified position. Position can be an integer (starting from 1), expression last(), or relative expression like last() - 1. third/child[1]
tag Elements with a child element named tag. 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} = Parse XML ${XML}
Should Be Equal ${root.tag} 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 ${2nd.text} ${NONE}  
${p} = Get Element ${XML} html/p
Should Be Equal ${p.text} \n${SPACE*6}Text with${SPACE}  

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} = Get Element ${XML} html/p/b
Should Be Equal ${b.tail} ${SPACE}and${SPACE}  

attrib

A Python dictionary containing attributes of the element.

${2nd} = Get Element ${XML} second
Should Be Equal ${2nd.attrib['id']} 2  
${3rd} = Get Element ${XML} third
Should Be Empty ${3rd.attrib}    

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:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml">
  <xsl:template match="/">
    <html></html>
  </xsl:template>
</xsl:stylesheet>
${root} = Parse XML ${NS} keep_clark_notation=yes
Should Be Equal ${root.tag} {http://www.w3.org/1999/XSL/Transform}stylesheet  
Element Should Exist ${root} {http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html  
Should Be Empty ${root.attrib}    

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:

<ns0:stylesheet xmlns:ns0="http://www.w3.org/1999/XSL/Transform">
  <ns0:template match="/">
    <ns1:html xmlns:ns1="http://www.w3.org/1999/xhtml"></ns1:html>
  </ns0:template>
</ns0:stylesheet>

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 ${root.tag} stylesheet    
Element Should Exist ${root} template/html    
Element Attribute Should Be ${root} xmlns http://www.w3.org/1999/XSL/Transform  
Element Attribute Should Be ${root} xmlns 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:

<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform">
  <template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml"></html>
  </template>
</stylesheet>

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} = Parse XML <root id="1" ns:id="2" xmlns:ns="http://my.ns"/>  
Element Attribute Should Be ${root} id 1
Element Attribute Should Be ${root} {http://my.ns}id 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 falsenoneor 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 ${XML} keep_clark_notation=yes # Same as the above.
Parse XML ${XML} keep_clark_notation=${TRUE} # Python True is true.
Parse XML ${XML} keep_clark_notation=${42} # Numbers other than 0 are true.

False examples:

Parse XML ${XML} keep_clark_notation=False # String false is false.
Parse XML ${XML} keep_clark_notation=no # Also string no is false.
Parse XML ${XML} keep_clark_notation=${EMPTY} # Empty string is false.
Parse XML ${XML} keep_clark_notation=${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} <new id="x"><c1/></new>    
Add Element ${XML} <c2/> xpath=new  
Add Element ${XML} <c3/> index=1 xpath=new
${new} = Get Element ${XML} new  
Elements Should Be Equal ${new} <new id="x"><c1/><c3/><c2/></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} <first/>    
Clear Element ${XML} xpath=html/p/b clear_tail=yes  
Element Text Should Be ${XML} Text with italics. xpath=html/p normalize_whitespace=yes
Clear Element ${XML}      
Elements Should Be Equal ${XML} <example/>    

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 ${elem} <first id="1">new text</first>  
Elements Should Be Equal ${copy1} <first id="new">text</first>  
Elements Should Be Equal ${copy2} <first id="1">text</first>  
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 ${XML} id 1 xpath=first
Element Attribute Should Be ${XML} id ${NONE}  

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 ${XML} id ? xpath=first
Element Attribute Should Match ${XML} id c*d 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 ${XML} id  
Element Should Not Have Attribute ${XML} xxx xpath=first

See also Get Element AttributeGet Element AttributesElement 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 messageargument. 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 ${XML} ${EMPTY} xpath=second/child
${paragraph} = Get Element ${XML} xpath=html/p
Element Text Should Be ${paragraph} Text with bold and italics. 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 ${XML} t??? xpath=first
${paragraph} = Get Element ${XML} xpath=html/p
Element Text Should Match ${paragraph} Text with * and *. 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 ${first} <first id="1">text</first>    
${p} = Get Element ${XML} html/p  
Elements Should Be Equal ${p} <p>Text with <b>bold</b> and <i>italics</i>.</p> normalize_whitespace=yes  
Elements Should Be Equal ${p} <p>Text with</p> exclude normalize

The last example may look a bit strange because the <p> element only has text Text with. The reason is that rest of the text inside <p> actually belongs to the child elements. This includes the . at the end that is the tail text of the <i> 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} = Get Element ${XML} first
Elements Should Match ${first} <first id="?">*</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 xpatharguments 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} = Evaluate Xpath ${XML} string(descendant::second[last()]/@id)  
Should Be Equal ${text} child    
${bold} = Evaluate Xpath ${XML} boolean(preceding-sibling::*[1] = 'bold') context=html/p/i
Should Be Equal ${bold} ${True}    

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} 4  
${children} = Get Child Elements ${XML} xpath=first
Should Be Empty ${children}    
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} = Get Element ${XML} second
${child} = Get Element ${element} 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} = Get Element Attribute ${XML} id xpath=first  
Should Be Equal ${attribute} 1      
${attribute} = Get Element Attribute ${XML} xx xpath=first default=value
Should Be Equal ${attribute} value      

See also Get Element AttributesElement Attribute Should BeElement 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} id  
${attributes} = Get Element Attributes ${XML} third
Should Be Empty ${attributes}    

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} = Get Element ${XML} html/p
${text} = Get Element Text ${paragraph} normalize_whitespace=yes
Should Be Equal ${text} Text with bold and italics.  

See also Get Elements TextsElement 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} 2  
${children} = Get Elements ${XML} first/child
Should Be Empty ${children}    
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 ${texts} 2  
Should Be Equal @{texts}[0] more text  
Should Be Equal @{texts}[1] ${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} = Parse XML <root><child/></root>  
${xml} = Parse XML ${CURDIR}/test.xml keep_clark_notation=True
${xml} = Parse XML ${CURDIR}/test.xml 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 ${XML} xpath=second    
Element Should Not Exist ${XML} xpath=second    
Remove Element ${XML} xpath=html/p/b remove_tail=yes  
Element Text Should Be ${XML} Text with italics. 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 ${XML} id xpath=first
Element Should Not Have Attribute ${XML} id 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 ${XML} xpath=first  
Element Should Not Have Attribute ${XML} 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 ${XML} xpath=second/child
Element Should Not Exist ${XML} 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 ${XML} attr value  
Element Attribute Should Be ${XML} attr value  
Set Element Attribute ${XML} id new xpath=first
Element Attribute Should Be ${XML} id new 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 ${XML} xxx xpath=second/child
Element Should Exist ${XML} second/xxx  
Element Should Not Exist ${XML} 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 ${XML} tail=& xpath=html/p/b  
Element Text Should Be ${XML} Text with bold&italics. xpath=html/p normalize_whitespace=yes
Set Element Text ${XML} slanted !! xpath=html/p/i
Element Text Should Be ${XML} Text with bold&slanted!! xpath=html/p 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 source, tag, 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.

Set Elements Text source, text=None,tail=None, xpath=.

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

可使用Parse XML關鍵字將XML解析爲元素結構。它接受XML文件的路徑和包含XML的字符串。關鍵字返回結構的根元素,而後包含其餘元素做爲其子元素及其子元素。將刪除源XML中可能的註釋和處理指令。

即便已定義架構,在解析期間也不驗證XML。如何處理doctype元素,不然取決於使用的XML模塊和平臺。標準的ElementTree徹底剝離了doctypes,可是當使用lxml時,它們會在保存XML時保留。使用IronPython時,根本不支持使用doctype解析XML。

Parse XML返回的元素結構以及Get Elementsource等關鍵字返回的元素能夠用做其餘關鍵字的參數。除了已經解析的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中的新增功能。

下面的簡單示例演示瞭如何使用此庫中的關鍵字以及BuiltInCollections庫來解析XML並驗證其內容。如何使用xpath表達式來查找元素以及返回的元素包含哪些屬性,以及更多示例,在「 使用xpath元素屬性查找元素」部分中進行討論。

在此示例中,以及本文檔中的許多其餘示例中,請${XML}參考如下示例XML文檔。實際上,${XML}它能夠是XML文件的路徑,也能夠包含XML自己。

<實例> <first id =「1」> text </ first> <second id =「2」> <子/> </秒> <第三> <child>更多文字</ child> <second id =「child」/> <子> <孫子/> </子> </第三> <HTML> <P> 帶<b>粗體</ b>和<i>斜體</ i>的文字。 </ p> </ HTML> </示例> 
$ {root} = 解析XML $ {} XML    
應該是平等的 $ {} root.tag    
$ {first} = 獲取元素 $ {}根 第一  
應該是平等的 $ {} first.text 文本    
字典應該包含密鑰 $ {} first.attrib ID    
元素文本應該是 $ {}第一 文本    
元素屬性應該是 $ {}第一 ID 1  
元素屬性應該是 $ {}根 ID 1 的xpath =第一
元素屬性應該是 $ {} XML ID 1 的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 第三
應該是平等的 $ {} elem.tag 第三  
@ {children} = 獲取元素 $ {} ELEM 兒童
應該是長度 $ {}兒童 2  

路徑

經過將標記名稱與正斜槓(/)組合來建立路徑。例如,parent/child匹配child元素下的全部元素parent。請注意,若是有多個parent元素都具備child元素,則parent/childxpath將匹配全部這些child元素。

$ {elem} = 獲取元素 $ {} XML 第二/兒童
應該是平等的 $ {} elem.tag 兒童  
$ {elem} = 獲取元素 $ {} XML 第三個/孩子/孫子
應該是平等的 $ {} elem.tag 孫子  

通配符

*能夠在路徑中使用星號()而不是標記名稱來表示任何元素。

@ {children} = 獲取元素 $ {} XML */兒童
應該是長度 $ {}兒童 3  

當前元素

當前元素用點(.)表示。一般,當前元素是隱式的,不須要包含在xpath中。

父元素

另外一個元素的父元素用兩個點(..)表示。請注意,沒法引用當前元素的父元素。僅在ElementTree 1.3(即Python / Jython 2.7及更高版本)中支持此語法。

$ {elem} = 獲取元素 $ {} XML */第二/..
應該是平等的 $ {} elem.tag 第三  

搜索全部子元素

兩個正斜槓(//)表示搜索全部子元素,而不只僅是直接子元素。若是從當前元素開始搜索,則須要顯式點。

@ {elements} = 獲取元素 $ {} XML 。//第二
應該是長度 $ {}元素 2  
$ {b} = 獲取元素 $ {} XML HTML // B
應該是平等的 $ {} b.text 膽大  

謂詞

謂詞容許使用除標籤名稱以外的其餘標準來選擇元素,例如,屬性或位置。它們使用語法在普通標記名稱或路徑以後指定path[predicate]。該路徑能夠具備上面解釋的通配符和其餘特殊語法。

ElementTree支持的謂詞在下表中說明。請注意,一般只在ElementTree 1.3中支持謂詞(即Python / Jython 2.7和更新版本)。

謂詞 火柴
@attrib 具備屬性的元素attrib 第二[@id]
@屬性=「值」 具備屬性attrib值的元素value * [@ ID = 「2」]
位置 指定位置的元素。位置能夠是整數(從1開始),表達式last()或相對錶達式last() - 1 第三/子[1]
標籤 帶有子元素的元素tag 第三/兒童[孫子]

謂詞也能夠堆疊起來path[predicate1][predicate2]。一個限制是可能的位置謂詞必須始終是第一位的。

元素屬性

返回元素的全部關鍵字(例如Parse XMLGet Element)都返回ElementTree的Element對象。這些元素能夠用做其餘關鍵字的輸入,但它們還包含幾個可使用擴展變量語法直接訪問的有用屬性。

下面解釋了在測試數據中使用既有用又方便的屬性。還能夠訪問其餘屬性(包括方法),但這一般在自定義庫中比直接在測試數據中更好。

這些示例使用與${XML}前面示例相同的結構。

標籤

元素的標記。

$ {root} = 解析XML $ {} XML
應該是平等的 $ {} root.tag

文本

元素包含的文本或者None元素沒有文本的Python 。請注意,文本包含可能的子元素的文本,也不包含子項之間或之間的文本。另請注意,在XML空格中很重要,所以文本還包含縮進和換行符。要獲取可能子項的文本(可選擇空格標準化),請使用「 獲取元素文本」關鍵字。

$ {1st} = 獲取元素 $ {} XML 第一
應該是平等的 $ {} 1st.text 文本  
$ {2nd} = 獲取元素 $ {} XML 第二/兒童
應該是平等的 $ {} 2nd.text $ {無}  
$ {p} = 獲取元素 $ {} XML HTML / P
應該是平等的 $ {} p.text \ n $ {SPACE * 6}帶$ {SPACE}的文字  

尾巴

在下一個打開或關閉標記以前的元素以後的文本。Python None若是元素沒有尾部。與此相似text,還tail包含可能的縮進和換行符。

$ {b} = 獲取元素 $ {} XML HTML / P / B
應該是平等的 $ {} b.tail $ {SPACE}和$ {空白}  

ATTRIB

包含元素屬性的Python字典。

$ {2nd} = 獲取元素 $ {} XML 第二
應該是平等的 $ {2nd.attrib [ '身份證']} 2  
$ {3rd} = 獲取元素 $ {} XML 第三
應該是空的 $ {} 3rd.attrib    

處理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文檔:

<xsl:stylesheet xmlns:xsl =「http://www.w3.org/1999/XSL/Transform」 的xmlns = 「http://www.w3.org/1999/xhtml」> <xsl:template match =「/」> <HTML> </ HTML> </ XSL:模板> </ XSL:樣式> 
$ {root} = 解析XML $ {} NS keep_clark_notation = YES
應該是平等的 $ {} root.tag {http://www.w3.org/1999/XSL/Transform}stylesheet  
元素應該存在 $ {}根 {http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html  
應該是空的 $ {} root.attrib    

正如您所看到的,在標記名稱中包含名稱空間URI會使xpath變得很是冗長和複雜。

若是保存XML,ElementTree會將名稱空間信息移回xmlns屬性。不幸的是它沒有恢復原始前綴:

<ns0:stylesheet xmlns:ns0 =「http://www.w3.org/1999/XSL/Transform」> <ns0:template match =「/」> <ns1:html xmlns:ns1 =「http://www.w3.org/1999/xhtml」> </ ns1:html> </ NS0:模板> </ NS0:樣式> 

結果輸出在語義上與原始輸出相同,可是這樣的修改前綴可能仍然不可取。另請注意,實際輸出略微取決於ElementTree版本。

默認命名空間處理

因爲ElementTree處理命名空間的方式使xpath變得如此複雜,所以默認狀況下,此庫從標記名稱中刪除命名空間並將該信息移回xmlns屬性。下面的示例顯示了它在實踐中的工做原理,其中${NS}變量包含與上一示例中相同的XML文檔。

$ {root} = 解析XML $ {} NS    
應該是平等的 $ {} root.tag 樣式表    
元素應該存在 $ {}根 模板/ HTML    
元素屬性應該是 $ {}根 XMLNS http://www.w3.org/1999/XSL/Transform  
元素屬性應該是 $ {}根 XMLNS http://www.w3.org/1999/xhtml 的xpath =模板/ HTML

如今標籤不包含名稱空間信息,xpath再次簡單。

此方法的一個小限制是名稱空間前綴丟失。所以,在這種狀況下,保存的輸出與原始輸出不徹底相同:

<stylesheet xmlns =「http://www.w3.org/1999/XSL/Transform」> <template match =「/」> <html xmlns =「http://www.w3.org/1999/xhtml」> </ html> </模板> </樣式表> 

此輸出在語義上也與原始輸出相同。若是原始XML只有默認名稱空間,則輸出看起來也相同。

使用lxml時的命名空間

此庫在使用lxml和不使用時都處理名稱空間。可是,與標準ElementTree相比,lxml在內部處理命名空間的方式存在差別。主要區別在於lxml存儲有關名稱空間前綴的信息,所以若是保存XML則會保留它們。另外一個明顯的區別是,若是父元素具備名稱空間,則lxml包含使用Get Element獲取的子元素中的名稱空間信息。

徹底剝離命名空間

因爲命名空間一般會增長沒必要要的複雜性,所以Parse XML支持使用徹底剝離它們strip_namespaces=True。啓用此選項後,若是保存XML,則名稱空間不會顯示在任何位置,也不會包含在名稱空間中。

屬性名稱空間

默認狀況下,XML文檔中的屬性與它們所屬的元素位於相同的名稱空間中。經過使用前綴可使用不一樣的命名空間,但這種狀況很是罕見。

若是屬性具備名稱空間前綴,則ElementTree將使用與處理元素相同的方式將其替換爲Clark Notation。由於從屬性中刪除命名空間可能會致使屬性衝突,因此此庫根本不處理屬性命名空間。所以,不管如何處理命名空間,如下示例的工做方式都相同。

$ {root} = 解析XML <root id =「1」ns:id =「2」xmlns:ns =「http://my.ns」/>  
元素屬性應該是 $ {}根 ID 1
元素屬性應該是 $ {}根 {HTTP://my.ns} ID 2

布爾參數

某些關鍵字接受以布爾值true或false處理的參數。若是這樣的參數以字符串形式給出,則若是它是空字符串或不區分大小寫,則被視爲false falsenoneno。不管其值如何,其餘字符串都被視爲true,其餘參數類型使用與Python相同的規則進行測試。

真實的例子:

解析XML $ {} XML keep_clark_notation =真 #字符串一般是正確的。
解析XML $ {} XML keep_clark_notation = YES #與上述相同。
解析XML $ {} XML keep_clark_notation = $ {TRUE} #Pcthon True是真的。
解析XML $ {} XML keep_clark_notation = $ {42} #0之外的數字爲真。

錯誤的例子:

解析XML $ {} XML keep_clark_notation =假 #String false爲false。
解析XML $ {} XML keep_clark_notation =無 #字符串no也是false。
解析XML $ {} XML keep_clark_notation = $ {EMPTY} #Empty字符串爲false。
解析XML $ {} XML keep_clark_notation = $ {FALSE} #Python False是假的。

在Robot Framework 2.9以前,全部非空字符串(包括falseno)都被認爲是真的。none在Robot Framework 3.0.3中考慮false是新的。

輸入

參數 文檔
use_lxml =假

導入庫,啓用了可選的lxml模式。

默認狀況下,此庫使用Python的標準ElementTree模塊來解析XML。若是use_lxml參數被賦予真值(參見布爾參數),則庫將使用lxml模塊。有關lxml提供的好處,請參閱使用lxml部分。

使用lxml須要在系統上安裝lxml模塊。若是啓用了lxml模式但未安裝模塊,則此庫將發出警告並恢復使用標準ElementTree。

對lxml的支持是Robot Framework 2.8.5中的新增功能。

快捷鍵

添加元素 · 清除元素 · copy元素 · 元素屬性應該是 · 元素屬性應該匹配 · 元素應該存在 · 元素應該不存在 · 元素應該沒有屬性 · 元素文字應該 · 元素文本應該匹配 · 元至字符串 · 元素應該相等 · 元素應該匹配 · 評估Xpath · 獲取子元素 · 獲取元素 ·獲取元素屬性 · 獲取元素屬性 · 獲取元素計數 · 獲取元素文本 · 獲取元素 · 獲取元素文本 · 日誌元素 · 解析Xml · 刪除元素 · 刪除元素屬性 · 刪除元素屬性 · 刪除元素 · 刪除元素屬性 · 刪除元素屬性 · 保存Xml · 設置元素屬性 · 設置元素標記 · 設置元素文本 ·設置元素屬性 · 設置元素標籤 · 設置元素文本

關鍵詞

關鍵詞 參數 文檔
添加元素 source, element, index = None, xpath =。

將子元素添加到指定的元素。

要使用source和指定要添加新元素的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

所述element添加能夠被指定爲到XML文件的路徑或做爲含有XML字符串,或者它能夠是一個已經被解析XML元素。在添加元素以前複製元素,所以修改原始元素或添加元素對另外一元素沒有影響。默認狀況下,該元素做爲最後一個子元素添加,但可使用自定義索引來更改位置。指數從零開始(0 =第一個位置,1 =第二個位置等),負數指的是結尾處的位置(-1 =倒數第二個位置,-2 =倒數第三個位置等)。

使用示例${XML}結構的示例

添加元素 $ {} XML <new id =「x」> <c1 /> </ new>    
添加元素 $ {} XML <C2 /> XPath的=新  
添加元素 $ {} XML <C3 /> 索引= 1 XPath的=新
$ {new} = 獲取元素 $ {} XML  
要素應該是平等的 $ {}新 <new id =「x」> <c1 /> <c3 /> <c2 /> </ new>    

使用「 刪除元素」或「 刪除元素」刪除元素。

清除元素 source, xpath =。, clear_tail =假

清除指定元素的內容。

要清除的元素使用source和指定xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

清除元素意味着刪除其文本,屬性和子元素。默認狀況下不會刪除元素的尾部文本,但能夠經過給出clear_tail一個真值來更改它(請參閱布爾參數)。有關尾部的更多信息,請參閱元素屬性部分。

使用示例${XML}結構的示例

清除元素 $ {} XML 的xpath =第一    
$ {first} = 獲取元素 $ {} XML 的xpath =第一  
要素應該是平等的 $ {}第一 <第一/>    
清除元素 $ {} XML 的xpath = HTML / P / B clear_tail = YES  
元素文本應該是 $ {} XML 帶斜體的文字。 的xpath = HTML / P normalize_whitespace = YES
清除元素 $ {} XML      
要素應該是平等的 $ {} XML <示例/>    

使用「 刪除元素」刪除整個元素。

複製元素 source, xpath =。

返回指定元素的副本。

要複製的元素使用source和指定xpath。它們與Get Element關鍵字具備徹底相同的語義。

若是以後修改了副本或原始元素,則更改對另外一個沒有影響。

使用示例${XML}結構的示例

$ {elem} = 獲取元素 $ {} XML 的xpath =第一
$ {copy1} = 複製元素 $ {} ELEM  
$ {copy2} = 複製元素 $ {} XML 的xpath =第一
設置元素文本 $ {} XML 新文本 的xpath =第一
設置元素屬性 $ {} COPY1 ID
要素應該是平等的 $ {} ELEM <first id =「1」>新文字</ first>  
要素應該是平等的 $ {} COPY1 <first id =「new」> text </ first>  
要素應該是平等的 $ {} COPY2 <first id =「1」> text </ first>  
元素屬性應該是 source, name, expectedxpath =。, 消息=無

驗證指定的屬性是否爲expected

使用source和指定其屬性已驗證的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。

若是name元素的屬性等於expected值,則關鍵字經過,不然失敗。可使用message參數覆蓋默認錯誤消息。

爲了測試元素沒有某個屬性,Python None(即變量${NONE})能夠用做指望值。更清潔的替代方案是使用Element We Not Attribute

使用示例${XML}結構的示例

元素屬性應該是 $ {} XML ID 1 的xpath =第一
元素屬性應該是 $ {} XML ID $ {無}  

另請參見元素屬性應匹配獲取元素屬性

元素屬性應該匹配 來源, 名稱, 模式, xpath =。消息=無

驗證指定的屬性是否匹配expected

此關鍵字的做用與「 元素屬性應該是的」徹底相同,只是能夠將指望值做爲元素屬性必須匹配的模式給出。

模式匹配與shell中的匹配文件相似,而且始終區分大小寫。在模式中,'*'匹配任何內容和'?' 匹配任何單個字符。

使用示例${XML}結構的示例

元素屬性應該匹配 $ {} XML ID 的xpath =第一
元素屬性應該匹配 $ {} XML ID 光盤 的xpath =第三/第二
元素應該存在 source, xpath =。, 消息=無

驗證一個或多個元素是否與給定元素匹配xpath

參數sourcexpath具備一樣的語義與獲取的元素關鍵字。若是xpath匹配中的一個或多個元素,則關鍵字傳遞source。可使用message參數覆蓋默認錯誤消息。

另請參見元素不該存在以及此關鍵字在內部使用的獲取元素計數

元素不該該存在 source, xpath =。, 消息=無

驗證沒有元素與給定的匹配xpath

參數sourcexpath具備一樣的語義與獲取的元素關鍵字。若是xpath匹配中的任何元素,則關鍵字將失敗source。可使用message參數覆蓋默認錯誤消息。

另請參見元素應存在以及此關鍵字在內部使用的獲取元素計數

元素不該該有屬性 source, name, xpath =。, 消息=無

驗證指定的元素是否沒有屬性name

使用source和指定其屬性已驗證的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。

若是指定的元素具備屬性,則關鍵字將失敗name。可使用message參數覆蓋默認錯誤消息。

使用示例${XML}結構的示例

元素不該該有屬性 $ {} XML ID  
元素不該該有屬性 $ {} XML XXX 的xpath =第一

另請參閱獲取元素屬性獲取元素屬性元素文本應該元素文本應該匹配

元素文本應該是 source, expected, xpath =。normalize_whitespace = Falsemessage = None

驗證指定元素的文本是否爲expected

驗證文本的元素使用source和指定xpath。它們與Get Element關鍵字具備徹底相同的語義。

要使用與Get Element Text相同的邏輯從指定的元素獲取要驗證的文本。這包括使用該normalize_whitespace選項的可選空白規範化。

若是元素的文本等於expected值,則關鍵字經過,不然失敗。可使用message參數覆蓋默認錯誤消息。使用元素文本應匹配以根據模式而不是精確值驗證文本。

使用示例${XML}結構的示例

元素文本應該是 $ {} XML 文本 的xpath =第一
元素文本應該是 $ {} XML $ {EMPTY} 的xpath =第二/子
$ {paragraph} = 獲取元素 $ {} XML 的xpath = HTML / P
元素文本應該是 $ {}段 帶粗體和斜體的文字。 normalize_whitespace = YES
元素文本應該匹配 source, pattern, xpath =。normalize_whitespace = Falsemessage = None

驗證指定元素的文本是否匹配expected

此關鍵字的工做方式與「 元素文本應該」徹底相同,只是能夠將指望值做爲元素文本必須匹配的模式給出。

模式匹配與shell中的匹配文件相似,而且始終區分大小寫。在模式中,'*'匹配任何內容和'?' 匹配任何單個字符。

使用示例${XML}結構的示例

元素文本應該匹配 $ {} XML T 450 的xpath =第一
$ {paragraph} = 獲取元素 $ {} XML 的xpath = HTML / P
元素文本應該匹配 $ {}段 帶*和*的文字。 normalize_whitespace = YES
元素到字符串 source, xpath =。, encoding =無

返回指定元素的字符串表示形式。

要轉換爲字符串的元素是使用source和指定的xpath。它們與Get Element關鍵字具備徹底相同的語義。

默認狀況下,字符串以Unicode格式返回。若是encoding賦值爲任何值,則字符串將以指定編碼中的字節形式返回。結果字符串永遠不會包含XML聲明。

另請參見日誌元素保存XML

要素應該是平等的 source, expectedexclude_children = Falsenormalize_whitespace = False

驗證給定source元素是否等於expected

兩者sourceexpected能夠做爲一個XML文件的路徑,做爲含XML字符串,或者做爲已經被解析XML元素結構。有關解析XML的更多信息,請參閱簡介

若是source元素和expected元素相等,則關鍵字經過。這包括測試元素的標籤名稱,文本和屬性。默認狀況下,也會以相同的方式驗證子元素,但能夠經過設置exclude_children爲true值來禁用它(請參閱布爾參數)。

驗證給定元素內的全部文本,但不包括它們以外的可能文本。默認狀況下,文本必須徹底匹配,但設置normalize_whitespace爲true值會使文本驗證獨立於換行符,製表符和空格量。有關處理文本的更多詳細信息,請參閱獲取元素文本約元素的關鍵字和討論文本尾部的屬性介紹

使用示例${XML}結構的示例

$ {first} = 獲取元素 $ {} XML 第一  
要素應該是平等的 $ {}第一 <first id =「1」> text </ first>    
$ {p} = 獲取元素 $ {} XML HTML / P  
要素應該是平等的 $ {P} <p>帶<b>粗體</ b>和<i>斜體</ i>的文字。</ p> normalize_whitespace = YES  
要素應該是平等的 $ {P} <p>帶</ p>的文字 排除 正常化

最後一個示例可能看起來有點奇怪,由於該<p>元素只有文本Text with。緣由是內部文本的其他部分<p>實際上屬於子元素。這包括.最後一個元素的尾部文本<i>

另請參見元素應匹配

元素應該匹配 source, expectedexclude_children = Falsenormalize_whitespace = False

驗證給定source元素是否匹配expected

此關鍵字的做用與Elements Should Be Equal徹底相同,只是指望值中的文本和屬性值能夠做爲模式給出。

模式匹配與shell中的匹配文件相似,而且始終區分大小寫。在模式中,'*'匹配任何內容和'?' 匹配任何單個字符。

使用示例${XML}結構的示例

$ {first} = 獲取元素 $ {} XML 第一
元素應該匹配 $ {}第一 <first id =「?」> * </ first>  

有關更多示例,請參閱元素應該相等

評估Xpath 來源, 表達, 上下文=。

計算給定的xpath表達式並返回結果。

使用sourcecontextarguments 指定執行表達式的上下文的元素。它們與Get Element關鍵字具備徹底相同的語義sourcexpath參數。

要評估的xpath表達式做爲expression參數給出。評估結果按原樣返回。

使用示例${XML}結構的示例

$ {count} = 評估Xpath $ {} XML 計數(第三/ *)  
應該是平等的 $ {}計數 $ {3}    
$ {text} = 評估Xpath $ {} XML 串(後裔::第二[最後一個()] / @ id)的  
應該是平等的 $ {}文 兒童    
$ {bold} = 評估Xpath $ {} XML boolean(preceding-sibling :: * [1] ='bold') 上下文= HTML / P / I
應該是平等的 $ {}大膽 $ {TRUE}    

僅當在導入庫時使用lxml模式時,此關鍵字纔有效。機器人框架2.8.5中的新功能。

獲取子元素 source, xpath =。

以列表形式返回指定元素的子元素。

要返回的子元素使用source和指定的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。

返回指定元素的全部直接子元素。若是元素沒有子元素,則返回空列表。

使用示例${XML}結構的示例

$ {children} = 獲取子元素 $ {} XML  
應該是長度 $ {}兒童 4  
$ {children} = 獲取子元素 $ {} XML 的xpath =第一
應該是空的 $ {}兒童    
獲取元素 source, xpath =。

返回source匹配的元素xpath

source能夠是XML文件的路徑,包含XML的字符串或已解析的XML元素。該xpath指定哪一個元素找到。有關可能的源和支持的xpath語法的更多詳細信息,請參閱簡介

若是多於或少於一個元素匹配,則關鍵字失敗xpath。若是要返回全部匹配的元素,請使用「 獲取元素」

使用示例${XML}結構的示例

$ {element} = 獲取元素 $ {} XML 第二
$ {child} = 獲取元素 $ {}元素 兒童

當須要整個結構時,建議使用Parse XML來解析XML。若是須要配置XML命名空間的處理方式,則必須使用它。

許多其餘關鍵字在內部使用此關鍵字,而且一般將修改XML的關鍵字記錄到二者以修改給定的源並返回它。若是源是以字符串形式給出,則修改源不適用。而後返回基於字符串解析而後修改的XML結構。

獲取元素屬性 source, name, xpath =。, 默認=無

返回指定元素的命名屬性。

要使用source和指定要返回其屬性的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。

name返回指定元素的屬性值。若是元素沒有這樣的元素,default則返回該值。

使用示例${XML}結構的示例

$ {attribute} = 獲取元素屬性 $ {} XML ID 的xpath =第一  
應該是平等的 $ {}屬性 1      
$ {attribute} = 獲取元素屬性 $ {} XML XX 的xpath =第一 默認=值
應該是平等的 $ {}屬性      

另請參閱獲取元素屬性元素屬性應該是元素屬性應匹配元素不該具備屬性

獲取元素屬性 source, xpath =。

返回指定元素的全部屬性。

要返回的元素使用source和指定xpath。它們與Get Element關鍵字具備徹底相同的語義。

屬性做爲Python字典返回。它是原始屬性的副本,所以修改它對XML結構沒有影響。

使用示例${XML}結構的示例

$ {attributes} = 獲取元素屬性 $ {} XML 第一
字典應該包含密鑰 $ {}屬性 ID  
$ {attributes} = 獲取元素屬性 $ {} XML 第三
應該是空的 $ {}屬性    

使用「 獲取元素屬性」獲取單個屬性的值。

獲取元素計數 source, xpath =。

返回並記錄給定xpath匹配的元素數。

參數source與此關鍵字內部使用的Get Elements關鍵字xpath具備徹底相同的語義。

另請參見元素應存在元素不該存在

獲取元素文本 source, xpath =。normalize_whitespace = False

返回元素的全部文本,多是空格規範化的。

要返回的文本的元素是使用source和指定的xpath。它們與Get Element關鍵字具備徹底相同的語義。

此關鍵字返回指定元素的全部文本,包括其子元素和孫子元素包含的全部文本。若是元素沒有文本,則返回空字符串。所以,返回的文本並不老是與元素的text屬性相同。

默認狀況下,元素內的全部空格(包括換行符和縮進)都按原樣返回。若是normalize_whitespace給出了一個真值(參見布爾參數),則刪除前導和尾隨空格,將換行符和製表符轉換爲空格,將多個空格摺疊爲一個空格。這在處理HTML數據時特別有用。

使用示例${XML}結構的示例

$ {text} = 獲取元素文本 $ {} XML 第一
應該是平等的 $ {}文 文本  
$ {text} = 獲取元素文本 $ {} XML 第二/兒童
應該是空的 $ {}文    
$ {paragraph} = 獲取元素 $ {} XML HTML / P
$ {text} = 獲取元素文本 $ {}段 normalize_whitespace = YES
應該是平等的 $ {}文 帶粗體和斜體的文字。  

另請參閱獲取元素文本元素文本應該元素文本應該匹配

獲取元素 source, xpath

返回source匹配的元素列表xpath

source能夠是XML文件的路徑,包含XML的字符串或已解析的XML元素。該xpath指定哪一個元素找到。有關詳細信息,請參閱簡介

匹配的元素將xpath做爲列表返回。若是沒有元素匹配,則返回空列表。若是您想得到一場比賽,請使用獲取元素

使用示例${XML}結構的示例

$ {children} = 獲取元素 $ {} XML 第三/兒童
應該是長度 $ {}兒童 2  
$ {children} = 獲取元素 $ {} XML 第一個孩子
應該是空的 $ {}兒童    
獲取元素文本 source, xpathnormalize_whitespace = False

返回xpath做爲列表匹配的全部元素的文本。

要返回的文本使用source和指定的元素xpath。它們與Get Elements關鍵字具備徹底相同的語義。

使用與Get Element Text相同的邏輯返回匹配元素的文本。這包括使用該normalize_whitespace選項的可選空白規範化。

使用示例${XML}結構的示例

@ {texts} = 獲取元素文本 $ {} XML 第三/兒童
應該是長度 $ {}文本 2  
應該是平等的 @ {文本} [0] 更多文字  
應該是平等的 @ {文本} [1] $ {EMPTY}  
日誌元素 source, level = INFO, xpath =。

記錄指定元素的字符串表示形式。

使用source和指定的元素xpath首先在內部使用Element To String關鍵字轉換爲字符串。而後使用給定的字符串記錄生成的字符串level

還會返回記錄的字符串。

解析Xml source, keep_clark_notation = False, strip_namespaces = False

將給定的XML文件或字符串解析爲元素結構。

source能夠是XML文件或含XML字符串的路徑。在這兩種狀況下,XML都被解析爲ElementTree 元素結構,並返回根元素。將刪除源XML中可能的註釋和處理指令。

正如在處理XML名稱空間部分中所討論的那樣,默認狀況下,此關鍵字會刪除ElementTree添加到標記名稱並將其移動到xmlns屬性中的名稱空間信息。這一般能夠顯着簡化處理帶有名稱空間的XML文檔。若是您不但願這種狀況發生,或者想要避免在XML沒有名稱空間時經過元素結構的小開銷,則能夠經過爲keep_clark_notation參數賦予true值來禁用此功能(請參閱布爾參數)。

若是要徹底刪除命名空間信息,以便即便保存XML也不包含它,您能夠爲strip_namespaces參數賦予真值。此功能是Robot Framework 3.0.2中的新功能。

例子:

$ {root} = 解析XML <根> <子/> </根>  
$ {xml} = 解析XML $ {} CURDIR /test.xml keep_clark_notation =真
$ {xml} = 解析XML $ {} CURDIR /test.xml strip_namespaces =真

若是要獲取某個元素而不是整個結構,請使用「 獲取元素」關鍵字。有關更多詳細信息和示例,請參閱解析XML部分。

刪除元素 source, xpath =, remove_tail = False

xpathsource結構中刪除匹配的元素。

使用與Get Element關鍵字相同的語義source指定要從中刪除的元素。返回生成的XML結構,若是是已經解析的XML結構,則也會對其進行修改。xpathsource

若是關鍵字xpath與一個元素不匹配,則關鍵字失敗。使用「 刪除元素」刪除全部匹配的元素。

默認狀況下不會刪除元素的尾部文本,但能夠經過給出remove_tail一個真值來更改它(請參閱布爾參數)。有關尾部的更多信息,請參閱元素屬性部分。

使用示例${XML}結構的示例

刪除元素 $ {} XML 的xpath =第二    
元素不該該存在 $ {} XML 的xpath =第二    
刪除元素 $ {} XML 的xpath = HTML / P / B remove_tail = YES  
元素文本應該是 $ {} XML 帶斜體的文字。 的xpath = HTML / P normalize_whitespace = YES
刪除元素屬性 source, name, xpath =。

name從指定的元素中刪除屬性。

要使用source和指定要刪除其屬性的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

刪除不存在的屬性並不是失敗。使用「 刪除元素屬性」刪除全部屬性,使用「 設置元素屬性」設置它們。

使用示例${XML}結構的示例

刪除元素屬性 $ {} XML ID 的xpath =第一
元素不該該有屬性 $ {} XML ID 的xpath =第一

只能從單個元素中刪除屬性。使用「 刪除元素屬性」可在一次調用中刪除多個元素的屬性。

刪除元素屬性 source, xpath =。

從指定元素中刪除全部屬性。

要使用source和指定要刪除的屬性的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

使用「 刪除元素屬性」刪除單個屬性,使用「 設置元素屬性」設置它們。

使用示例${XML}結構的示例

刪除元素屬性 $ {} XML 的xpath =第一  
元素不該該有屬性 $ {} XML ID 的xpath =第一

只能從單個元素中刪除屬性。使用「 刪除元素屬性」可在一次調用中刪除多個元素的全部屬性。

刪除元素 source, xpath =, remove_tail = False

xpathsource結構中刪除全部匹配的元素。

要使用與Get Elements關鍵字相同的語義source指定要從中刪除的元素。返回生成的XML結構,若是是已經解析的XML結構,則也會對其進行修改。xpathsource

若是xpath匹配沒有元素,那不是失敗。使用「 刪除元素」能夠刪除一個元素。

默認狀況下不會刪除元素的尾部文本,但可使用remove_tail刪除元素相似的參數來更改元素的尾部文本。

使用示例${XML}結構的示例

刪除元素 $ {} XML XPath的= * /兒童
元素不該該存在 $ {} XML 的xpath =第二/子
元素不該該存在 $ {} XML XPath的=第三/兒童
刪除元素屬性 source, name, xpath =。

name從指定的元素中刪除屬性。

刪除元素屬性相似,但刪除與給定匹配的全部元素的屬性xpath

Robot Framework 2.8.6中的新功能。

刪除元素屬性 source, xpath =。

從指定元素中刪除全部屬性。

刪除元素屬性相似,但刪除與給定匹配的全部元素的全部屬性xpath

Robot Framework 2.8.6中的新功能。

保存Xml , 路徑, 編碼= UTF-8

將給定元素保存到指定文件。

source使用與Get Element關鍵字相同的語義指定要保存的元素

保存元素的文件用path以及要使用的編碼表示encoding。生成的文件始終包含XML聲明。

生成的XML文件可能與原始文件不徹底相同:

  • 註釋和處理指令始終被刪除。
  • 只有在使用lxml時纔會保留可能的doctype和名稱空間前綴。
  • 其餘細微差異可能取決於ElementTree或lxml版本。

若是您只須要元素的字符串表示,請使用Element To String

設置元素屬性 來源, 名稱, 價值, xpath =。

name將指定元素的屬性設置爲value

要使用source和指定要設置的屬性的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

既能夠設置新屬性又能夠覆蓋現有屬性。使用「 刪除元素屬性」或「 刪除元素屬性 」將其刪除。

使用示例${XML}結構的示例

設置元素屬性 $ {} XML ATTR  
元素屬性應該是 $ {} XML ATTR  
設置元素屬性 $ {} XML ID 的xpath =第一
元素屬性應該是 $ {} XML ID 的xpath =第一

只能設置單個元素的屬性。使用「 設置元素屬性」在一次調用中設置多個元素的屬性。

設置元素標記 source, tag, xpath =。

設置指定元素的標記。

要使用source和指定要設置的標記的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

使用示例${XML}結構的示例

設置元素標記 $ {} XML newTag  
應該是平等的 $ {} XML.tag newTag  
設置元素標記 $ {} XML XXX 的xpath =第二/子
元素應該存在 $ {} XML 第二/ XXX  
元素不該該存在 $ {} XML 第二/兒童  

只能設置單個元素的標記。使用「 設置元素標記」在一次調用中設置多個元素的標記。

設置元素文本 source, text = None, tail = None, xpath =。

設置指定元素的文本和/或尾部文本。

要使用source和指定要設置的文本的元素xpath。它們與Get Element關鍵字具備徹底相同的語義。返回生成的XML結構,若是source是已經解析的XML結構,則也會對其進行修改。

僅當給出新值text和/或tail值時,纔會更改元素的文本和尾部文本。有關文本尾部的更多信息,請參閱元素屬性部分。

使用示例${XML}結構的示例

設置元素文本 $ {} XML 新文本 的xpath =第一  
元素文本應該是 $ {} XML 新文本 的xpath =第一  
設置元素文本 $ {} XML 尾=& 的xpath = HTML / P / B  
元素文本應該是 $ {} XML 帶粗體和斜體的文字。 的xpath = HTML / P normalize_whitespace = YES
設置元素文本 $ {} XML 傾斜 的xpath = HTML / P / I
元素文本應該是 $ {} XML 文字大膽傾斜! 的xpath = HTML / P normalize_whitespace = YES

只能設置單個元素的文本/尾部。使用「 設置元素文本」在一次調用中設置多個元素的文本/尾部。

設置元素屬性 來源, 名稱, 價值, xpath =。

name將指定元素的屬性設置爲value

Set Element Attribute相似,但設置與給定元素匹配的全部元素的屬性xpath

Robot Framework 2.8.6中的新功能。

設置元素標記 source, tag, xpath =。

設置指定元素的標記。

Set Element Tag相似,但設置與給定匹配的全部元素的標記xpath

Robot Framework 2.8.6中的新功能。

設置元素文本 source, text = None, tail = None, xpath =。

設置指定元素的文本和/或尾部文本。

Set Element Text相似,但設置與給定元素匹配的全部元素的文本或尾部xpath

Robot Framework 2.8.6中的新功能。

共有37個關鍵字。 
Libdoc於2018-04-25 23:41:29 生成。

相關文章
相關標籤/搜索