0

Scenario - I have a file with x number of alphabets (a, b, c, d, e, f).

I want to take this alphabets and write it into a list or a dictionary. The file can end at any alphabet but the loop must stop if it has picked up the last alphabet and there is no other alphabets.

Each alphabet must be captured individually and appended to the list/dictionary and not using variables like what is the length of file as we don't know this and it will always change.

Run loop until file is empty and add the contents into a list/dictionary. The dictionary can be as {1:a,2:b,3:c,4....}. Maybe a case of using Exit For Loop If?

4
  • There is no while loop in the framework. What you could do is read the file using the OperatingSystem library and then split it by commas or by lines and do a FOR loop to iterate the file's content. Commented Mar 19, 2021 at 18:09
  • agree, but how to determine the end of the content and terminate the loop. Trying to check this with exit for loop if. stackoverflow.com/questions/36328595/… Commented Mar 19, 2021 at 18:15
  • what if it isnt a file and it is a website that has n amount of records and your need to capture all records until you reach the end of all records, so records = None then exit for loop and by then you would store all values in a list or a dictionary Commented Mar 19, 2021 at 18:17
  • You do not actually read the file in the for loop. Get File reads the file, you just split it into a list and then loop until the list ends. As for the website it is very application dependent, if there are links you could get all web elements in a list and loop through the list. I think in most cases you should be able to acquire a list and then the a FOR loop. Commented Mar 19, 2021 at 18:44

1 Answer 1

0

There is no while loop in the framework. What you can do is the following:

If this is file.txt:

a,b,c,d,e,f

This is how you could read each letter and put it into a dictionary.

  1. Read the file as whole.
  2. Split it based on the structure of the file.
  3. Loop through the list got by the splitting.
*** Settings ***
Library    OperatingSystem
Library    Collections

*** Test Cases ***
Test
    &{dict}=    Create Dictionary
    ${key}=    Evaluate    0
    ${file}=    Get File    file.txt
    ${splitted}=    Evaluate    """${file}""".split(',')
    FOR    ${letter}    IN    @{splitted}
        ${key}=    Evaluate    ${key} + 1
        Set To Dictionary   ${dict}     ${key}    ${letter}
    END
    Log    ${dict}

As for a web example it depends on the application but here is a general approach. This test will open Stack Overflow and get all questions then log everything about them. It will only fetch the first page but you could loop (FOR) through all pages similarly.

  1. Define a locator that would match all "records".
  2. Get a list of records by using the Get WebElements keyword.
  3. Loop through the list and apply logic.
*** Settings ***
Library    SeleniumLibrary

*** Test Cases ***
Web Example
    Open Browser    https://stackoverflow.com/questions    Headless Firefox
    ${questions}=    Get WebElements    //*[starts-with(@id, "question-summary-")]
    FOR    ${q}    IN    @{questions}
        Log    ${q.text}
    END
    [Teardown]    Close Browser
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for examples.. for file example - this works. the application one is tricky as it is just getting list of questions and normal For loop. I need to find a example to solve but my approach is to run an infinite For loop with an if statement after to test the variable. if the variable is None or a value that states the end then the command Exit for loop is run so that it exits the loop and continues execution.
@Wizard You could create your own library in which you can have a while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.