简体   繁体   English

使用 Robot 框架,如何解析 XML 并验证数据?

[英]Using Robot framework, how can I parse XML and verify the data?

I have the output of a robot command,which is in xml scalar variable say, ${xml}.我有一个机器人命令的 output,它位于 xml 标量变量 ${xml} 中。 How can I parse the xml to get and verify the value of ipv4 address in Robot framework?如何解析 xml 以获取并验证 Robot 框架中 ipv4 地址的值?

   <rpc-reply
       xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
       message-id="101">
     <data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores">
       <interfaces
           xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"
           xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type"
           xmlns:or="urn:ietf:params:xml:ns:yang:ietf-origin">

         <interface or:origin="or:intended">
           <name>eth0</name>
           <type>ianaift:ethernetCsmacd</type>
           <!-- other parameters from ietf-interfaces omitted -->

           <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
             <enabled or:origin="or:default">true</enabled>
             <forwarding or:origin="or:default">false</forwarding>
             <mtu or:origin="or:system">1500</mtu>
             <address>
               <ip>192.0.2.1</ip>

First of all, your XML is invalid as it does not have closing tags.首先,您的 XML 无效,因为它没有结束标签。 I corrected it and here's the XML.我纠正了它,这是 XML。

ipaddress.xml ipaddress.xml

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
<data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores">
    <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type" xmlns:or="urn:ietf:params:xml:ns:yang:ietf-origin">
        <interface or:origin="or:intended">
            <name>eth0</name>
            <type>ianaift:ethernetCsmacd</type>
            <!-- other parameters from ietf-interfaces omitted -->

            <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
                <enabled or:origin="or:default">true</enabled>
                <forwarding or:origin="or:default">false</forwarding>
                <mtu or:origin="or:system">1500</mtu>
                <address>
                    <ip>192.0.2.1</ip>
                </address>
            </ipv4>
        </interface>
    </interfaces>
</data>

The test file itself:测试文件本身:

XMLTest.robot XMLTest.robot

*** Settings ***
Library    XML

*** Test Cases ***
Verify IP Address
    Verify IP Address Value    192.0.2.1

*** Keywords ***
Verify IP Address Value
    [Arguments]    ${expected_value}
    ${root} =   Parse XML    ipaddress.xml
    Element Text Should Be   ${root}    ${expected_value}    data/interfaces/interface/ipv4/address/ip

When I run it: robot XMLTest.robot当我运行它时: robot XMLTest.robot

The output is: output 是:

==============================================================================
XMLTest
==============================================================================
Verify IP Address                                                     | PASS |
------------------------------------------------------------------------------
XMLTest                                                               | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================

That should do it!应该这样做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM