Search code examples
pythonseleniumlettuce

Python function (lettuce step) always retrurn True


I am working with selenium and lettuce for testing in 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. I even used len() but no success
Here is the result of print statement.
Right Now total rows in table: True


Solution

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