如何检查该元素是否包含子标记?

时间:2017-02-24 19:15:37

标签: selenium automation robotframework

我想针对以下情况编写机器人框架测试用例: 如何检查元素是否包含子标记(<br>)?

首先<div>,不包含<br>标记 第二个<div>,包含<br>作为子标记

示例:

<div class="heading parbase section">
     <h6 class="h3 text-success text-left " id="this-heading-is-on-a-single-line" data-qe="qa-heading-line-break-single">

        This Heading Is On A Single Line 

     </h6>    
</div>

<div class="heading parbase section">
     <h6 class="h3 text-success text-left       " id="this-heading-is-on-two-lines" data-qe="qa-heading-line-break-two">

        This Heading Is  <br>

        On Two Lines 

     </h6>
</div>

2 个答案:

答案 0 :(得分:1)

以下是两种方法,在同一测试用例中。第一个获取父元素的html值,然后您可以检查是否存在任何子字符串。

第二个使用xpath - 它检查父元素是否具有br类型的直接子元素 - 我推荐它,因为它不依赖于字符串解析,以及它的所有否定(case,whitespace等)

*** Variables ***
${parent element}       xpath=//h6[@id="this-heading-is-on-a-single-line"]

*** Test Cases ***
A testcase
    ${value}=   Get Element Attribute   ${parent element}@innerHTML

    # will be ${true} if there's <br>, ${false} otherwise
    ${string presence check}=   Run Keyword And Return Status   Should Contain   ${value}   <br>     

    ${xpath presence check}=    Run Keyword And Return Status   Element Should Be Visible   ${parent element}/br
    # again - ${true} if there's <br>; if it's not guaranteed to be a direct child of the <h6>, use // - e.g. ${parent element}//br

答案 1 :(得分:0)

这是一个完整的解决方案:

*** Settings ***
Library           String

*** Test Cases ***
Negative Test
    ${myteststring}=    Set Variable    <div class="heading parbase section"> \ \ \ \ \ <h6 class="h3 text-success text-left " id="this-heading-is-on-a-single-line" data-qe="qa-heading-line-break-single"> \ \ \ \ \ \ \ \ \ This Heading Is On A Single Line \ \ \ \ \ \ \ </h6> \ \ \ \ </div>
    ${result}=    Contains <br>?    ${myteststring}
    Should Not Be True    ${result}

Positive Test
    ${myteststring}=    Set Variable    <div class="heading parbase section"> \ \ \ \ \ <h6 class="h3 text-success text-left \ \ \ \ \ \ " id="this-heading-is-on-two-lines" data-qe="qa-heading-line-break-two"> \ \ \ \ \ \ \ \ \ This Heading Is \ <br> \ \ \ \ \ \ \ \ \ On Two Lines \ \ \ \ \ \ \ </h6> </div>
    ${result}=    Contains <br>?    ${myteststring}
    Should Be True    ${result}

*** Keywords ***
Contains <br>?
    [Arguments]    ${arg1}
    ${result}=    Get Regexp Matches    ${arg1}    (?im)<br>
    ${result}=    Evaluate    len(${result}) > 0
    [Return]    ${result}