def add_action(action_name, where='test'): """adds an action using the autocomplete list""" if where == 'test': steps_section = element('div#testSteps') elif where == 'setup': steps_section = element('#setupSteps') if not steps_section.is_displayed(): actions.click('#showSetupLink>a') actions.wait(0.5) elif where == 'teardown': steps_section = element('#teardownSteps') if not steps_section.is_displayed(): actions.click('#showTeardownLink>a') actions.wait(0.5) # get last input in steps section inputs = steps_section.find_all('input.form-control.step-first-input') last_input = inputs[-1] # use the last empty input or add a new one if it's not empty new_action_input = None if not len(last_input.get_attribute('value')): new_action_input = last_input else: steps_section.find('button.add-step').click() inputs = steps_section.find_all('input.form-control.step-first-input') new_action_input = inputs[-1] # actions.click('#testSteps button.add-step') # action_inputs = elements("#testSteps .step-first-input") # last_input = action_inputs[-1] actions.send_keys(new_action_input, action_name) actions.press_key(new_action_input, 'DOWN') actions.press_key(new_action_input, 'ENTER')
def _rename_elem(elem_type, old_fullpath, new_fullpath): if elem_type == 'test': tree_ul = element(id='testCasesTree') elif elem_type == 'page': tree_ul = element(id='pagesTree') elif elem_type == 'suite': tree_ul = element(id='suitesTree') else: raise ('Error: elem_type must be in {}'.format( ['test', 'page', 'suite'])) split_path = old_fullpath.split('/') elem_name = split_path.pop() if split_path: _expand_tree_path(tree_ul, list(split_path)) full_dot_path = old_fullpath.replace('/', '.') selector = "li.tree-element[fullpath='{}']".format(full_dot_path) tree_elem = tree_ul.find(selector) rename_button = tree_elem.find( '.tree-element-buttons > button.rename-button') actions.click(rename_button) actions.wait(0.5) # TODO prompt_input = element('#promptModal #promptModalInput') actions.clear(prompt_input) actions.send_keys(prompt_input, new_fullpath) actions.click('#promptModal #prompSaveButton')
def submit_prompt_modal(value): """Send a value to prompt modal and click Save button""" prompt_input = element('#promptModal #promptModalInput', wait_displayed=5) prompt_input.clear() prompt_input.send_keys(value) save_button = element('#promptModal #prompSaveButton') save_button.click() save_button.wait_not_displayed(5)
def add_test_hook(test_hook_name): element(test_hook_selector).click() if test_hook_name == 'before_test': element('#addTestHookBeforeTest').click() if test_hook_name == 'before_each': element('#addTestHookBeforeEach').click() if test_hook_name == 'after_each': element('#addTestHookAfterEach').click() if test_hook_name == 'after_test': element('#addTestHookAfterTest').click()
def test(data): actions.navigate(data.env.url + 'elements/') text_input = element('#input-one') text_area = element('#textarea-1') assert not text_input.has_focus() text_input.focus() assert text_input.has_focus() assert not text_area.has_focus() actions.send_keys(text_area, 'test') assert text_area.has_focus() assert not text_input.has_focus()
def wait_until_execution_end(timeout=30): # TODO use a timeout manager for _ in range(timeout * 2): total_tests_text = element('#totalRow .total-tests').text total_tests = int(total_tests_text) if total_tests_text else 99 total_ok_text = element('#totalRow .tests-ok').text total_ok = int(total_ok_text) if total_ok_text else 0 total_failed_text = element('#totalRow .tests-failed').text total_failed = int(total_failed_text) if total_failed_text else 0 if total_tests == total_ok + total_failed: return time.sleep(0.5) assert False, 'Timeout waiting for execution to end'
def fill_in_column_values_by_index(col_index, values): """Fill in a column with values by index. Add any extra necessary rows. """ empty_column_values_by_index(col_index) row_index = 0 for value in values: rows = elements('#dataTable tbody tr') if row_index >= len(rows): # add a row element(new_row_button).click() rows = elements('#dataTable tbody tr') rows[row_index].find_all('input')[col_index].send_keys(value) row_index += 1
def add_tag(tag_name): actions.step('Add tag {}'.format(tag_name)) tags_input_element = element(tags_input) value = tags_input_element.value if value: tag_name = ', ' + tag_name tags_input_element.send_keys(tag_name)
def user_in_table(username): rows = element(user_table).find_all('tbody>tr') for row in rows: tds = row.find_all('td') if tds[0].text == username: return row return False
def project_permission_in_table(project, permission): rows = element(project_permission_table).find_all('tr') for row in rows: tds = row.find_all('td') if tds[0].text == project and tds[1].text == permission: return row return False
def select_env(env_name): input_ = element(environments_input) for i in range(len(env_name)): actions.send_keys(input_, env_name[i]) actions.wait(0.1) actions.press_key(input_, 'DOWN') actions.press_key(input_, 'ENTER')
def select_browser(env_name): input_ = element(browser_input) for i in range(len(env_name)): actions.send_keys(input_, env_name[i]) actions.wait(0.1) actions.press_key(input_, 'DOWN') actions.press_key(input_, 'ENTER') actions.press_key(input_, 'TAB')
def assert_test_counter(selected=None, total=None): counter_text = element('#testCount').text actual_selected = counter_text.split('/')[0] actual_total = counter_text.split('/')[1] if selected: assert actual_selected == str(selected), 'expected selected tests to be {} but was {}'.format(selected, actual_selected) if total: assert actual_total == str(total), 'expected total tests to be {} but was {}'.format(total, actual_total)
def _access_elem(elem_type, fullpath): if elem_type == 'test': tree_ul = element(id='testCasesTree') elif elem_type == 'page': tree_ul = element(id='pagesTree') elif elem_type == 'suite': tree_ul = element(id='suitesTree') else: raise('Error: elem_type must be in {}'.format(['test', 'page', 'suite'])) split_path = fullpath.split('/') elem_name = split_path.pop() if split_path: _expand_tree_path(tree_ul, list(split_path)) full_dot_path = fullpath.replace('/', '.') selector = "li.tree-element[fullpath='{}']".format(full_dot_path) tree_elem = tree_ul.find(selector) actions.click(tree_elem.find('a'))
def _add_directory(elem_type, fullpath): if elem_type == 'test': tree_ul = element(id='testCasesTree') elif elem_type == 'page': tree_ul = element(id='pagesTree') else: raise('Error: elem_type must be in {}'.format(['test', 'page'])) split_path = fullpath.split('/') elem_name = split_path.pop() if split_path: _expand_tree_path(tree_ul, list(split_path)) dot_path = '.'.join(split_path) if '.'.join(split_path) else '.' form_container = tree_ul.find("li.form-container[fullpath='{}']".format(dot_path)) form_container.find('a.new-directory-link').click() add_elem_input = form_container.find('input.new-element-input') actions.send_keys(add_elem_input, elem_name) actions.press_key(add_elem_input, 'ENTER')
def get_value(code_mirror_selector='div.CodeMirror', timeout=5): """Use the Javascript codeMirror object to retrieve the value of the code editor. """ get_browser().wait_for_element_present('div.CodeMirror', timeout) code_mirror = element(code_mirror_selector) script = 'return arguments[0].CodeMirror.getValue()' all_code = get_browser().execute_script(script, code_mirror) return all_code
def wait_for_test_to_run(timeout=5): loader_icon = element('div#testRunModal div#loaderContainer') for _ in range(timeout): if not loader_icon.is_displayed(): return else: time.sleep(1) if loader_icon.is_displayed(): raise Exception('test is still running')
def navigate_to_category(category): actions.mouse_hover(category_menu) menu_categories = element(category_menu).find_all(css='.sub-menu > li > a') for m in menu_categories: if category in m.get_attribute('innerHTML'): print(m.get_attribute('innerHTML')) actions.click(m) return raise Exception('Category not found')
def _verify_elem_exists(elem_type, fullpath): if elem_type == 'test': tree_ul = element(id='testCasesTree') elif elem_type == 'page': tree_ul = element(id='pagesTree') elif elem_type == 'suite': tree_ul = element(id='suitesTree') else: raise('Error: elem_type must be in {}'.format(['test', 'page', 'suite'])) split_path = fullpath.split('/') elem_name = split_path.pop() if split_path: _expand_tree_path(tree_ul, list(split_path)) full_dot_path = fullpath.replace('/', '.') selector = "li.tree-element[fullpath='{}']".format(full_dot_path) try: tree_element = tree_ul.find(selector, timeout=1) except: raise Exception('Page {} does not exist'.format(fullpath))
def assert_tags(expected_tags): tag_value = element(tags_input).value actual_tags = [x.strip() for x in tag_value.split(',')] if '' in actual_tags: actual_tags.remove('') msg = 'expected {} tags but found {}'.format(len(expected_tags), len(actual_tags)) assert len(actual_tags) == len(expected_tags), msg for t in expected_tags: assert t in actual_tags, 'tag "{}" is not in Tags input'.format(t)
def wait_until_execution_end(timeout=30): container = element('.report-container') script = 'return arguments[0].ExecutionReport.suiteFinished' actions.wait(3) for _ in range(timeout): suite_finished = actions.get_browser().execute_script( script, container) if suite_finished: return time.sleep(1) raise Exception('Timeout waiting for execution to end')
def verify_page_directory_exists(fullpath): pages_tree_ul = element(id='pagesTree') split_path = fullpath.split('/') page_name = split_path.pop() if split_path: project_common._expand_tree_path(pages_tree_ul, list(split_path)) full_dot_path = fullpath.replace('/', '.') dir_selector = "li.branch[fullpath='{}']".format(full_dot_path) try: tree_element = pages_tree_ul.find(dir_selector, timeout=1) except: raise Exception('Page directory {} does not exist'.format(fullpath))
def add_variable_to_datatable(var_name, var_values=None): """Add a variable to the datatable to the first empty column. Add a new column if there is no empty column. Optional, add values to the column """ headers = elements('#dataTable thead tr th') # first is numbering column, remove del (headers[0]) # find first empty header empty_col_index = None for i, header in enumerate(headers): header_input = header.find('input') if header_input.value == '': empty_col_index = i break if empty_col_index is None: # add a new column element(new_column_button).click() empty_col_index = i + 1 fill_in_header_by_index(empty_col_index, var_name) if var_values: fill_in_column_values_by_index(empty_col_index, var_values)
def add_action(action_name, params=[], where='test'): """adds an action using the autocomplete list""" if where == 'test': steps_section = element('div#testSteps') elif where == 'setup': steps_section = element('#setupSteps', wait_displayed=False) if not steps_section.is_displayed(): actions.click('#showSetupLink>a') actions.wait(0.5) elif where == 'teardown': steps_section = element('#teardownSteps', wait_displayed=False) if not steps_section.is_displayed(): actions.click('#showTeardownLink>a') actions.wait(0.5) # get last step steps = steps_section.find_all('div.step') step = steps[-1] # if the last action input is not empty, add a new one if step.find('input.step-first-input').value: steps_section.find('button.add-step').click() steps = steps_section.find_all('div.step') step = steps[-1] action_input = step.find('input.step-first-input') for i in range(len(action_name)): action_input.send_keys(action_name[i]) actions.wait(0.1) actions.press_key(action_input, 'DOWN') actions.press_key(action_input, 'ENTER') # fill in each param if params: param_inputs = step.find_all('input.parameter-input') for i, param in enumerate(params): param_inputs[i].send_keys(param)
def assert_general_total_row(columns): total_row = element(general_table_total_row) for column, expected in columns.items(): if column == 'Total Tests': td = total_row.find('td[data="total-tests"]') elif column == 'Success': td = total_row.find('td[result="success"]') elif column == 'Failure': td = total_row.find('td[result="failure"]') elif column == 'Error': td = total_row.find('td[result="error"]') else: raise ValueError('{} is an incorrect column value'.format(column)) msg = 'expected column "{}" to be "{}" but was "{}"'.format( column, expected, td.text) assert td.text == expected, msg
def verify_product_categories(categories): categories = categories.split(',') categories = [x.strip() for x in categories] menu_categories = element(category_menu).find_all(css='.sub-menu > li > a') menu_categories_text = [ x.get_attribute('innerHTML') for x in menu_categories ] menu_categories_text = [ x.replace('<span></span>', '') for x in menu_categories_text ] categories_missing = [] for category in categories: if category not in menu_categories_text: categories_missing.append(category) if categories_missing: raise Exception('The following categories are missing: {}'.format( ','.join(categories_missing)))
def verify_empty_test_execution_modal_content(test_name): test_result = element('div#testRunModal .modal-body #testResultContainer') test_result_logs = test_result.find_all('#testResultLogs>div') first_log_expected = 'INFO Test execution started: {}'.format(test_name) second_log_expected = 'INFO Browser: chrome' third_log_expected = 'INFO Test passed' assert first_log_expected in test_result_logs[0].text assert second_log_expected in test_result_logs[1].text assert third_log_expected in test_result_logs[2].text assert len(test_result_logs) == 3 test_results = test_result.find_all('#testResults>.report-result>span') assert test_results[0].text == 'Result: pass' assert test_results[1].text == 'Error:' assert 'Elapsed Time:' in test_results[2].text assert test_results[3].text == 'Browser: chrome' assert test_results[4].text == 'Steps:' assert len(test_results) == 5
def assert_report_is_running(): spinner = element(main_spinner, wait_displayed=True) spinner.wait_not_displayed()
def test_detail_row_by_full_test_name(test_file, test_name): return element( '#detailTable tr.test-detail-row[test-file="{}"][test-name="{}"]'. format(test_file, test_name))
def login(username, password): actions.step('Login with user {}'.format(username)) element(username_input).send_keys(username) element(password_input).send_keys(password) element(login_button).click()