Skip to content

Possibility to exit for loops with BuiltIn.Exit For Loop keyword and with custom exceptions #502

Closed

Description

@spooning

Originally submitted to Google Code by aish212 on 15 Mar 2010

I have a scenario where in i am retriving the xpath for elements from a web
page and searching for one element on that page. Now the issue is a run
through a :FOR loop to navigate through each element.
The problem here is even if i find the element which i searching for, i
cannot exist the for loop and RIDE forces the execution till the :For
condition is met.

The complications and execution time gets worse when my XPATH count drives
my :for loop to run for 500+ iteration.

I just wished if there was some logic like "Break" statement which can exit
the for loop

Activity

  1. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 16 Mar 2010

    In general I'm against adding more programming features to the test data syntax, but
    because we've already added the for loop, adding a keyword that can break it doesn't
    make the situation any worse.

    A workaround for not having break is using the Fail keyword with a special message
    inside the for loop, and having the for loop in a user keyword that is executed
    inside Run Keyword And Expect Error. One reason I think we should add the Break
    keyword is that this workaround is so ugly but people still use it.

    There are so many features already in RF 2.5 that this probably won't be included to
    it. If someone is interested to help with the implementation, testing, and/or
    documentation we can consider adding it, though.

  2. added this to the 2.5.2 milestone on Jun 29, 2014
  3. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 29 Apr 2010

    Issue 543 has been merged into this issue.

  4. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 25 May 2010

    Issue 496 has been merged into this issue.

  5. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 30 Jul 2010

    Issue 602 has been merged into this issue.

  6. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 24 Aug 2010

    The workaround explained in the comment 1 (does c1 create a link to it?) doesn't anymore work in teardowns in RF 2.5 and newer. That happens because nowadays all keywords are executed in teardowns so for loop execution doesn't stop after Fail. We need to investigate could we add Break in 2.5.2.

  7. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 25 Aug 2010

    We decided to name the new keyword Exit For Loop instead of Break because the former is more clear to non-programmers. This new keyword also works slightly differently than break in normal programming languages: you can use it also in user keywords that the for loop uses and it will still exit the loop.

    The implementation is based on exceptions having special attribute similarly as continuable exceptions and fatal errors have. The attribute is ROBOT_EXIT_FOR_LOOP and it is possible to use it also with custom keywords.

    The new functionality was implemented, with tests, in r3899. Still needs documentation, a little cleanup, and some tests for custom keywords.

  8. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @jussimalinen on 25 Aug 2010

    Documented, cleaned up and did some tests for custom keywords.

  9. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by noahjenn... on 28 Jan 2013

    "Exit Keyword" and/or "Exit Keyword If" should be a BuiltIn keyword for logical instances such as...

    --${status} | ${value} = | Run Keyword And Ignore Error | [enter keyword to test]
    --Run Keyword If (OR "Exit Keyword If") | '${status}' == 'PASS' | Exit Keyword
    --MULTIPLE additional keywords to perform if '${status}' == 'FAIL'

    ...IF THERE ARE MULTIPLE KEYWORDS TO RUN FOLLOWING A 'PASS/FAIL', the best current option is to make individual user created keywords for each outcome.
    (OR list out remainder of keywords starting with...
    --Run Keyword If | '${status}' == 'PASS/FAIL' | [keyword to run]
    --Repeat, Repeat, Repeat, etc.
    ...although this limits possible keywords as some can't be used in a secondary position, such as setting variable values where "${variable} =" is the first column)

    Any repetition of a specific order of keywords should be made into user created keywords as best practices anyway, but the "Exit Keyword" BuiltIn Keyword would be beneficial where the desired combination of keywords to run depending on result do not warrant an additional user created keyword.

    IE.
    If I want to have a keyword that logs out of a website, but I want it to first verify that the user is even logged in, it would be much more streamlined to begin the log-out keyword with a logical...
    --${status} | ${value] = | Run Keyword And Ignore Error | Element Should Be Visible | (element only visible while logged-in)
    --Exit Keyword If | '${status}' == 'FAIL'
    --Log-Out steps keywords
    ...Because this situation has 2 outcomes; 1 is to continue performing remaining keywords within the current user created keyword, and the other is to ignore remaining keywords within the current user created keyword and move onto the next test suite keyword; it would be a very beneficial to have "Exit Keyword" in the BuiltIn Library, as opposed to creating 3 keywords (1 to verify if logged-in, 1 to log out, 1 to combine the two into one keyword).

  10. spooning commented on Jun 29, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 30 Jan 2013

    Issue 496 has been merged into this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions