Dharmesh Rupani
Dharmesh Rupani

Reputation: 1007

How to check that element contains child <br> tag or not?

I want to write robot framework test case for below situation: How to check that an element contains child tag(<br>)or not?

First <div>, doesn't contain <br> tag Second <div>, contains <br> as child tag

Example:

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

Upvotes: 1

Views: 2269

Answers (2)

Todor Minakov
Todor Minakov

Reputation: 20047

Here are two approaches, in the same test case. The first one gets the html value of the parent element, and then you can check for the presence of whatever substring.

The 2nd one uses xpath - it checks does the parent element has a direct child of br type - I'd recommend it, cause it doesn't depend on string parsing, with all its negatives (case, whitespace, etc).

*** 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

Upvotes: 1

Helio
Helio

Reputation: 3722

Here is a complete possible solution:

*** 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}

Upvotes: 0

Related Questions