【问题标题】:Python function (lettuce step) always retrurn TruePython 函数(生菜步骤)总是返回 True
【发布时间】:2016-06-23 09:49:42
【问题描述】:

我正在使用 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) 

但有趣的是,我总是得到“真”而不是列表计数。我什至使用了 len() 但没有成功
这是打印语句的结果。
现在表格中的总行数:True

【问题讨论】:

  • @MosesKoledoye 是的,我已经这样做了。上面的函数在这里被调用。
  • @HassanMehmood 好的,我明白了。谢谢。你是对的
  • @HassanMehmood 您的回答有问题。我希望这一步是通用的。因此,每当我需要检查员工表的行数时,我都会调用该步骤。但是您建议的方式将受到限制。但是是的,如果没有返回所以我需要做一些黑客攻击。
  • 已解决:)。是的,我知道这可以通过简单的功能来完成。问题已解决
  • 使用全局变量,你建议的方式

标签: python selenium lettuce


【解决方案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) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2015-12-16
    • 2013-03-25
    相关资源
    最近更新 更多