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

2024-05-15 02:54:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个robot命令的输出,它是xml标量变量,比如${xml}。 如何在Robot框架中解析xml来获取和验证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>

Tags: oriptypexmlparamsorigininterfacesietf
1条回答
网友
1楼 · 发布于 2024-05-15 02:54:49

首先,您的XML是无效的,因为它没有结束标记。我更正了它,这是XML。你知道吗

你知道吗IP地址.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>

测试文件本身:

你知道吗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

当我运行它时: robot XMLTest.robot

输出为:

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

应该可以了!你知道吗

相关问题 更多 >

    热门问题