1

Here is my code: in function add_new_employee I'm getting employee_id and then i want to use that employee_id in my next function new_employee_added Can someone help me with that? thank you

employee_id= ()
def add_new_employee(driver, first_name, last_name):
    driver.find_element_by_css_selector("#menu_pim_viewPimModule").click()
    driver.find_element_by_css_selector("[name='btnAdd']").click()
    driver.find_element_by_css_selector("#firstName").send_keys(first_name)
    driver.find_element_by_css_selector("#lastName").send_keys(last_name)
    driver.find_element_by_css_selector("#photofile").\
        send_keys(os.path.abspath("cloud-computing-IT.jpg"))
    global employee_id
    employee_id = 
   driver.find_element_by_css_selector("#employeeId").get_attribute("value")
    return employee_id

def new_employee_added(driver):
    global employee_id
    driver.find_element_by_css_selector("#menu_pim_viewPimModule").click()
    for i in range(1, 50):
        try:
            driver.find_element_by_link_text("%s" % employee_id).click()
            break
        except NoSuchElementException:
            driver.find_element_by_link_text("%s" % i).click()

def test_new_employee(driver, first_name="Patrick", last_name="Patterson"):
    login(driver, username="Admin", password="Password")
    # add new user with First Name, Last Name and Photo
    add_new_employee(driver,first_name, last_name)
    #verify that new employee added
    new_employee_added(driver)
    #ckeck if we got the right employee page
    logout(driver)

in function add_new_employee I'm getting employee_id and then i want to use that employee_id in my next function new_employee_added Can someone help me with that? thank you

1
  • def new_employee_added(driver, employee_id): ? Commented Aug 5, 2017 at 22:56

2 Answers 2

1

Add the parameter to the function definition, then pass it in when you call it:

def new_employee_added(driver, employee_id):
    ...

def test_new_employee(driver, first_name="Patrick", last_name="Patterson"):
    login(driver, username="Admin", password="Password")
    # add new user with First Name, Last Name and Photo
    employee_id = add_new_employee(driver,first_name, last_name)
    #verify that new employee added
    new_employee_added(driver, employee_id)
    #ckeck if we got the right employee page
    logout(driver)
Sign up to request clarification or add additional context in comments.

5 Comments

when i do it this way function add_new_employee runs two times, i just need output from it
@IhorHarmatii no it doesn't. See my edit, hopefully that's clearer.
it runed 2 times, i wondering why, so i've tried use global var for that, look at my code, i edit it
@IhorHarmatii if it runs twice then there's some other code you're not showing.
could you edit my code, because i don't understand a little bit, thank you
0

Just save output of add_new_employee in local variable and then pass it as argument to your new_employee_added function.

2 Comments

how can i use local variable in another function? i've tried global and it works
@Ihor Harmatii, yes, you are right, but I meant using it in your test_new_employee function and save as local variable in this function. Global variable is fine also, but try to avoid using them because it could bring a mess. You can just add extra parameter to your new_employee_added function like new_employee_added(driver, employee_id).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.