How to use Condition in Robot Framework

18,206 views
Skip to first unread message

ankur

unread,
Aug 16, 2012, 8:52:53 AM8/16/12
to robotframe...@googlegroups.com
Hi,
    I want to create test cases in Robot framework for following scenario,

For example,
if (page contain) = abc text
{
 Click button A
}
Else
Click button B

On my Home page ,say if text 'abc' present then i want to Click button A else click button B

Plz give me solution for it
Thanks
Ankur

Tatu aalto

unread,
Aug 16, 2012, 9:14:01 AM8/16/12
to anku...@gmail.com, robotframe...@googlegroups.com

Ugh

You could use the BuildIn library Run Keyword If (1) and Run Keyword Unless (2) keywords.

See the Run Keyword If keyword documentation for examples.

-Tatu
(1)  http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html?r=2.7.3#Run%20Keyword%20If
(2) http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html?r=2.7.3#Run%20Keyword%20Unless

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/robotframework-users/-/Cwpd_KudAZoJ.
To post to this group, send email to robotframe...@googlegroups.com.
To unsubscribe from this group, send email to robotframework-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/robotframework-users?hl=en.

Tatu Aalto

unread,
Aug 17, 2012, 2:36:07 AM8/17/12
to ankur solanki, robotframework-users
Ugh

Well no, I did have something in like this mind:
${is abc text present}=     Get abc text from page
Run Keyword If     '${is abc text present}' == 'abc'     Click Button A    
Run Keyword Unless     '${is abc text present}' == 'abc'     Click Button B

${is abc text present}  is a scalar variable [1], not a list variable [2]. Get abc text from page, Click Button A and Click Button B are user keywords [3] that will do the test logic that you want. Run Keyword If and Run Keyword Unless are the library keywords [4] from the BuildIn library [5].

The Get abc text from page user keyword should look the text 'abc' from your page, and return text 'abc' if it is present. If the text 'abc' is not present, then it should return something else, normal way to do is to return empty. Then the Run Keyword If and Run Keyword Unless library keywords will validate condition and execute the correct user keyword to click the correct button in your page. 


- Tatu
[1] http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.3#scalar-variables
[2] http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.3#list-variables
[3] http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.3#creating-user-keywords
[4] http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.3#using-test-libraries
[5] robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html

On 16.8.2012 17:04, ankur solanki wrote:
will i have to add new list variable in resource.txt, say @{text} with value abc in it??
then in test case i have to use like that?
Run Keyword If   '${text}'=='PASS'    then what comes in Action section???? mean if it pass then i have to click on 'A' button

On Thu, Aug 16, 2012 at 7:14 PM, Tatu aalto <aalto...@gmail.com> wrote:

Ugh

The ${status} is just variable, you can replaced it with any variable you like. In your case it would be the variable where you have stored the text 'abc' if it is present in you page. If the text 'abc' is not present in you page, then the variable should have something else in it.

The actions are other keywords, in your case I would assume that they would be user defined keywords: click button A and Click button B.

-Tatu

On Aug 16, 2012 4:32 PM, "ankur solanki" <anku...@gmail.com> wrote:
Run Keyword If   '${status}' == 'PASS'   Some Action
Run Keyword Unless  '${status}' == 'PASS'  Another Action

the Scalar 'status' i have to add in resource file??
In 'Some Action' i have to use click keyword???? How to use it ,,i am not able to understand 

E T H I E S

unread,
Jan 10, 2014, 6:41:50 AM1/10/14
to robotframe...@googlegroups.com
Thank you Tatu, this concept helped me too.

Example for reference:

Verify the Username in table
    ${status}    ${value} =    Run Keyword And Ignore Error    Page1
    Run Keyword If    '${status}' == 'PASS'    ClickNextButton
    Run Keyword Unless    '${status}' == 'PASS'    Click the Modify icon

ClickNextButton
    Click link    userListTable_next
    ${status}    ${value} =    Run Keyword And Ignore Error    Page1
    Run Keyword If    '${status}' == 'PASS'    Verify the Username in table
    Run Keyword Unless    '${status}' == 'PASS'    Click the Modify icon

Click the Modify icon
    Click Element    xpath=//td[contains(.,"${USERNAME}")]/../td[6]//img

Page1
    Page Should not Contain Element    xpath=//td[contains(.,"${USERNAME}")]/../td[2]

Supraja Reddy

unread,
May 27, 2014, 2:13:08 AM5/27/14
to robotframe...@googlegroups.com
HI,

I have used the below code but i am unable to execute correctly.I am verifying different email addresses whether they are logging in successfully and if not logging in successfully,i am 'log'(saving) the Email address.Can someone advice me.

Testcase:

${status} ${value}= Run Keyword And Ignore Error Condition
Run Keyword If '${status}' == 'PASS' Click logout
Run Keyword Unless ${status} == PASS Save Invalid Email

Condition: Page Should Contain Link //span[@id='global-scv-loggedin']/div[2]/a[2]

Click Logout: Click Link //span[@id='global-scv-loggedin']/div[2]/a[2]

Save Invalid Email: log ${username${var}}

In advance thank you.

Thanks,
Supraja
 
Message has been deleted

Kevin O.

unread,
May 30, 2014, 12:53:40 PM5/30/14
to robotframe...@googlegroups.com
Next time please provide the Test Case failure text.
You are missing quotes in the condition that you have in the previous line:
Run Keyword Unless ${status} == PASS

I would consider changing the logic to this (adjust for your AUT):
Wait Until Page Contains Element    xpath=//*[contains(.,"Invalid Login Attempt")] | //span[@id='global-scv-loggedin']/div[2]/a[2]    # this will keep the script in sync with the AUT.  each XPath should find an element in only one scenario (succussful or unscuccessful login). note the pipe in the middle
${logged in}=    Get Matching Xpath Count    //span[@id='global-scv-loggedin']/div[2]/a[2]    # no Is Element Present keyword, this is the next best thing
Run Keyword If    ${logged in}    Click logout
Run Keyword Unless ${logged in} Save Invalid Email

This way you are not blindly ignoring any error. The XPath count is an integer but in the condition to the Run keywords, a non-zero integer is True and 0 is False.
Reply all
Reply to author
Forward
0 new messages