Python函数(步骤)总是retrurn Tru

2024-05-16 09:14:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用selenium和莴苣进行python测试。 我有这个步骤来计算employee表的行数

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

但有趣的是,我总是得到“真”而不是列表计数。我甚至用了len()但是没有成功
这是打印报表的结果。
当前表中的总行数:True


Tags: worlddefstepdrivercounttableemployee步骤
1条回答
网友
1楼 · 发布于 2024-05-16 09:14:40

您需要将计数放入某个全局变量中。见以下更新步骤。你知道吗

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

相关问题 更多 >