【问题标题】:Using Robot Framework, I am unable to get some elemnts in XML使用机器人框架,我无法在 XML 中获取某些元素
【发布时间】:2021-12-25 22:38:28
【问题描述】:

以下是 XML 文件内容:

   <?xml version="1.0" encoding="UTF-8"?>
<reports>

<!--
    ===================
          Title
    ===================
-->

    <report landingPageWidgetType="SECTION_CONTROLLER"
            width="3"
            title="Marketing"
            showTimespanFilter="true"
            backgroundColor="#0F8287">
    </report>

<!--
    ===================
          1st row
    ===================
-->

    <report reportQueryType="GENERAL_COUNTS"
            landingPageWidgetType="COUNTER_REPORT"
            generalCountsCounters="TOTAL_USERS,ACTIVE_USERS,NEW_USERS,SESSIONS"
            timeType="LAST_MONTH"
            width="3"
            title="Users"
            reportType="STATIC"
            chartType="COUNTERS">
        <Parameters>
            <Conditions>
                <Condition type="USERS_REPORTED">true</Condition>
            </Conditions>
        </Parameters>
    </report>

    <report reportQueryType="GENERAL_COUNTS"
            landingPageWidgetType="COUNTER_REPORT"
            generalCountsCounters="TOTAL_ENDPOINTS,ACTIVE_ENDPOINTS,NEW_ENDPOINTS,SESSIONS"
            timeType="LAST_MONTH"
            width="3"
            title="Endpoints"
            reportType="STATIC"
            chartType="COUNTERS">
        <Parameters>
            <Conditions>
                <Condition type="USERS_REPORTED">false</Condition>
                <Condition type="SINGLE_ENDPOINT">false</Condition>
            </Conditions>
        </Parameters>
    </report>

<!--
    ===================
          2nd row
    ===================
-->

    <report reportQueryType="GEOGRAPHIC_USERS"
            landingPageWidgetType="STANDARD_REPORT"
            timeType="LAST_12_MONTHS"
            width="2"
            title="Usage Map: Users"
            reportType="MAP"
            xAxisTitle="Country"
            yAxisTitle="# of Users">
        <Parameters>
            <Conditions>
                <Condition type="USERS_REPORTED">true</Condition>
                <Condition type="CUSTOMERS_REPORTED">true</Condition>
            </Conditions>
        </Parameters>
    </report>

    <report reportQueryType="GEOGRAPHIC_USERS"
            landingPageWidgetType="STANDARD_REPORT"
            timeType="LAST_12_MONTHS"
            width="3"
            title="Usage Map: Users"
            reportType="MAP"
            xAxisTitle="Country"
            yAxisTitle="# of Users">
        <Parameters>
            <Conditions>
                <Condition type="USERS_REPORTED">true</Condition>
                <Condition type="CUSTOMERS_REPORTED">false</Condition>
            </Conditions>
        </Parameters>
    </report>

我正在尝试获取每个报告下的标题并检查是否存在条件,显示条件的类型及其值。

我尝试了以下方法:

${xml_obj}= Parse XML   ${xmlFile}          
${title}=   Get Element Text    ${xml_obj}  @title      
Log To Console  ${title}                

//没有显示任何内容,RIDE 无法识别 @ 符号。

另外,创建了一个 for 循环来查找条件:

    @{reports}= Get Elements    ${xml_obj}  .//report//Conditions       
${elemList}=    Get elements    ${reports}[0]   *       
@{elemList}=    Convert To List ${elemList}     
Log To Console  ${elemList}             // also it was shown empty. 

FOR ${var}  IN  @{elemList}     
    @{report_title}=    Get Elements Texts  ${var}  title   
    Log To Console  ${report_title}         
END                 

我是 RFW 的新手,我被要求创建一个测试套件,该套件从 XML 中提取数据,然后将其与网页数据进行比较。

谁能指导我找到更好的方法?

谢谢,

【问题讨论】:

    标签: xml parsing robotframework


    【解决方案1】:

    您不能使用 Get Element text 来获取标题,因为标题是一个属性。此代码从报告中解析标题,应该可以帮助您入门:

    *** Settings ***
    Library    XML
    
    *** Variables ***
    ${xmlFile}    MyXMLFile.xml
    
    *** Test Cases ***
    Parse title
        ${xml_obj}=     Parse XML       ${xmlFile}
        @{elements}=    Get Elements    ${xml_obj}   report
        FOR             ${element}   IN    @{elements}
            ${title}=         Get Element Attribute    ${element}    title
            Log To Console    ${title}
        END
    

    【讨论】:

    • 太棒了!这次真是万分感谢。你是一个救生员。
    • 关于问题的第二部分,有些报告是有条件的,我怎样才能得到所有报告的条件?
    猜你喜欢
    • 2016-10-24
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2020-12-09
    • 2014-09-29
    • 2016-04-26
    • 2019-02-18
    相关资源
    最近更新 更多