Ameet Pradhan
Ameet Pradhan

Reputation: 23

How to run a particular keyword multiple times in a test case of robot framework using seleniumlibrary?

I want to execute two keywords in a test-case out of which, one keyword has to be executed only once and the other has to be executed multiple times. Please find the code below to understand the logic of the execution:

 *** Settings ***
 Test Setup         OPEN CHROME BROWSER
 Test Teardown     CLOSE CHROME BROWSER
 Test Template     KEYWORD1
 Force Tags         Smoke

 *** Test Cases ***       userid         userpass      content
 VALID CREDENTIAL    [email protected]    mypass      CONTENT A
                                                      CONTENT B
                                                      CONTENT C
 *** Keywords ***
--------------------------------------------------------------------------
KEYWORD 1
[Arguments]  ${userid} {userpass}
GO TO LOGIN PAGE
ENTER USERID
ENTER PASSWORD
CLICK ON LOGIN BUTTON
-----needs to run once and then KEYWORD 2 should run thrice---------------
KEYWORD 2
[Arguments] ${content}
CLICK ON CONTENT TILE  ${content}
DO SOME ACTION
GO TO HOME

I want 'keyword 1' to be executed only once and 'keyword 2' to be repeated 3 times as per the content list. Please guide me how to handle this.

Current issue: While continuing with second test it asks userid and userpass to be passed again.

What I want to achieve: Login once into the web-portal(KEYWORD1). RUN KEYWORD 2 with CONTENT A as arguement, then with CONTENT B as arguement and finally with CONTENT C. I should not login for each time the content needs to be changed.

Upvotes: 0

Views: 10806

Answers (3)

Ameet Pradhan
Ameet Pradhan

Reputation: 23

*** Settings ***
Test Setup        KEYWORD1  ${reg_userid}  ${reg_userpass}
Test Teardown     CLOSE CHROME BROWSER
Test Template     KEYWORD2  ${content}
Force Tags         Smoke

*** Test Cases ***      content
VALID CREDENTIAL       CONTENT A
                       CONTENT B
                       CONTENT C
*** Keywords ***
#------------------------KEYWORD 1 runs once--------------------------------
KEYWORD 1
  [Arguments]  ${userid} {userpass}
  OPEN CHROME BROWSER
  GO TO LOGIN PAGE
  ENTER USERID
  ENTER PASSWORD
  CLICK ON LOGIN BUTTON
#------------------------KEYWORD 2 runs thrice-----------------------------
KEYWORD 2
  [Arguments] ${content}
  CLICK ON CONTENT TILE  ${content}
  DO SOME ACTION
  GO TO HOME

***Variables***
${reg_userid}      [email protected]
${reg_userpass}    password

Upvotes: 1

andrexorg
andrexorg

Reputation: 77

You can create another keyword like below

Execute Keyword Multiple Times
    [Arguments]    ${keyword_name}    @{params}
    FOR    ${i}    IN    @{params}
        Run Keyword    ${keyword_name}    ${i}
    END

and you can call it

Execute Keyword Multiple Times    KEYWORD 2    CONTENT A    CONTENT B ...

If You need to give more than one param you can use dict.

I suggest you another way: give to KEYWORD 2 n args and put a loop into it. The code will be more understandable.

Upvotes: 1

Bence Kaulics
Bence Kaulics

Reputation: 7271

What you need is the Repeat Keyword from the BuildIn library.

Examples:

Repeat Keyword    5 times     Go Back         
Repeat Keyword    ${var}  Some Keyword    arg1    arg2
Repeat Keyword    2 minutes   Some Keyword    arg1    arg2

Upvotes: 2

Related Questions