简体   繁体   English

Python函数(生菜步骤)始终为True

[英]Python function (lettuce step) always retrurn True

I am working with selenium and lettuce for testing in python. 我正在使用硒和生菜在python中进行测试。 I have this step for counting employee table rows 我有此步骤来计算员工表行

@step('I count employee table rows')
def i_count_emp_table_rows(step):
    try:
        elems = world.driver.find_elements_by_xpath(".//*[@id='myTable']/tr")
        sum = 0
        for item in elems:
            sum= sum+1
        return sum
    except Exception, e:
        print e
        return None

And I have another step, In this step, I want to save the number of employees in employee table (using above step) before I go to next page after clicking Add Employee Button. 我还有另一个步骤,在这一步中,我想在单击添加员工按钮后转到下一页之前,将员工数保存在员工表中(使用上面的步骤)。

@step('I click the Add Employee Button')
def i_click_the_add_employee_button(step):
    world.prev_no_of_emp = step.given('I count employee table rows')
    print "Right Now total rows in table: " + str(world.pre_no_of_emp)
    done, world.driver = click_page_element(admin_add_employee_button_xpath, world.driver, wait=10) 

But the funny thing is that I always get "True" instead of a list count. 但是有趣的是,我总是得到“ True”而不是列表计数。 I even used len() but no success 我什至使用len()但没有成功
Here is the result of print statement. 这是print语句的结果。
Right Now total rows in table: True 现在表中的总行数:True

You need to put the count into some global variable. 您需要将计数放入一些全局变量中。 See below updated steps. 请参阅以下更新的步骤。

@step('I count employee table rows')
def i_count_emp_table_rows(step):
    try:
        elems = world.driver.find_elements_by_xpath(".//*[@id='myTable']/tr")
        world.count = len(elems)
    except Exception, e:
        print e.message
        world.count = None

@step('I click the Add Employee Button')
def i_click_the_add_employee_button(step):
    step.given('I count employee table rows')
    print "Right Now total rows in table: " + str(world.count)
    done, world.driver = click_page_element(admin_add_employee_button_xpath, world.driver, wait=10) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM