2017-02-24 33 views
0

我想爲下面的情況編寫機器人框架測試用例: 如何檢查一個元素是否包含子標籤(<br>)?如何檢查該元素是否包含子標記<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> 

回答

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} 
+0

'\'是什麼?你在逃避白色空間嗎? – Goralight

+0

看起來如此;有一個內建變量 - '$ {space}',加上RF的乘數 - '$ {space * 5}'會產生5個空格。 – Todor

+0

'\'是由RIDE自動添加的。它保持行格式。 – Helio

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 
相關問題