'How to ignore the fail case and continues to next value

I am looping values from an Excel file by using ${txnExcel} to search and compare the value. But sometimes ${txnExcel} is doesn't exist in the system. In that case I want to ignore the fail (as there is no need to compare value) and continue to the next ${txnExcel} by skipping the error.

open excel      ${PATH_EXCEL}
    ${exp_row_count}    get row count   Sheet1
    :For    ${i}    in range     1       ${exp_row_count}
    \   ${excel_index}      evaluate  ${i}+1
    \   ${txnExcel}    Read Cell Data By Name    Sheet1    B${excel_index}
    \   ${ServiceTypeExcel}    Read Cell Data By Name    Sheet1    C${excel_index}
    \   ${TransferAmountExcelOrigin}    Read Cell Data By Name    Sheet1    D${excel_index}
    \   ${CurrencyExcel}    Read Cell Data By Name    Sheet1    E${excel_index}
    \   ${TransferAmountExcel}  set variable    ${TransferAmountExcelOrigin} ${CurrencyExcel}
#=======================================================================================
    \   input text       name=id_or_tsn     ${txnExcel}
    \   click button    name=Submit
    \   wait until element is visible   xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]    5s

   #===========================Compare Transaction Element================================================
   \   ${txnCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]
   \   ${ServiceTypeCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[3]/div[2]/span
   \   ${TransferAmountCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[5]/div[2]/span
   \   ${CurrencyCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[4]/div[2]
   \   should be equal as strings    ${txnExcel}    ${txnCompare}       invalid TXN comparison[${i}]
   \   should be equal as strings    ${ServiceTypeExcel}    ${ServiceTypeCompare}       invalid ServiceType comparison[${i}]
   \   should be equal as strings    ${TransferAmountExcel}    ${TransferAmountCompare}       invalid Transfer Amount comparison[${i}]
   \   should be equal as strings    ${CurrencyExcel}    ${CurrencyCompare}       invalid Currency comparison[${i}]
#============================End Compare Transaction Element====================================================


Solution 1:[1]

Here the solution that work for me now .

open excel      ${PATH_EXCEL}
    ${exp_row_count}    get row count   Sheet1
    :For    ${i}    in range     1       ${exp_row_count}
    \   ${excel_index}      evaluate  ${i}+1
    \   ${txnExcel}    Read Cell Data By Name    Sheet1    B${excel_index}
    \   ${ServiceTypeExcel}    Read Cell Data By Name    Sheet1    C${excel_index}
    \   ${TransferAmountExcelOrigin}    Read Cell Data By Name    Sheet1    D${excel_index}
    \   ${CurrencyExcel}    Read Cell Data By Name    Sheet1    E${excel_index}
    \   ${TransferAmountExcel}  set variable    ${TransferAmountExcelOrigin} ${CurrencyExcel}
    #=======================================================================================
    \   input text       name=id_or_tsn     ${txnExcel}
    \   click button    name=Submit
    \   ${found_txn}    run keyword and return status   wait until element is visible   xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]    5s
    \   ${error_message}    run keyword unless  ${found_txn}    get text    xpath=/html/body/div[2]/div[2]
    \   run keyword and continue on failure     element should be visible       xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]
    \   run keyword unless      ${found_txn}        continue for loop
    #===========================Compare Transaction Element================================================
    \   ${txnCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]
    \   ${ServiceTypeCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[3]/div[2]/span
    \   ${TransferAmountCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[5]/div[2]/span
    \   ${CurrencyCompare}  get text   xpath=/html/body/div[2]/div[2]/div/div/div/div[4]/div[2]
    \   should be equal as strings    ${txnExcel}    ${txnCompare}       invalid TXN comparison[${i}]
    \   should be equal as strings    ${ServiceTypeExcel}    ${ServiceTypeCompare}       invalid ServiceType comparison[${i}]
    \   should be equal as strings    ${TransferAmountExcel}    ${TransferAmountCompare}       invalid Transfer Amount comparison[${i}]
    \   should be equal as strings    ${CurrencyExcel}    ${CurrencyCompare}       invalid Currency comparison[${i}]

Solution 2:[2]

The way I understood your question was that you cycle through items fetched from Excel. Sometimes the item in Excel does not exist in the application. If that is the case, then skip the checks for that item and fetch the next item from excel.

As correctly referenced by @pankaj mishra, the keyword Run Keyword and Ignore Error should be used. This keyword outputs 2 values (this is why you see 2 variables before it). The first one contains the status and the second one the actual value.

This is then used by the keyword Continue For Loop which breaks off this cycle when the keyword returned an error (FAIL). All the checks that followed (represented by the logging step) are then skipped.

*** Test Cases ***
TC
    @{list}    Create List    ${3}    ${6}    ${15}   ${21}
    Log To Console  \n  
    :FOR    ${id}    IN    @{list}
    \    ${status}    ${result}    
    \    ...    Run Keyword And Ignore Error    Mock Element Exists    ${id}
    \    
    \    Continue For Loop If    '${status}' == 'FAIL'
    \    Log To Console   No Failure for ${id}

*** Keywords ***
Mock Element Exists
    [Arguments]    ${id}
    &{dic}    Create Dictionary    3=1    6=2    18=6    21=7
    [Return]   ${dic['${id}']}

Solution 3:[3]

You can put a if condition on your code when dealing with ${txnExcel} .

Just check as below in your desired language.Below code is just a logic. Write it in your desired language.

if(${txnExcel} != NULL){
   // put your compare code inside this if statement.
}

Solution 4:[4]

you can use Run keyword and ignore error or Run Keyword And Continue On Failure. details about these keywords can be found from

http://robotframework.org/robotframework/latest/libraries/BuiltIn.html

also there is a good example about these keywords here

How to ignore Get Table Text from Cell, if xpath of cell not match

in your code , your can use below line

     \   ${status}    ${result}
     \    ...   Run Keyword And Ignore Error    input text       name=id_or_tsn     ${txnExcel}
     \    Continue For Loop If    '${status}' == 'FAIL'

and one more time in last to skip error for ${txnexcel}

Run Keyword And Continue On Failure    Should Be Equal As Strings  
  ${txnExcel}    ${txnCompare}       invalid TXN comparison[${i}]

Even if the value or variable ${txnExcel} is not provided , it will continue to next KW.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sidara KEO
Solution 2 A. Kootstra
Solution 3 Mahmud Riad
Solution 4