def enable_columns_to_filter(self, session): column_list = [filter1.field_name for filter1 in self.filters] if not len(column_list): return SAPGuiElements.call_menu(session, MENU_FIELDS_FOR_SELECTION) max_scroll = int(SAPGuiElements.get_max_scroll_position(session, GUI_CHILD_USER_AREA1)) pos_scroll = 0 startpos = 5 do_cycle = True while do_cycle: SAPGuiElements.set_scroll_position(session, pos_scroll, GUI_CHILD_USER_AREA1) max_i = 0 for i, element in SAPGuiElements.iter_elements_by_template(session, GUI_CHILD_USER_AREA1, "wnd[1]/usr/lbl[4,{0}]", startpos): max_i = i if element.text in column_list: SAPGuiElements.set_checkbox(session, "wnd[1]/usr/chk[2,{0}]".format(i)) if pos_scroll < max_scroll: new_pos_scroll = min(pos_scroll + max_i, max_scroll) startpos = max_i - (new_pos_scroll - pos_scroll) + 1 pos_scroll = new_pos_scroll else: do_cycle = False SAPGuiElements.press_keyboard_keys(session, "Enter", GUI_CHILD_WINDOW1)
def create_new_session(sid, user, pwd, client=None, langu=None, close_conn=False, change_pwd=True): conn_descr = SAPLogonINI.get_connect_name_by_sid(sid) if not conn_descr: raise ValueError("Connection to '{0}' not found".format(sid)) sap_session = SAPNewSession.get_new_session_by_name(conn_descr) if sap_session: if user: SAPGuiElements.set_text(sap_session, USERNAME_FIELD, user) if pwd: SAPGuiElements.set_text(sap_session, PASSWORD_FIELD, pwd) if client: SAPGuiElements.set_text(sap_session, CLIENT_FIELD, client) if langu: SAPGuiElements.set_text(sap_session, LANGUAGE_FIELD, langu) SAPGuiElements.press_keyboard_keys(sap_session, "Enter") msg = SAPGuiElements.get_status_message(sap_session) if msg and msg[0] == "E": SAPGuiElements.press_keyboard_keys(sap_session, "Shift+F3") raise RuntimeError("Could not log to the SAP server. {0}".format(msg[2])) SAPNewSession.is_already_logon_window(sap_session) return_pwd = SAPLogonPwd.is_change_password_window_while_connect(sap_session, change_pwd=change_pwd) SAPNewSession.close_new_windows_while_connect(sap_session) if close_conn: SAPExistedSession.close_session(sap_session) return return_pwd
def __set_auth_filter(session, rsusr002_filter): SAPGuiElements.select_element(session, PRIVILEGE_TAB) for i, auth in enumerate(rsusr002_filter.auth_objects): if i >= MAX_AUTH_OBJECTS: break SAPGuiElements.try_to_set_text( session, AUTH_OBJECT_TEMPLATE.format(type="{type}", num=i + 1), auth.name) SAPGuiElements.press_keyboard_keys(session, "Enter") for k in range(0, 10): try: text = SAPGuiElements.get_text( session, FIELD_NAME_TEMPLATE.format(num=i + 1, num1=k)) except AttributeError: break else: values = auth.get_values_for_field(text) if not values or len(values) == 0: continue for j, value in enumerate(values): if j >= MAX_AUTH_VALUES: break SAPGuiElements.try_to_set_text( session, AUTH_VALUES_TEMPLATE.format(type="{type}", num=i + 1, num1=k, num2=j + 1), value)
def is_already_logon_window(sap_session): try: select = SAPGuiElements.get_element(sap_session, MULTI_USER_DISCONNECT) except AttributeError: pass else: SAPGuiElements.select_element(sap_session, select.id) SAPGuiElements.press_keyboard_keys(sap_session, "Enter", pysapgui.sapgui.GUI_CHILD_WINDOW1)
def close_new_windows_while_connect(sap_session): max_windows = 10 try: for i in range(0, max_windows): SAPGuiElements.get_element(sap_session, pysapgui.sapgui.GUI_CHILD_WINDOW1) SAPGuiElements.press_keyboard_keys(sap_session, "Enter", pysapgui.sapgui.GUI_CHILD_WINDOW1) except AttributeError: pass
def __set_table_name(self, session): SAPGuiElements.set_text(session, TABLENAME_FIELD, self.table_name) SAPGuiElements.press_keyboard_keys(session, "Enter") gui_msg = SAPGuiElements.get_status_message(session) if gui_msg: if gui_msg[1] == "402": msg = "Table '{0}' not found. GUI Message: {1}".format(self.table_name, gui_msg) raise ValueError(msg) elif gui_msg[1] == "419": msg = "Not authorized to view the '{0}' table. GUI Message: {1}".format(self.table_name, gui_msg[2]) raise PermissionError(msg)
def save_to_file(self, sap_session=None): if not sap_session: sap_session = self.sap_session SAPGuiElements.press_keyboard_keys(sap_session, "Ctrl+Shift+F9") if self.file_format == 'html': SAPGuiElements.select_element(sap_session, HTML_FORMAT) filename = self.__get_filename() SAPGuiElements.press_button(sap_session, CONFIRM_FORMAT_BUTTON) SAPGuiElements.set_text(sap_session, PATH_TEXT_FIELD, self.report_dir) SAPGuiElements.set_text(sap_session, FILENAME_TEXT_FIELD, filename) SAPGuiElements.press_button(sap_session, REPLACE_BUTTON) self.__del_gif_files() return filename
def __set_report_and_execute(self, sap_session): SAPGuiElements.set_text(sap_session, PROGRAM_FIELD, self.report_name) SAPGuiElements.press_keyboard_keys(sap_session, "F8") gui_msg = SAPGuiElements.get_status_message(sap_session) if gui_msg: if gui_msg[1] == "017": msg = "Wrong report name '{0}'. GUI Message: {1}".format(self.report_name, gui_msg[2]) raise ValueError(msg) elif gui_msg[1] == "322": msg = "Not authorized to execute '{0}' report. GUI Message: {1}".format(self.report_name, gui_msg[2]) raise PermissionError(msg)
def is_change_password_window_while_connect(sap_session, change_pwd=True): try: SAPGuiElements.get_element(sap_session, CHANGE_PWD_FIELD1) SAPGuiElements.get_element(sap_session, CHANGE_PWD_FIELD2) except AttributeError: # All is Ok. No need to change password pass else: if change_pwd: SAPLogonPwd.__change_password_login(sap_session) else: SAPGuiElements.press_keyboard_keys( sap_session, "F12", pysapgui.sapgui.GUI_CHILD_WINDOW1) raise RuntimeError('Change password to the SAP user')
def __execute_and_return_entries_number(self, session): SAPGuiElements.press_keyboard_keys(session, "F8") gui_msg = SAPGuiElements.get_status_message(session) if gui_msg and gui_msg[1] == "485": msg = "Not authorized to analyze privileges. GUI Message: {0}".format( gui_msg[2]) raise PermissionError(msg) elif gui_msg and gui_msg[1] == "265": return 0 elif hasattr(self, "save_to_file") and self.save_to_file: filename = self.save(session) self.comment = '=HYPERLINK("{0}", "more details")'.format(filename) grid = SAPAlvGrid(session, REPORT_GRID) grid_num = grid.get_row_count() return grid_num
def __call_transaction(sap_session, transaction, window_id=pysapgui.sapgui.GUI_MAIN_WINDOW, check_error=True): tcode = TRANSACTION_PREFIX + transaction SAPGuiElements.set_text(sap_session, TRANSACTION_TEXT_FIELD.format(window=window_id), tcode) SAPGuiElements.press_keyboard_keys(sap_session, "Enter") if not check_error: return gui_msg = SAPGuiElements.get_status_message(sap_session) if gui_msg: if gui_msg[1] == "343": msg = "Wrong transaction name '{0}'. GUI Message: {1}".format(transaction, gui_msg[2]) raise ValueError(msg) elif gui_msg[1] == "077": msg = "Not authorized to execute '{0}' transaction. GUI Message: {1}".format(transaction, gui_msg[2]) raise PermissionError(msg) elif gui_msg[1] == "410": msg = "Blocked action in '{0}' transaction. GUI Message: {1}".format(transaction, gui_msg[2]) raise PermissionError(msg)
def change_password_su3(sap_session, old_pwd): SAPTransaction.call(sap_session, CHANGE_PASSWORD_TRANSACTION) new_pwd = SAPLogonPwd.gen_password() SAPGuiElements.press_button(sap_session, CHNAGE_PWD_BUTTON_SU3) msg = SAPGuiElements.get_status_message(sap_session) if msg: if msg[1] in ('190', '180'): raise RuntimeError("Password could not be changed. {0}".format( msg[2])) try: SAPGuiElements.set_text(sap_session, OLD_PWD_FIELD, old_pwd) SAPGuiElements.set_text(sap_session, NEW_PWD_FIELD1, new_pwd) SAPGuiElements.set_text(sap_session, NEW_PWD_FIELD2, new_pwd) SAPGuiElements.press_keyboard_keys( sap_session, "Enter", pysapgui.sapgui.GUI_CHILD_WINDOW1) except AttributeError as error: raise AttributeError( "Could not find GUI elements to change password. {0}".format( str(error))) try: SAPGuiElements.get_element(sap_session, pysapgui.sapgui.GUI_CHILD_WINDOW2) SAPGuiElements.press_keyboard_keys( sap_session, "Enter", pysapgui.sapgui.GUI_CHILD_WINDOW2) SAPGuiElements.press_keyboard_keys( sap_session, "F12", pysapgui.sapgui.GUI_CHILD_WINDOW1) except AttributeError: # All is Ok. Password changed successfully return new_pwd else: raise AttributeError( "Couldn't set new password. Reconfigure password policy")
def set_filter(self, session): SAPGuiElements.press_keyboard_keys(session, "Shift+F4") if hasattr(self, "exclude_single_values"): if len(self.exclude_single_values): SAPGuiElements.select_element(session, EXCLUDE_VALUES) for i, item in enumerate(self.exclude_single_values): if item == "": SAPGuiElements.press_button(session, EXCLUDE_VALUES_BUTTON.format(i)) SAPGuiElements.press_button(session, OK_BUTTON_FILTER) else: SAPGuiElements.try_to_set_text(session, EXCLUDE_VALUES_TEMPLATE.format(type="{type}", row=i), item) if hasattr(self, "equal_single_values"): if len(self.equal_single_values): SAPGuiElements.select_element(session, INCLUDE_VALUES) for i, item in enumerate(self.equal_single_values): if item == "": SAPGuiElements.press_button(session, INCLUDE_VALUES_BUTTON.format(i)) SAPGuiElements.press_button(session, OK_BUTTON_FILTER) else: SAPGuiElements.try_to_set_text(session, INCLUDE_VALUES_TEMPLATE.format(type="{type}", row=i), item) SAPGuiElements.press_keyboard_keys(session, "F8")
def __change_password_login(sap_session): new_pwd = SAPLogonPwd.gen_password() try: SAPGuiElements.set_text(sap_session, CHANGE_PWD_FIELD1, new_pwd) SAPGuiElements.set_text(sap_session, CHANGE_PWD_FIELD2, new_pwd) SAPGuiElements.press_keyboard_keys( sap_session, "Enter", pysapgui.sapgui.GUI_CHILD_WINDOW1) except AttributeError as error: raise AttributeError( "Could not find GUI elements to change password. {0}".format( str(error))) try: SAPGuiElements.get_element(sap_session, pysapgui.sapgui.GUI_CHILD_WINDOW2) except AttributeError: # All is Ok. Password changed pass else: raise AttributeError( "Couldn't set new password. Reconfigure password policy") return new_pwd
def __set_se16_parameters(session): SAPGuiElements.call_menu(session, MENU_USER_PARAMETERS) SAPGuiElements.select_element(session, FIELD_NAME_SELECTION) SAPGuiElements.select_element(session, ALV_GRID_SELECTION) SAPGuiElements.press_keyboard_keys(session, "Enter", GUI_CHILD_WINDOW1) TCodeSE16.first_call = False
def __set_param_name_and_execute(self, sap_session): SAPGuiElements.set_text(sap_session, PARAM_NAME_FIELD, self.param_name) SAPGuiElements.press_keyboard_keys(sap_session, "F8")
def __get_entries_number(self, session): SAPGuiElements.press_keyboard_keys(session, "Ctrl+F7") entries_num = SAPGuiElements.get_text(session, NUMBER_ENTRIES_FIELD) SAPGuiElements.press_keyboard_keys(session, "Enter", GUI_CHILD_WINDOW1) return self.__parse_val(entries_num)