def wait_for_element_not_exist(element, timeout=20): """wait for a webelement to stop existing in the DOM. Wait for a webelement to stop existing in the DOM or until the timeout ends. If the webelement still exists after the time ended it will not raise an exception. """ try: timeout = int(timeout) except: raise Exception('Timeout should be digits only') execution.logger.info( 'Waiting for element {} to not exist'.format(element)) webelement = None try: s = get_browser().find(element, timeout=3) except: execution.logger.debug('Element already does not exist, continuing...') return start_time = time.time() still_exists = True remaining_time = time.time() - start_time while still_exists and remaining_time <= timeout: execution.logger.debug('Element still exists in the DOM, waiting...') time.sleep(0.5) remaining_time = time.time() - start_time try: webelement = get_browser().find(element, timeout=0) except: still_exists = False execution.logger.debug('Element stopped existing')
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 test(data): actions.open_browser() actions.navigate(data.env.url) assert browser.get_browser().current_url == data.env.url actions.open_browser('second_browser') actions.activate_browser('second_browser') actions.navigate(data.env.url + 'elements/') assert browser.get_browser().current_url == data.env.url + 'elements/' actions.activate_browser('main') assert browser.get_browser().current_url == data.env.url
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 refresh_page(): """Refresh the page.""" _run_wait_hook() step_message = 'Refresh page' browser.get_browser().refresh() #get_browser().execute_script("location.reload()") #browser = get_browser() #browser.get(browser.current_url); execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def test(data): actions.open_browser() actions.navigate(data.env.url) assert browser.get_browser().current_url == data.env.url actions.open_browser('second_browser') actions.activate_browser('second_browser') assert golem_steps.get_last_step_message( ) == 'Activate browser second_browser' actions.navigate(data.env.url + 'elements/') assert browser.get_browser().current_url == data.env.url + 'elements/' actions.activate_browser('main') assert browser.get_browser().current_url == data.env.url
def create_project(project_name, ignore_exists=False): actions.click(create_project_button) actions.wait_for_element_displayed(project_name_input) actions.send_keys(project_name_input, project_name) try: actions.click(create_button) actions.wait_for_element_displayed(create_project_button, 2) except TimeoutException as e: if ignore_exists: if common.error_modal_is_displayed(): get_browser().refresh() return else: raise e
def navigate(url): step_message = 'Navigate to: \'{0}\''.format(url) driver = get_browser() driver.get(url) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def mouse_hover(element): _run_wait_hook() driver = get_browser() webelement = driver.find(element) step_message = 'Mouse hover element \'{0}\''.format(webelement.name) execution.logger.info(step_message) ActionChains(driver).move_to_element(webelement).perform()
def get_cookies(): """Returns a list of dictionaries, corresponding to cookies visible in the current session. """ execution.logger.debug('Get all current cookies') driver = browser.get_browser() return driver.get_cookies()
def wait_for_element_not_visible(element, timeout=20): """Wait for an element to stop being visible. After the timeout, this won't throw an exception. Parameters: element : element timeout (optional, default: 20) : value """ try: timeout = int(timeout) except: raise Exception('Timeout should be digits only') execution.logger.info( 'Waiting for element {} to be not visible'.format(element)) webelement = None try: webelement = browser.get_browser().find(element, timeout=3) except: execution.logger.debug('Element is already not visible, continuing...') return if webelement: start_time = time.time() timed_out = False while webelement.is_displayed() and not timed_out: execution.logger.debug('Element is still visible, waiting...') time.sleep(0.5) if time.time() - start_time > timeout: timed_out = True execution.logger.info('Timeout, element is still visible.')
def delete_all_cookies(): """Delete all cookies from the current session. Note: this only deletes cookies from the current domain. """ execution.logger.debug('Delete all cookies') driver = browser.get_browser().delete_all_cookies()
def wait_for_element_not_exist(element, timeout=20): """Wait for a webelement to stop existing in the DOM. If the webelement still exists after the timeout ended, it will not raise an exception. Parameters: element : element timeout (optional, default: 20) : value """ try: timeout = int(timeout) except: raise Exception('Timeout should be digits only') execution.logger.info('Waiting for element {} to not exist'.format(element)) webelement = None try: s = browser.get_browser().find(element, timeout=3) except: execution.logger.debug('Element already does not exist, continuing...') return start_time = time.time() still_exists = True remaining_time = time.time() - start_time while still_exists and remaining_time <= timeout: execution.logger.debug('Element still exists in the DOM, waiting...') time.sleep(0.5) remaining_time = time.time() - start_time try: webelement = get_browser().find(element, timeout=0) except: still_exists = False execution.logger.debug('Element stopped existing')
def wait_for_element_not_visible(element, timeout=20): """Wait for an element to stop being visible. After the timeout, this won't throw an exception. Parameters: element : element timeout (optional, default: 20) : value """ try: timeout = int(timeout) except: raise Exception('Timeout should be digits only') execution.logger.info('Waiting for element {} to be not visible'.format(element)) webelement = None try: webelement = browser.get_browser().find(element, timeout=3) except: execution.logger.debug('Element is already not visible, continuing...') return if webelement: start_time = time.time() timed_out = False while webelement.is_displayed() and not timed_out: execution.logger.debug('Element is still visible, waiting...') time.sleep(0.5) if time.time() - start_time > timeout: timed_out = True execution.logger.info('Timeout, element is still visible.')
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 clear(element): _run_wait_hook() webelement = get_browser().find(element) step_message = 'Clear {0} element'.format(webelement.name) execution.logger.info(step_message) webelement.clear() _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def test(data): actions.navigate(data.env.url+'elements/') element = ('id', 'input-one') actions.send_keys(element, 'test text') b = browser.get_browser() assert b.find(element).get_attribute('value') == 'test text' actions.clear_element(element) assert b.find(element).get_attribute('value') == ''
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_browser_suggestions(expected_list): suggestion_divs = get_browser().find_all( 'div.autocomplete-suggestions > div.autocomplete-suggestion') actual_suggestions = [s.text for s in suggestion_divs] for suggestion in expected_list: if suggestion not in actual_suggestions: actions.fail('Expected {} to be in browser suggestion list'.format( suggestion))
def set_window_size(width, height): _run_wait_hook() browser = get_browser() step_message = 'Set browser window size to {0}x, {1}y.'.format( width, height) browser.set_window_size(width, height) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def get_cookie(name): """Get a cookie by its name. Returns the cookie if found, None if not. Parameters: name : value """ execution.logger.debug('Get cookie "{}"'.format(name)) driver = browser.get_browser() return driver.get_cookie(name)
def accept_alert(): """Accept an alert""" # TODO implement through browser step_message = 'Accept alert' execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) driver = browser.get_browser() driver.switch_to.alert.accept()
def select_by_index(element, index): _run_wait_hook() webelement = get_browser().find(element) step_message = 'Select option of index {0} from element {1}'.format( index, webelement.name) select = selenium.webdriver.support.select.Select(webelement) select.select_by_index(index) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def verify_exists(element): _run_wait_hook() step_message = 'Verify that the element exists' execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) try: webelement = get_browser().find(element, timeout=1) except: raise ElementNotFound('Element {} does not exist'.format(element))
def navigate(url): """Navigate to a URL Parameters: url : value """ step_message = 'Navigate to: \'{0}\''.format(url) driver = browser.get_browser() driver.get(url) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def verify_text(text): _run_wait_hook() driver = get_browser() step_message = 'Verify \'{0}\' is present in page'.format(text) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) if text not in driver.page_source: raise TextNotPresent( "Text '{}' was not found in the page".format(text))
def select_by_value(element, value): _run_wait_hook() webelement = get_browser().find(element) step_message = 'Select \'{0}\' value from element {1}'.format( value, webelement.name) select = selenium.webdriver.support.select.Select(webelement) select.select_by_value(value) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def verify_is_visible(element): _run_wait_hook() webelement = get_browser().find(element) step_message = 'Verify the element \'{0}\' is visible'.format( webelement.name) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) if not webelement.is_displayed(): raise Exception('Element is not visible')
def mouse_hover(element): """Hover an element with the mouse Parameters: element : element """ _run_wait_hook() driver = browser.get_browser() webelement = driver.find(element) step_message = 'Mouse hover element \'{0}\''.format(webelement.name) execution.logger.info(step_message) ActionChains(driver).move_to_element(webelement).perform()
def click(element): """Click an element Parameters: element : element """ _run_wait_hook() webelement = browser.get_browser().find(element) step_message = 'Click {0}'.format(webelement.name) execution.logger.info(step_message) webelement.click() _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def verify_is_visible(element): """Verify an element is visible Parameters: element : element """ _run_wait_hook() webelement = browser.get_browser().find(element) step_message = 'Verify the element \'{0}\' is visible'.format(webelement.name) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) if not webelement.is_displayed(): raise Exception('Element is not visible')
def set_window_size(width, height): """Set the browser window size. Parameters: width : value height : value """ _run_wait_hook() browser = browser.get_browser() step_message = 'Set browser window size to {0}x, {1}y.'.format(width, height) browser.set_window_size(width, height) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def verify_text(text): """Verify that the given text is present anywhere in the page. Parameters: text : value """ _run_wait_hook() driver = browser.get_browser() step_message = 'Verify \'{0}\' is present in page'.format(text) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) if text not in driver.page_source: raise TextNotPresent("Text '{}' was not found in the page".format(text))
def verify_alert_is_present(): """Verify an alert is present""" # TODO implement through browser step_message = 'Verify an alert is present' execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) driver = browser.get_browser() try: alert = driver.switch_to.alert except NoAlertPresentException: assert False, 'an alert was not present'
def verify_exists(element): """Verify that en element exists. Parameters: element : element """ _run_wait_hook() step_message = 'Verify that the element exists' execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) try: webelement = browser.get_browser().find(element, timeout=1) except: raise ElementNotFound('Element {} does not exist'.format(element))
def select_by_value(element, value): """Select an option from a select dropdown by value. Parameters: element : element value : value """ _run_wait_hook() webelement = browser.get_browser().find(element) step_message = 'Select \'{0}\' value from element {1}'.format(value, webelement.name) select = selenium.webdriver.support.select.Select(webelement) select.select_by_value(value) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def select_by_index(element, index): """Select an option from a select dropdown by index. Parameters: element : element index : value """ _run_wait_hook() webelement = browser.get_browser().find(element) step_message = 'Select option of index {0} from element {1}'.format(index, webelement.name) select = selenium.webdriver.support.select.Select(webelement) select.select_by_index(index) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def verify_text_in_element(element, text): """Verify the given text is present in element. Parameters: element : element text : value """ _run_wait_hook() webelement = browser.get_browser().find(element) step_message = 'Verify element \'{0}\' contains text \'{1}\''.format(webelement.name, text) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) if text not in webelement.text: raise TextNotPresent("Text \'{0}\' was not found in element {1}. Text \'{2}\' was found." .format(text, webelement.name, webelement.text))
def verify_selected_option(element, text): """Verify an element has a selected option, passed by option text. Parameters: element : element text : value """ _run_wait_hook() webelement = browser.get_browser().find(element) select = selenium.webdriver.support.select.Select(webelement) step_message = ('Verify selected option of element \'{0}\'' ' is \'{1}\''.format(webelement.name, text)) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) if not select.first_selected_option.text == text: raise TextNotPresent('Option selected in element \'{0}\' ' 'is not {1}' .format(webelement.name, text))
def send_keys(element, text): """Send keys to an input. Parameters: element : element text : value """ _run_wait_hook() webelement = browser.get_browser().find(element) step_message = 'Write \'{0}\' in element {1}'.format(text, webelement.name) # TODO chrome driver drops some characters when calling send_keys # if execution.browser_name in ['chrome', 'chrome-headless', 'chrome-remote']: # for c in text: # webelement.send_keys(c) # time.sleep(0.1) # else: webelement.send_keys(text) execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step'])
def capture(message=''): """Take a screenshot Parameters: message (optional) : value """ _run_wait_hook() execution.logger.info('Take screenshot {}'.format(message)) driver = browser.get_browser() # store image at this point, the target directory is already # created since the beginning of the test, stored in golem.core.report_directory img_id = str(uuid.uuid4())[:8] img_path = os.path.join(execution.report_directory, '{}.png'.format(img_id)) driver.get_screenshot_as_file(img_path) if len(message) == 0: message = 'Screenshot' full_message = '{0}__{1}'.format(message, img_id) step(full_message)
def wait_for_element_enabled(element, timeout=20): """Wait for element to be enabled. After timeout this won't throw an exception. Parameters: element : element timeout (optional, default: 20) : value """ execution.logger.info('Waiting for element {} to be enabled'.format(element)) start_time = time.time() timed_out = False #webelement = None #try: webelement = browser.get_browser().find(element, timeout) enabled = webelement.is_enabled() while not enabled and not timed_out: execution.logger.debug('Element is not enabled, waiting..') time.sleep(0.5) enabled = webelement.is_displayed() if time.time() - start_time > timeout: timed_out = True
def wait_for_element_visible(element, timeout=20): """Wait for element to be visible. After timeout this won't throw an exception. Parameters: element : element timeout (optional, default: 20) : value """ try: timeout = int(timeout) except: raise Exception('Timeout should be digits only') _run_wait_hook() execution.logger.info('Waiting for element {} to be visible'.format(element)) start_time = time.time() timed_out = False webelement = browser.get_browser().find(element) while not webelement.is_displayed() and not timed_out: execution.logger.debug('Element is not visible, waiting..') time.sleep(0.5) if time.time() - start_time > timeout: timed_out = True
def get_current_url(): """Return the current browser URL """ return browser.get_browser().current_url
def get_browser(): """Get the current active browser""" return browser.get_browser()