def wait_write(text_to_write, attempts=10, ps=None, flash='#', index=0, css_select=None, *args, **kwargs): """ writes to a text field text_to_write: text intended to be written to text field type text_to_write: string attempts: number of tries, seperated by a 1 second sleep to get the elements type attempts: int ps: logging message of your choosing type ps: string flash: accentuates your print statement in the console type flash: string index: will return the elment you specify by index type index: int xpath: an xpath like you would pass into get_element_by_xpath() type xpath: string css_select: a css selector like you would pass into get_element_by_css() type css_select: string *args, **kwargs: any element pair you would pass into get_element() e.g tag='some_tag_type', css_class='some_class_name' """ if css_select: wait_for_element(css_select=css_select) else: wait_for_element(*args, **kwargs) while attempts > 0: try: if css_select: write_textfield(get_elements_by_css(css_select)[index], text_to_write) assert_text(get_elements_by_css(css_select)[index], text_to_write) break else: write_textfield(get_elements(*args, **kwargs)[index], text_to_write) assert_text(get_elements(*args, **kwargs)[index], text_to_write) break except Exception as e: attempts -= 1 sleep(1) if attempts == 0: raise e if ps: test_print(ps, flash)
def wait_click_url(ps=None, flash='#', attempts=10, index=0, element=None, css_select=None, *args, **kwargs): """ clicks url AND waits for url to change ps: logging message of your choosing type ps: string flash: accentuates your print statement in the console type flash: string index: will return the elment you specify by index type index: int attempts: number of tries, seperated by a 1 second sleep to get the elements type attempts: int xpath: an xpath like you would pass into get_element_by_xpath() type xpath: string css_select: a css selector like you would pass into get_element_by_css() type css_select: string *args, **kwargs: any element pair you would pass into get_element() e.g tag='some_tag_type', css_class='some_class_name' """ chk_url = get_current_url() counter = 1 while chk_url == get_current_url(): if element: wait_for(click_element, element) elif css_select: try: wait_for(click_element, get_elements_by_css(css_select)[index]) except Exception as e: if counter > 2: test_print('ERROR: "{0}". Attempt {1} of {2}'.format(e, counter, attempts)) else: try: wait_for(click_element, get_elements(*args, **kwargs)[index]) except Exception as e: if counter > 2: test_print('ERROR: "{0}". Attempt {1} of {2}'.format(e, counter, attempts)) if counter == attempts: raise Exception('Error: The url did not change after {0} attempts'.format(attempts)) break else: sleep(1) counter += 1 if ps: test_print(ps, flash)
def wait_for_element(ps=None, flash='#', index=0, attempts=10, xpath=None, css_select=None, *args, **kwargs): """ Waits for one specific elements. Returns an element ps: logging message of your choosing type ps: string flash: accentuates your print statement in the console type flash: string index: will return the elment you specify by index type index: int attempts: number of tries, seperated by a 1 second sleep to get the elements type attempts: int xpath: an xpath like you would pass into get_element_by_xpath() type xpath: string css_select: a css selector like you would pass into get_element_by_css() type css_select: string *args, **kwargs: any element pair you would pass into get_element() e.g tag='some_tag_type', css_class='some_class_name' """ some_element = None counter = 1 while not some_element: try: if xpath: some_element = get_elements_by_xpath(xpath)[index] elif css_select: some_element = get_elements_by_css(css_select)[index] else: some_element = get_elements(*args, **kwargs)[index] except Exception as e: if counter > 2: test_print('ERROR: {0}. Attempt {1} of {2}'.format(e, counter, attempts)) sleep(1) counter += 1 if counter > attempts and not some_element: raise e break if some_element and ps: test_print(ps, flash) return some_element
def get_elements_multiple(args_kwargs): """ Call multiple get_elements calls, returning a list of all results. Useful when waiting for different possible elements to appear. """ ret = [] for args, kwargs in args_kwargs: l = [] try: l = a.get_elements(*args, **kwargs) except AssertionError: pass ret.extend(l) if not ret: raise AssertionError('Could not identify elements: 0 elements found') return ret
def click_delete_button(): """Remove the first device in the list by clicking the delete button.""" # Clicks the first Delete button it finds # this button is actually a link click_link(get_elements(tag='a', text_regex='Delete')[0])
# Create an account email_address = helpers.register_account() # Make sure we are not logged in helpers.logout() # Authenticate via the api so that an application can be created url = ('%s/api/1.0/authentications?ws.op=authenticate&token_name=%s' % (get_base_url(), TOKEN_NAME)) password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, email_address, PASSWORD) opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(password_mgr)) urllib2.install_opener(opener) req = urllib2.Request(url) f = urllib2.urlopen(req) data = f.read() # now test that the app is there and that we can delete it helpers.login(email_address, PASSWORD) go_to(urls.APPLICATIONS) assert_text_contains('content', TOKEN_NAME) delete_buttons = get_elements(css_class='btn-sm', name='Delete') # delete application token click_button(delete_buttons[0]) # log back in and should have no app helpers.logout() helpers.login(email_address, PASSWORD) go_to(urls.APPLICATIONS) fails(assert_text_contains, 'content', TOKEN_NAME)
# -*- Mode: Python -*- # vi:si:et:sw=4:sts=4:ts=4 from sst import actions as a import common import data admin = common.Admin(data.admin) admin.login() admin.navigate('Customers', 'Manage Customers') admin.customer_edit_by_email(data.user['email_address'][1]) # for some reason there are two buttons? I can only find one in the source buttons = a.get_elements(title='Delete Customer') # wait=False lets us interact with the alert a.click_button(buttons[0], wait=False) # confirm the dialog when clicking to delete a.accept_alert('Are you sure you want to do this?') admin.logout()