CSV : Continue to loop even if there is an error

Hello,

Here is my code :

Read CSV file

    ${csv}=    Get File    ${CURDIR}${/}CSV/read_csv_file_inotr.csv

    @{read}=    Create List    ${csv}

    @{lines}=    Split To Lines    @{read}    1

    FOR    ${line_csv}    IN    @{lines}

        Log To Console    \n${line_csv}

        Open Browser     ${line_csv}    chrome

        ${locationURL}=    Get Location

        Should Be Equal    ${line_csv}    ${locationURL}

        Log To Console    Les URLs d'entrée et de sortie sont identiques pour l'URL ${line_csv}

    END

On this code, I want to be sure that each URL which is on the csv don’t contain redirection.
But I don’t want that the test fails once it reachs the first error (I would like that the test checks all the URLs of the csv and list the failed URLs. It would be very useful to have the entire list of the failed URLs to modify it one time !)

Could you help me ?

John

There are a few keywords which will execute keywords and will either return status about passing or failing or completely ignore the error

  • Run Keyword And Return Status
  • Run Keyword And Ignore Error
  • Run Keyword And Continue On Failure

By using Run Keyword And Return Status you can run the keyword and then run some keywords based upon whether or not it was successful. So you could gather a list of successful and failed URLS.

Hi,

Thanks for your reply !
But I don’t understand everything. I try the keywords you have mentionned but I haven’t the expected results.
Perhaps I have wrongly wrote it ?

Can you give me an example based on my code to reach my goal ?

Using the structure, workflow, and code you are starting with we could do

*** Settings ***
Library    Collections

*** Test Cases ***
Read CSV file
    @{passedURLs}=    Create List

    @{failedURLs}=    Create List

    ${csv}=    Get File    ${CURDIR}${/}CSV/read_csv_file_inotr.csv

    @{read}=    Create List    ${csv}

    @{lines}=    Split To Lines    @{read}    1

    FOR    ${line_csv}    IN    @{lines}

        Log To Console    \n${line_csv}

        Open Browser     ${line_csv}    chrome

        ${locationURL}=    Get Location

        ${matches}=    Run Keyword and Return Status    Should Be Equal    ${line_csv}    ${locationURL}

        IF   ${matches}

            Append To List  ${passedURLs}    ${libe_csv}

        ELSE

            Append To List  ${failedURLs}    ${libe_csv}

    END

   Length Should Be    ${failedURLs}    0    Les URLs d'entrée et de sortie sont identiques pour l'URL

This is a rough solution. The first fault I find is the complication that is there is a failure within the list it, as written, is hard to see which URLS failed. I guess you could do something like

    FOR  ${failedURL}  IN  ${failedURLS}

        Log To Console    Les URLs d'entrée et de sortie sont identiques pour l'URL ${failedURL}

    END

I would also seeing this ask if one might want to use DataDriver Library such that each url becomes its own test case.

1 Like

Hi,
Thanks a lot !
Your solution seems to be good but I have a question :
When I use the “Append to list” keyword, it is not incremental (it’s always equal to 1). Is it normal ?

I have modified your proposal with adding Set Global Variable ${passedURLs} and ${passedURLs}= Create List on the IF loop but I expected to have the real list of passedURLs and failedURLs on the list.

Even if this is a rough solution, if i works, I would be happy :slight_smile:

Hello,

I finally found the problem !
The “Create List” keyword must not be on the IF loop :slight_smile:

I succeed to display the failed URL and passed URL

Thanks a lot

1 Like