在RobotFrameWork中执行“else”值

时间:2014-01-08 08:14:02

标签: python python-2.7 selenium selenium-webdriver robotframework

我有一个变量$ {var} 它有一个值('true'或'false')

我有一个测试用例(由4个步骤组成)

Run My Test
    Select Radio Button    CSS_ID   ${var}   #right now here value of ${var} is 'True'
    Correct window should popup 
    Select Radio Button    CSS_ID   False    #Here I've to manually write 'False' which I dont want to do 
    #Instead of writing False I want to execute this keyword 'Select Radio Button' on any value other than 'True' 
    #Like we have in traditional Programming language, 'not ${var}' or '!${var}' or similar thing 
    InCorrect window should popup 

OR Vice-Versa

Run My Test
    Select Radio Button    CSS_ID   False
    Correct window should popup 
    Select Radio Button    CSS_ID   "ELSE"  #True #here "ELSE" means anything else which is not "False"
    InCorrect window should popup

我知道run keyword ifrun keyword unless内置关键字,但在这种特殊情况下不知道如何使用。

PS:如果这是True-False组合,我会编写一个简单的脚本,但这是主要问题,我还有其他组合......就像Valid-InvalidYes-NoIs-a - Has-a等。

到目前为止我尝试过的。 。 。 我创建了自己的关键字SelectRadioBtn

from Selenium2Library import Selenium2Library

class test(Selenium2Library):
    def SelectRadioBtn(self, group_name, value):
        elements = self._get_radio_buttons(group_name)  #here I find all radio buttons with given ID
        for element in elements:
              val = element.get_attribute('value') #and then check if value of current element doesn't match with given value thats mean it's counter value we are looking for 
              if val != value:
                 break
        element = self. _get_radio_button_with_value(group_name, val) #and then use above found counter value to select radio button
        if not element.is_selected():
            element.click()

然后将其用作

Run My Test
        Select Radio Button    CSS_ID   False
        Correct window should popup 
        SelectRadioBtn    CSS_ID   False  #"True"
        InCorrect window should popup

但它不起作用:(

2 个答案:

答案 0 :(得分:0)

从文档中检查Run Keyword IfRun Keyword Unless个关键字。

答案 1 :(得分:0)

我是通过创建自己的关键字来实现的。

Select Radio Button Otherwise    CSS_ID   True

这将选择值为True以外的所有单选按钮。

这是我的最终代码

Run My Test
    Select Radio Button    CSS_ID   True
    Correct window should popup 
    Select Radio Button Otherwise    CSS_ID   True  
    InCorrect window should popup