def verify_last_action(action_name, where='test'): if where == 'test': action_inputs = elements("#testSteps .step-first-input") elif where == 'setup': action_inputs = elements("#setupSteps .step-first-input") elif where == 'teardown': action_inputs = elements("#teardownSteps .step-first-input") last_input = action_inputs[-1] actions.assert_equals(last_input.get_attribute('value'), action_name)
def steps(where='test'): if where == 'test': return elements('#testSteps>.steps>.step') elif where == 'setup': return elements('#setupSteps>.steps>.step') elif where == 'teardown': return elements('#teardownSteps>.steps>.step') else: raise ValueError('invalid where value: {}'.format(where))
def assert_last_action(action_name, where='test'): action_inputs = None if where == 'test': action_inputs = elements("#testSteps .step-first-input") elif where == 'setup': action_inputs = elements("#setupSteps .step-first-input") elif where == 'teardown': action_inputs = elements("#teardownSteps .step-first-input") last_input = action_inputs[-1] actual_value = last_input.get_attribute('value') msg = 'Expected action to be {} but was {}'.format(action_name, actual_value) assert actual_value == action_name, msg
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 verify_page_in_list(page_name): actions.step('Verify that page {} is in the list'.format(page_name)) imported_pages = elements('#pageObjects>div>input.selected-page') for page in imported_pages: if page.get_attribute('value') == page_name: return raise Exception('Page {} was not found in the list'.format(page_name))
def assert_page_in_list(page_name): actions.step('Verify that page {} is in the list'.format(page_name)) imported_pages = elements('#pageObjects>div>input.page-name') for page in imported_pages: if page.get_attribute('value') == page_name: return raise AssertionError('Page {} was not found in the list'.format(page_name))
def assert_result_steps(expected_steps): if not expected_steps: assert not get_browser().element_is_present('#testRunModal .step-list') else: steps = elements('#testRunModal .step-list>li') for i, expected_step in enumerate(expected_steps): msg = 'Expected step {} to be {} but was {}'.format(i, expected_step, steps[i].text) assert steps[i].text == expected_step, msg
def assert_result_errors(expected_errors): # expects one test set if not expected_errors: assert not get_browser().element_is_present('#testRunModal .error-list') else: errors = elements('#testRunModal .error-list>li') for i, expected_error in enumerate(expected_errors): assert errors[i].text == expected_error
def verify_product_in_results(product_name): products = elements('.product_grid_display>.product_grid_item') for product in products: if product.find('.prodtitle>a').text == product_name: return raise Exception('Product {} was not found'.format(product_name))
def verify_toast_message_is_displayed(toast_message): for _ in range(6): toasts = elements('div.toast>.toast-message') for toast in toasts: if toast.text == toast_message: return time.sleep(0.5) assert False, 'Toast with message "{}" was not found'.format(toast_message)
def access_project(project_name): actions.step('Access project {}'.format(project_name)) items = elements(project_list_item) for item in items: if item.text == project_name: item.click() return raise Exception('Project {} not found'.format(project_name))
def get_toast_with_message(toast_message): for _ in range(6): toasts = elements('div.toast>.toast-message') for toast in toasts: if toast_message in toast.text: return toast time.sleep(0.5) return None
def verify_error_message(error_message): actions.step('Verify that the error {} is displayed'.format(error_message)) actions.wait_for_element_visible(error_modal) errors = elements(css='#errorList>li') for error in errors: if error.text == error_message: return raise Exception('Error message {} was not found'.format(error_message))
def assert_toast_message_is_displayed_and_contains(toast_message): actions.step('Assert a toast is displayed with message {}'.format(toast_message)) for _ in range(6): toasts = elements('div.toast>.toast-message') for toast in toasts: if toast_message in toast.text: return True time.sleep(0.5) assert False, 'Toast with message "{}" was not found'.format(toast_message)
def add_product_to_cart(product_name): products = elements('div.default_product_display') for product in products: if product.find('.prodtitle>a.wpsc_product_title').text == product_name: button = product.find(add_to_cart_button) actions.click(button) return raise Exception('The product with name {} was not found'.format(product_name))
def verify_error_message(error_message): actions.wait_for_element_visible(error_modal) items = elements(error_list_items) error_messages = [x.text for x in items] actions.capture( 'verify the application shows the error message: {}'.format( error_message)) if not error_message in error_messages: raise Exception( 'Error message {} is not present'.format(error_message))
def wait_for_test_to_run(timeout=10): actions.step('Wait for test to run') get_browser().wait_for_element_displayed('#testRunModal', timeout=10) selector = '#testRunModal i.fa.fa-cog.fa-spin' for _ in range(timeout): spinners = elements(selector) if not any(x.is_displayed() for x in spinners): return time.sleep(1) raise TimeoutError('waiting for test to finish running')
def add_element(element_def): actions.click(add_element_button) elemement_rows = elements('#elements>div.element') last_element_row = elemement_rows[-1] element_name_input = last_element_row.find('input.element-name') element_selector_input = last_element_row.find('input.element-selector') element_value_input = last_element_row.find('input.element-value') element_display_name_input = last_element_row.find( 'input.element-display-name') actions.send_keys(element_name_input, element_def[0]) actions.send_keys(element_selector_input, element_def[1]) actions.send_keys(element_value_input, element_def[2]) actions.clear_element(element_display_name_input) actions.send_keys(element_display_name_input, element_def[3]) actions.press_key(element_display_name_input, 'TAB')
def get_autocomplete_suggestions(): """Get the list of autocomplete suggestions. There must be one autocomplete suggestions list displayed otherwise it will return None """ suggestions = None auto_selector_lists = elements('.autocomplete-suggestions') for suggestion_list in auto_selector_lists: if suggestion_list.is_displayed(): suggestions = [] suggestion_elements = suggestion_list.find_all('div.autocomplete-suggestion') for suggestion in suggestion_elements: suggestions.append(suggestion.text) break return suggestions
def verify_element_exists(element_def): elemement_rows = elements('#elements>div.element') for row in elemement_rows: element_name = row.find('input.element-name').get_attribute('value') element_selector = row.find('input.element-selector').get_attribute( 'value') element_value = row.find('input.element-value').get_attribute('value') element_display_name = row.find( 'input.element-display-name').get_attribute('value') cond1 = element_name == element_def[0] cond2 = element_selector == element_def[1] cond3 = element_value == element_def[2] cond4 = element_display_name == element_def[3] if cond1 and cond2 and cond3 and cond4: return raise Exception('The element was not found')
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 assert_variable_in_datatable(column_name): header_inputs = elements('#dataTable>thead input') msg = '{} header was not found in datatable'.format(column_name) assert any(x.value == column_name for x in header_inputs), msg
def empty_column_values_by_index(index): datatable_rows = elements('#dataTable tbody tr') for row in datatable_rows: row.find_all('input')[index].clear()
def assert_amount_of_tests(expected_amount): actions.step('Assert number of tests is {}'.format(expected_amount)) rows = elements('#detailTable > tbody > tr.test-row') actual = len(rows) assert actual == expected_amount, 'expected {} tests, got {}'.format( expected_amount, actual)
def assert_amount_of_sets(amount): """Assert that the run modal has the correct amount of sets run (tabs)""" tabs = elements('#testRunModal .test-run-tab') assert len(tabs) == amount, 'expected {} tabs, got {}'.format(amount, len(tabs))
def assert_result_log_line(index, expected_line): # expects one test set test_result_logs = elements('#TestRunModalTabContainer>.test-run-tab-content>.test-result-logs>.log-line') actual_line = test_result_logs[index].text msg = 'Expected "{}" in "{}"'.format(expected_line, actual_line) assert expected_line in actual_line, msg
def _project_exists(project_name): items = elements(project_list_item) project_names = [x.text for x in items] return project_name in project_names
def project_is_present(project_name): items = elements(project_list_item) project_names = [x.text for x in items] return project_name in project_names
def fill_in_header_by_index(index, value): header_input = elements('#dataTable>thead input')[index] header_input.send_keys(value)
def verify_amount_of_tests(expected_amount): wait_until_execution_end() rows = elements('#detailTable > tbody > tr') assert len(rows) == expected_amount