Home > Net >  How to BREAK the loops in Selenium Robot framework 3.2.2 - Getting an error 'Break' is a r
How to BREAK the loops in Selenium Robot framework 3.2.2 - Getting an error 'Break' is a r

Time:05-11

How can I break the Get data count FOR loop, once my IF condition is satisfy.

Here I want to select a date through for loop, but currently my if condition is satisfied still its continue executing the for loop.

Library    SeleniumLibrary

*** Variables ***
${browserName}    chrome
${siteURL}    https://www.booking.com/

*** Test Cases ***
Login should failed with unregistered email
    Open website login page
    Click login
    Get row count
    #[Teardown]    Close Browser

*** Keywords ***
Open website login page
    Open Browser    ${siteURL}    ${browserName}
    Maximize Browser Window
    
Click login
    Click Element    //div[@class='xp__dates-inner']//span[@class='sb-date-field__icon sb-date-field__icon-btn bk-svg-wrapper calendar-restructure-sb']
    Wait Until Page Contains Element    //div[@class='xp__dates-inner']//span[@class='sb-date-field__icon sb-date-field__icon-btn bk-svg-wrapper calendar-restructure-sb']
    
Get row count
    ${rowTRCount}=  Get Element Count    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr
    FOR  ${indexTR}    IN RANGE   1    ${rowTRCount} 1  
        ${curTRText}      Get Text      //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]
        Log To Console    TR: ${curTRText}
        Get data count    ${indexTR} 
    END
    
Get data count   
    [Arguments]    ${indexTR}
    Log To Console   ${indexTR}
    Set Test Variable    ${conditionCheck}    ${False}
    Convert To Boolean    ${conditionCheck}  
    ${rowTDCount}=  Get Element Count    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td
    FOR  ${indexTD}    IN RANGE   1   ${rowTDCount} 1
        ${noDate}    Get Text    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td[${indexTD}]
        Exit For Loop IF    '${noDate}' == ''
        ${curTDText}      Get Text      //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td[${indexTD}]//span
        Log To Console    TD: ${curTDText}
        ${CurrentDate}=      Get Time    day
        ${dayConvertToInt}=    Convert To Integer    ${CurrentDate}  
        ${finalDate}=    Evaluate   ${dayConvertToInt}  10
        Run Keyword If    ${curTDText}==${finalDate}    Select date    ${indexTR}    ${indexTD}    ${conditionCheck}
    END
    
Select date
    [Arguments]    ${indexTR}    ${indexTD}    ${conditionCheck}
    Click Element    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td[${indexTD}]//span
    BREAK

CodePudding user response:

Using Pass Execution keyword, Its working fine.

    [Arguments]    ${indexTR}
    Log To Console   ${indexTR}
    ${rowTDCount}=  Get Element Count    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td
    FOR  ${indexTD}    IN RANGE   1   ${rowTDCount} 1
        ${noDate}    Get Text    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td[${indexTD}]
        Exit For Loop IF    '${noDate}' == ''
        ${curTDText}      Get Text      //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td[${indexTD}]//span
        Log To Console    TD: ${curTDText}
        ${CurrentDate}=      Get Time    day
        ${dayConvertToInt}=    Convert To Integer    ${CurrentDate}  
        ${finalDate}=    Evaluate   ${dayConvertToInt}  10
        Run Keyword If    ${curTDText}==${finalDate}    Select date    ${indexTR}    ${indexTD}
    END
    
Select date
    [Arguments]    ${indexTR}    ${indexTD}
    Click Element    //*[@id="frm"]/div[1]/div[2]/div[2]/div/div/div[3]/div[1]/table/tbody/tr[${indexTR}]/td[${indexTD}]//span
    Pass Execution    Date is selected. 

CodePudding user response:

You can use "Exit For Loop" in the place of "Pass Execution", because pass execution "Skips rest of the current test, setup, or teardown with PASS status."

"Exit For Loop" only exits the for loop and continues the next keywords in test case For you, "Exit For Loop" would be the right choice for you as per your question.

Exit For Loop

Detailed documentation of pass execution: https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Pass Execution

  • Related