Python函数(生菜步骤)总是重新生成True

时间:2016-06-23 09:49:42

标签: python selenium lettuce

我正在使用硒和生菜在python中进行测试。 我有计算员工表行的这一步

@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

我还有另外一步,在这一步中,我想在点击添加员工按钮后转到下一页之前保存员工表中的员工数(使用上面的步骤)。

@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) 

但有趣的是,我总是得到" True"而不是列表计数。我甚至使用 len() 但没有成功 这是print语句的结果。
现在表中的总行数:True

1 个答案:

答案 0 :(得分:1)

您需要将计数放入某个全局变量中。请参阅以下更新的步骤。

@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)