def menu_click(self, menu_locator, menu_option): self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() menu_elem = self._selenium_instance._element_find(menu_locator, True, True) self._selenium_instance._info("Opening Menu '%s'" % menu_locator) ActionChains(self._selenium_instance._current_browser()).move_to_element(menu_elem).perform() self._selenium_instance._info("Selecting Menu Option '%s'" % menu_option) ActionChains(self._selenium_instance._current_browser()).click(self._selenium_instance._element_find(menu_option, True, True)).perform()
def select_from_EWB_menu(self, menu_option_text, *args): """ Selects an option from an E-WorkBook Web menu. Can be used on any menu following the standard menu format used within EWB Web. NOTE: Simulates events using native events. Currently no fallback for browsers where native events are not supported in WebDriver (i.e. Safari). *Arguments* - _menu_option_text_ - the top level menu option to click on (if no *args are specified) or hover over (if *args are specified) - _*args_ - optional additional sub menu item text *Example* | Open Insert Menu | | Select From Menu | Insert File | Other | """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() prev_menu = menu_option_text self._selenium_instance.wait_until_page_contains_element('ewb-popup-menu') self._menu_select('ewb-popup-menu', menu_option_text) for arg in args: ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_RIGHT).perform() self._selenium_instance.wait_until_page_contains_element('xpath=//div[contains(@class, "x-menu-item-active")]/a[text()="{0}"]'.format(prev_menu)) #self._ensure_menu_selected(prev_menu) prev_menu = arg menu_id = self._selenium_instance.get_element_attribute('xpath=//a[text()="{0}"]/../../..@id'.format(arg)) self._sub_menu_select(menu_id, arg) self._selenium_instance.wait_until_page_contains_element('xpath=//div[contains(@class, "x-menu-item-active")]/a[text()="{0}"]'.format(prev_menu)) ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.RETURN).perform()
def _sub_menu_select(self, menu_id, menu_option_text, load_timeout=30): """ Selects a sub-menu option SJ - Is this really needed? A sub-menu is just another menu... """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() row_number = 0 number_of_rows = 0 number_of_unselectable_rows = 0 base_time = time.time() while ((number_of_rows == 0) and ((time.time()-base_time) < load_timeout)): self._selenium_instance._info("Counting number of menu elements") number_of_rows = int(self._selenium_instance.get_matching_xpath_count('//*[@id="{0}"]/div/div'.format(menu_id))) number_of_selectable_rows = self._selenium_instance.get_matching_xpath_count('//*[@id="{0}"]/div/div/a'.format(menu_id)) number_of_unselectable_rows = int(number_of_rows) - int(number_of_selectable_rows) time.sleep(1) self._selenium_instance._info("Number of menu elements = '%s'" % number_of_rows) for index in range(1, int(number_of_rows)+1): try: if self._selenium_instance.get_text('xpath=//*[@id="{0}"]/div/div[{1}]'.format(menu_id, index)) == menu_option_text: row_number= index self._selenium_instance._info("Row number = '%s'" % row_number) except ValueError: pass row_number = row_number - number_of_unselectable_rows self._selenium_instance._info("Row number = '%s'" % row_number) for i in range(int(row_number)-1): ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_DOWN).perform()
def select_cell_range(self, start_x, start_y, end_x, end_y): """ Selects a cell range starting at cell (start_x,start_y) and ending at (end_x,end_y) """ self._selenium_instance = SeleniumInstanceHelper( )._get_selenium_instance() #click at start_x,start_y coordinates #self._selenium_instance.click_element("xpath=//*[@id='gwt-debug-cell{0},{1}']/../..".format(start_x,start_y)) #hold shift and click at end_x,end_y coordinates try: start_cell = self._selenium_instance._element_find( "xpath=//*[@id='gwt-debug-cell{0},{1}']/../..".format( start_y, start_x), True, True) end_cell = self._selenium_instance._element_find( "xpath=//*[@id='gwt-debug-cell{0},{1}']/../..".format( end_y, end_x), True, True) ActionChains( self._selenium_instance._current_browser()).click_and_hold( start_cell).move_to_element(end_cell).release().perform() except: #Handle possible StaleElementReferenceException due to matrix reload on click time.sleep(1) start_cell = self._selenium_instance._element_find( "xpath=//*[@id='gwt-debug-cell{0},{1}']/../..".format( start_y, start_x), True, True) end_cell = self._selenium_instance._element_find( "xpath=//*[@id='gwt-debug-cell{0},{1}']/../..".format( end_y, end_x), True, True) ActionChains( self._selenium_instance._current_browser()).click_and_hold( start_cell).move_to_element(end_cell).release().perform()
def download_file_from_EWB(self, url, filename, cookie='JSESSIONID'): """ Downloads the file from the given url to the given filename location. *Arguments* - _url_ - the file download url - _filename_ - the name and path of the output file - _cookie_ - the session cookie name, defaults to JSESSIONID *Example* | Download File From EWB | https://localhost:8443/my/download/url | ${OUTPUT_DIR}/downloaded_file.txt | """ self._selenium_instance = SeleniumInstanceHelper( )._get_selenium_instance() print(self._selenium_instance._current_browser().get_cookies()) cookie_value = self._selenium_instance._current_browser().get_cookie( cookie)['value'] cookie_string = '{0}={1}'.format(cookie, cookie_value) cookie_header = {'Cookie': cookie_string} print cookie_header req = urllib2.Request(url, headers=cookie_header) response = urllib2.urlopen(req) file_content = response.read() directory = os.path.dirname(filename) if not os.path.exists(directory): os.makedirs(directory) file = open(filename, 'wb') file.write(file_content) file.close()
def _ensure_menu_selected(self, menu_text, timeout=30): #checks a menu item is selected - private self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() base_time = time.time() menu_selected = False while not menu_selected and (((time.time()-base_time) < timeout)): try: self._selenium_instance.wait_until_page_contains_element('xpath=//a[text()="{0}"]/..[contains(@class, "x-menu-item-active")]'.format(menu_text)) menu_selected = True except: self._selenium_instance._info("RETRYING ELEMENT LOCATION") if not menu_selected: raise AssertionError('Menu item "{0}" not selected'.format(menu_text))
def select_new_entity_type(self, creation_type_text, entity_type): """ Selects a new entity type from the E-WorkBook Web popup menu | Open Tile Header Tool Menu | Administrators | | Select New Entity Type | Create New | Project | """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() self._menu_select('ewb-popup-menu', creation_type_text) ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_RIGHT).perform() if creation_type_text=='Use Record to Create': self._sub_menu_select('ewb-popup-menu-submenu-ewb-entity-menu-template', entity_type) else: self._sub_menu_select('ewb-popup-menu-submenu-ewb-entity-menu-new', entity_type) ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.RETURN).perform()
def _menu_select(self, menu_id, menu_option_text, load_timeout=30): """ Selects a menu option """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() row_number = 0 number_of_rows = 0 base_time = time.time() while ((number_of_rows == 0) and ((time.time()-base_time) < load_timeout)): number_of_rows = self._selenium_instance.get_matching_xpath_count('//*[@id="{0}"]/div/div'.format(menu_id)) time.sleep(1) self._selenium_instance._info("Number of menu elements = '%s'" % number_of_rows) for index in range(1, int(number_of_rows)+1): try: if self._selenium_instance.get_text('xpath=//*[@id="{0}"]/div/div[{1}]/a'.format(menu_id, index)) == menu_option_text: row_number= index self._selenium_instance._info("Row number = '%s'" % row_number) except ValueError: pass self._selenium_instance._info("Row number = '%s'" % row_number) for i in range(int(row_number)): ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_DOWN).perform()
class MenuHelperFunctions: def __init__(self): self._selenium_instance = None #EWB Menu Actions def select_from_EWB_menu(self, menu_option_text, *args): """ Selects an option from an E-WorkBook Web menu. Can be used on any menu following the standard menu format used within EWB Web. NOTE: Simulates events using native events. Currently no fallback for browsers where native events are not supported in WebDriver (i.e. Safari). *Arguments* - _menu_option_text_ - the top level menu option to click on (if no *args are specified) or hover over (if *args are specified) - _*args_ - optional additional sub menu item text *Example* | Open Insert Menu | | Select From Menu | Insert File | Other | """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() prev_menu = menu_option_text self._selenium_instance.wait_until_page_contains_element('ewb-popup-menu') self._menu_select('ewb-popup-menu', menu_option_text) for arg in args: ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_RIGHT).perform() self._selenium_instance.wait_until_page_contains_element('xpath=//div[contains(@class, "x-menu-item-active")]/a[text()="{0}"]'.format(prev_menu)) #self._ensure_menu_selected(prev_menu) prev_menu = arg menu_id = self._selenium_instance.get_element_attribute('xpath=//a[text()="{0}"]/../../..@id'.format(arg)) self._sub_menu_select(menu_id, arg) self._selenium_instance.wait_until_page_contains_element('xpath=//div[contains(@class, "x-menu-item-active")]/a[text()="{0}"]'.format(prev_menu)) ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.RETURN).perform() def _ensure_menu_selected(self, menu_text, timeout=30): #checks a menu item is selected - private self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() base_time = time.time() menu_selected = False while not menu_selected and (((time.time()-base_time) < timeout)): try: self._selenium_instance.wait_until_page_contains_element('xpath=//a[text()="{0}"]/..[contains(@class, "x-menu-item-active")]'.format(menu_text)) menu_selected = True except: self._selenium_instance._info("RETRYING ELEMENT LOCATION") if not menu_selected: raise AssertionError('Menu item "{0}" not selected'.format(menu_text)) def select_new_entity_type(self, creation_type_text, entity_type): """ Selects a new entity type from the E-WorkBook Web popup menu | Open Tile Header Tool Menu | Administrators | | Select New Entity Type | Create New | Project | """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() self._menu_select('ewb-popup-menu', creation_type_text) ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_RIGHT).perform() if creation_type_text=='Use Record to Create': self._sub_menu_select('ewb-popup-menu-submenu-ewb-entity-menu-template', entity_type) else: self._sub_menu_select('ewb-popup-menu-submenu-ewb-entity-menu-new', entity_type) ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.RETURN).perform() def _menu_select(self, menu_id, menu_option_text, load_timeout=30): """ Selects a menu option """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() row_number = 0 number_of_rows = 0 base_time = time.time() while ((number_of_rows == 0) and ((time.time()-base_time) < load_timeout)): number_of_rows = self._selenium_instance.get_matching_xpath_count('//*[@id="{0}"]/div/div'.format(menu_id)) time.sleep(1) self._selenium_instance._info("Number of menu elements = '%s'" % number_of_rows) for index in range(1, int(number_of_rows)+1): try: if self._selenium_instance.get_text('xpath=//*[@id="{0}"]/div/div[{1}]/a'.format(menu_id, index)) == menu_option_text: row_number= index self._selenium_instance._info("Row number = '%s'" % row_number) except ValueError: pass self._selenium_instance._info("Row number = '%s'" % row_number) for i in range(int(row_number)): ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_DOWN).perform() def _sub_menu_select(self, menu_id, menu_option_text, load_timeout=30): """ Selects a sub-menu option SJ - Is this really needed? A sub-menu is just another menu... """ self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() row_number = 0 number_of_rows = 0 number_of_unselectable_rows = 0 base_time = time.time() while ((number_of_rows == 0) and ((time.time()-base_time) < load_timeout)): self._selenium_instance._info("Counting number of menu elements") number_of_rows = int(self._selenium_instance.get_matching_xpath_count('//*[@id="{0}"]/div/div'.format(menu_id))) number_of_selectable_rows = self._selenium_instance.get_matching_xpath_count('//*[@id="{0}"]/div/div/a'.format(menu_id)) number_of_unselectable_rows = int(number_of_rows) - int(number_of_selectable_rows) time.sleep(1) self._selenium_instance._info("Number of menu elements = '%s'" % number_of_rows) for index in range(1, int(number_of_rows)+1): try: if self._selenium_instance.get_text('xpath=//*[@id="{0}"]/div/div[{1}]'.format(menu_id, index)) == menu_option_text: row_number= index self._selenium_instance._info("Row number = '%s'" % row_number) except ValueError: pass row_number = row_number - number_of_unselectable_rows self._selenium_instance._info("Row number = '%s'" % row_number) for i in range(int(row_number)-1): ActionChains(self._selenium_instance._current_browser()).send_keys(Keys.ARROW_DOWN).perform() def menu_click(self, menu_locator, menu_option): self._selenium_instance = SeleniumInstanceHelper()._get_selenium_instance() menu_elem = self._selenium_instance._element_find(menu_locator, True, True) self._selenium_instance._info("Opening Menu '%s'" % menu_locator) ActionChains(self._selenium_instance._current_browser()).move_to_element(menu_elem).perform() self._selenium_instance._info("Selecting Menu Option '%s'" % menu_option) ActionChains(self._selenium_instance._current_browser()).click(self._selenium_instance._element_find(menu_option, True, True)).perform()
class DownloadHelperFunctions: def __init__(self): self._selenium_instance = None def download_file_from_EWB(self, url, filename, cookie='JSESSIONID'): """ Downloads the file from the given url to the given filename location. *Arguments* - _url_ - the file download url - _filename_ - the name and path of the output file - _cookie_ - the session cookie name, defaults to JSESSIONID *Example* | Download File From EWB | https://localhost:8443/my/download/url | ${OUTPUT_DIR}/downloaded_file.txt | """ self._selenium_instance = SeleniumInstanceHelper( )._get_selenium_instance() print(self._selenium_instance._current_browser().get_cookies()) cookie_value = self._selenium_instance._current_browser().get_cookie( cookie)['value'] cookie_string = '{0}={1}'.format(cookie, cookie_value) cookie_header = {'Cookie': cookie_string} print cookie_header req = urllib2.Request(url, headers=cookie_header) response = urllib2.urlopen(req) file_content = response.read() directory = os.path.dirname(filename) if not os.path.exists(directory): os.makedirs(directory) file = open(filename, 'wb') file.write(file_content) file.close() def download_spreadsheet_audit_log(self, entity_id, filename, server, port=8443, http_scheme='https', username='******', password='******'): """ Downloads the audit log for the spreadsheet given by the entity_version_id to the given filename location. The audit log is downloaded as a CSV by default *Arguments* - _entity_id_ - the entity ID for the spreadsheet containing the audit log - _file_name_ - the full path to use as the audit log output location - _server_ - the EWB application server name/IP - _port=8443_ - the port on which the EWB application server is running, defaults to 8443 - _http_scheme=https_ - the scheme (http/https) the EWB application server is using, defaults to https - _username=Administrator_ - the user to connect as, defaults to Administrator - _password=Administrator_ - the password for the user to connect as, defaults to Administrator *Example* | Download Spreadsheet Audit Log | 12345 |${OUTPUT_DIR}/audit_log.csv | VPCS-EWB24 | """ basic_auth = base64.b64encode('{0}:{1}'.format(username, password)) audit_id = self._get_child_audit_id(server, port, http_scheme, entity_id, basic_auth) v_id = self._get_latest_version_id(server, port, http_scheme, audit_id, basic_auth) url = '{0}://{1}:{2}/ewb/services/1.0/entities/{3}/data?entityVersionId={4}'.format( http_scheme, server, port, audit_id, v_id) self._download_to_file(url, filename, basic_auth) def _get_child_audit_id(self, server, port, scheme, entity_id, basic_auth): url = '{0}://{1}:{2}/ewb/services/1.0/entities/{3}?includeChildren=true'.format( scheme, server, port, entity_id) resp = self._perform_get(url, basic_auth, {'accept': 'application/json'}) return jsonpointer.resolve_pointer( json.loads(resp), '/children/entity/0/entityCore/entityId') def _get_latest_version_id(self, server, port, scheme, entity_id, basic_auth): url = '{0}://{1}:{2}/ewb/services/1.0/entities/{3}?includeVersionInfo=true'.format( scheme, server, port, entity_id) resp = self._perform_get(url, basic_auth, {'accept': 'application/json'}) return jsonpointer.resolve_pointer(json.loads(resp), '/versionInfo/versionId') def _perform_get(self, url, basic_auth=None, additional_headers={}): req = urllib2.Request(url) if basic_auth: req.add_header("Authorization", "Basic %s" % basic_auth) for header in additional_headers: req.add_header(header, additional_headers[header]) try: # wrap in try-except since this will fail on 2.7.8 and below import ssl gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) response = urllib2.urlopen(req, context=gcontext) except AttributeError: # 2.7.8 and below did not check cert integrity so continue with default context response = urllib2.urlopen(req) return response.read() def _download_to_file(self, url, filename, basic_auth=None): file_content = self._perform_get(url, basic_auth) directory = os.path.dirname(filename) if not os.path.exists(directory): os.makedirs(directory) file = open(filename, 'wb') file.write(file_content) file.close()