Search code examples
seleniumrobotframeworkdata-driven-tests

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.


Solution

  • *** 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