I have a test, that checks an element on a page: if element A is presented, test(function) returns A and runs another function A. If element B is presented, test(function) returns B and runs another function B. How can I do it in Python? I think of using dictionary and something like "if" expression, but I hardly imagine, how to do it. Thanks in advance! I don't have exact code, just something like this :/
from Pages import SearchHelper
def finding_pop(browser):
pop_click = SearchHelper(browser)
pop_click.go_to_site()
#element = finding_element_function.get_text()
return element
if element =='A':
#function test_A_click must be run
else:
#function test_B_click must be run
def test_A_click(browser):
pop_click = SearchHelper(browser)
pop_click.go_to_site()
logs = pop_click.setting_logs_raw('performance')
result = pop_click.for_logs(pop_click.log_filter, logs)
binded = pop_click.searching_params_in_logs_pops(result, 'bind_to')
ignored = pop_click.searching_params_in_logs_pops(result, 'ignore_to')
if binded == None and ignored == None:
handles, url, url1 = pop_click.opened_in_new_window()
assert handles > 1
assert url != url1
def test_B_click(browser):
pop_click = SearchHelper(browser)
pop_click.go_to_site()
logs = pop_click.setting_logs_raw('performance')
result = pop_click.for_logs(pop_click.log_filter, logs)
binded = pop_click.searching_params_in_logs_pops(result, 'bind_to')
ignored = pop_click.searching_params_in_logs_pops(result, 'ignore_to')
if binded == None and ignored == None:
handles, url, url1 = pop_click.opened_in_current_window()
print(url, url1)
assert handles > 1
assert url != url1
CodePudding user response:
Remember that the return
statement should always be at the end of function, because on the return statement the function ends and no further parts of the function get executed. So in the finding_pop
fucntion when you write return element
, that is where your function ends. The if/else
statements below return element
are not being executed.
For this perticular scenario I think you don't need a return statemnet inside your finding_pop
function , since you're finding_pop
function is returning element
and then you're performing the if/else
on element
inside the same the same function.
You can simply change your finding_pop
function to:
def finding_pop(browser):
pop_click = SearchHelper(browser)
pop_click.go_to_site()
element = finding_element_function.get_text()
if element =="A":
test_A_click(browser)
elseif element =="B":
test_B_click(browser)
But if you are really in need of a return statement then you need to introduce a separate runner function that will take in the return of finding_pop
as an input and then perform if/else
to choose the next function to perform.
So it'll be as:
def finding_pop(browser):
pop_click = SearchHelper(browser)
pop_click.go_to_site()
element = finding_element_function.get_text()
return element
def chose_func_A_or_b(browser):
element = finding_pop(browser)
if element == "A":
test_A_click(browser)
if element == "B":
test_B_click(browser)