Parama Sivam
Parama Sivam

Reputation: 117

How to use if/else condition inside a FOR loop in robot framework

This is my code:

:FOR    ${a}    IN RANGE    2    ${Row_Count}

\    Run Keyword If    '${temp}'== 'True'    Click Link    xpath=//table[@id='listAllSTR']/tbody/tr[${a}]/td[2]/a    and 
\    ...    Screen validation for Answered    ${STR_detail}    and
\    ...    ELSE    Continue For Loop
\    Run Keyword If    ${a}>${Row_Count}   Exit For Loop**

When the if condition passes (i.e. if '${temp}'== 'True'), I need to click a link, but I'm getting an error saying

Keyword 'Selenium2Library.Click Link' expected 1 argument, got 5.

I don't know what to do.

Can anyone help me out?

Upvotes: 2

Views: 12767

Answers (2)

Dev
Dev

Reputation: 2813

in robot framework ... are used to continue the code on next line as part of previous line in the example you trying after click link keyword you add ... which causing this error remove those and your code will start running.
click link keyword accepts only one parameter as locator in your case it considers following to lines as parameter

:FOR    ${a}    IN RANGE    2    ${Row_Count}

\    Run Keyword If    '${temp}'== 'True'    Click Link    
xpath=//table[@id='listAllSTR']/tbody/tr[${a}]/td[2]/a   
\    Screen validation for Answered    ${STR_detail}   
\    ELSE    Continue For Loop
\    Run Keyword If    ${a}>${Row_Count}   Exit For Loop

edit
new syntax of for loop

FOR    ${a}    IN RANGE    2    ${Row_Count}

    Run Keyword If    '${temp}'== 'True'    Click Link    
xpath=//table[@id='listAllSTR']/tbody/tr[${a}]/td[2]/a   
    Screen validation for Answered    ${STR_detail}   
    ELSE    Continue For Loop
    Run Keyword If    ${a}>${Row_Count}   Exit For Loop
END

Upvotes: 0

Chandella07
Chandella07

Reputation: 2117

About the Issue.

You are executing multiple keywords in your if statement so, it is taking other keywords as arguments to first one.

Solution

You can create a custom keyword and add other keywords to it. Use this custom keyword in your if statement. see below example.

*** Keywords ***
Custom Keyword From If
    [Documentation]    Keywords documentation.
    keyword1
    keyword2

*** Test Cases ***
Test Custom Keyword
    Run Keyword If    '${a}'=='True'    Custom Keyword From If

NOTE:

For executing multiple keywords robot has the keyword "run keywords" see the documentation link

Upvotes: 6

Related Questions