ITK-dev-shared-components 0.0.4__tar.gz → 0.0.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/gridview_util.py +200 -0
- {ITK_dev_shared_components-0.0.4/src → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components/SAP/multi_session.py +20 -9
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/opret_kundekontakt.py +73 -0
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/sap_login.py +119 -0
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/sap_util.py +35 -0
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP/tree_util.py +71 -0
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/__init__.py +0 -0
- {ITK_dev_shared_components-0.0.4/src → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/PKG-INFO +1 -1
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info/SOURCES.txt +17 -0
- ITK_dev_shared_components-0.0.6/ITK_dev_shared_components.egg-info/requires.txt +2 -0
- {ITK_dev_shared_components-0.0.4 → ITK_dev_shared_components-0.0.6}/PKG-INFO +1 -1
- {ITK_dev_shared_components-0.0.4 → ITK_dev_shared_components-0.0.6}/pyproject.toml +3 -3
- ITK_dev_shared_components-0.0.6/tests/test_gridview_util.py +75 -0
- ITK_dev_shared_components-0.0.4/src/ITK_dev_shared_components/SAP/sap_login.py +0 -53
- ITK_dev_shared_components-0.0.4/src/ITK_dev_shared_components/SAP/sap_login_cli.py +0 -19
- ITK_dev_shared_components-0.0.4/src/ITK_dev_shared_components.egg-info/SOURCES.txt +0 -12
- ITK_dev_shared_components-0.0.4/src/ITK_dev_shared_components.egg-info/requires.txt +0 -1
- {ITK_dev_shared_components-0.0.4/src/ITK_dev_shared_components → ITK_dev_shared_components-0.0.6/ITK_dev_shared_components/SAP}/__init__.py +0 -0
- {ITK_dev_shared_components-0.0.4/src → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/dependency_links.txt +0 -0
- {ITK_dev_shared_components-0.0.4/src → ITK_dev_shared_components-0.0.6}/ITK_dev_shared_components.egg-info/top_level.txt +0 -0
- {ITK_dev_shared_components-0.0.4 → ITK_dev_shared_components-0.0.6}/LICENSE +0 -0
- {ITK_dev_shared_components-0.0.4 → ITK_dev_shared_components-0.0.6}/README.md +0 -0
- {ITK_dev_shared_components-0.0.4 → ITK_dev_shared_components-0.0.6}/setup.cfg +0 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""This module provides static functions to peform common tasks with SAP GuiGridView COM objects."""
|
|
2
|
+
|
|
3
|
+
def scroll_entire_table(grid_view, return_to_top=False) -> None:
|
|
4
|
+
"""This function scrolls through the entire table to load all cells.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
grid_view: A SAP GuiGridView object.
|
|
8
|
+
return_to_top: Whether to return the table to the first row after scrolling. Defaults to False.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
_type_: _description_
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
for i in range(0, grid_view.RowCount, grid_view.VisibleRowCount):
|
|
15
|
+
grid_view.FirstVisibleRow = i
|
|
16
|
+
|
|
17
|
+
if return_to_top:
|
|
18
|
+
grid_view.FirstVisibleRow = 0
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
|
|
22
|
+
"""Get all values from a table and return them in a 2D tuple.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
grid_view: A SAP GuiGridView object.
|
|
26
|
+
pre_load: Whether to first scroll through the table to load all values.
|
|
27
|
+
If a row hasn't been loaded before reading, the row data will be empty.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
tuple[tuple[str]]: A 2D tuple of all cell values in the gridview.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
if pre_load:
|
|
34
|
+
scroll_entire_table(grid_view, True)
|
|
35
|
+
|
|
36
|
+
columns = grid_view.ColumnOrder
|
|
37
|
+
row_count = grid_view.RowCount
|
|
38
|
+
|
|
39
|
+
output = []
|
|
40
|
+
|
|
41
|
+
for r in range(row_count):
|
|
42
|
+
row_data = []
|
|
43
|
+
for c in columns:
|
|
44
|
+
v = grid_view.GetCellValue(r, c)
|
|
45
|
+
row_data.append(v)
|
|
46
|
+
|
|
47
|
+
output.append(tuple(row_data))
|
|
48
|
+
|
|
49
|
+
return tuple(output)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def get_row(grid_view, row:int, scroll_to_row=False) -> tuple[str]:
|
|
53
|
+
"""Returns the data of a single row.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
grid_view: A SAP GuiGridView object.
|
|
57
|
+
row: The zero-based index of the row.
|
|
58
|
+
scroll_to_row: Whether to scroll to the row before reading the data.
|
|
59
|
+
This ensures the data of the row has been loaded before reading.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
tuple[str]: A tuple of the row's data.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
if scroll_to_row:
|
|
66
|
+
grid_view.FirstVisibleRow = row
|
|
67
|
+
|
|
68
|
+
row_data = []
|
|
69
|
+
for c in grid_view.ColumnOrder:
|
|
70
|
+
v = grid_view.GetCellValue(row, c)
|
|
71
|
+
row_data.append(v)
|
|
72
|
+
return tuple(row_data)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def iterate_rows(grid_view) -> tuple[str]:
|
|
76
|
+
"""This generator yields each row of the table in order.
|
|
77
|
+
This is preferable to loading all rows as this will only load a row when it's needed.
|
|
78
|
+
As a side effect this function scrolls the yielded row into view, so the
|
|
79
|
+
table doesn't need to be pre-loaded before calling this function.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
grid_view: A SAP GuiGridView object.
|
|
83
|
+
|
|
84
|
+
Yields:
|
|
85
|
+
tuple[str]: A tuple of the next row's data.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
row = 0
|
|
89
|
+
while row < grid_view.RowCount:
|
|
90
|
+
# Only scroll when row isn't visible
|
|
91
|
+
if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
|
|
92
|
+
grid_view.FirstVisibleRow = row
|
|
93
|
+
|
|
94
|
+
yield get_row(grid_view, row)
|
|
95
|
+
row += 1
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def get_column_titles(grid_view) -> tuple[str]:
|
|
99
|
+
"""Get the column titles of the table instead of the column ids.
|
|
100
|
+
To get the column ids use grid_view.ColumnOrder.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
grid_view: A SAP GuiGridView object.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
tuple[str]: A tuple of the gridview's column titles.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
return tuple(grid_view.GetColumnTitles(c)[0] for c in grid_view.ColumnOrder)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def find_row_index_by_value(grid_view, column:str, value:str) -> int:
|
|
113
|
+
"""Find the index of the first row where the given column's value
|
|
114
|
+
matches the given value. Returns -1 if no row is found.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
grid_view: A SAP GuiGridView object.
|
|
118
|
+
column: The id of the column.
|
|
119
|
+
value: The value to search for.
|
|
120
|
+
|
|
121
|
+
Raises:
|
|
122
|
+
ValueError: If the gridview doesn't contain a column with the given id.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
int: The index of the first row which column value matches the given value.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
if column not in grid_view.ColumnOrder:
|
|
129
|
+
raise ValueError(f"Column '{column}' not in grid_view")
|
|
130
|
+
|
|
131
|
+
for row in range(grid_view.RowCount):
|
|
132
|
+
# Only scroll when row isn't visible
|
|
133
|
+
if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
|
|
134
|
+
grid_view.FirstVisibleRow = row
|
|
135
|
+
|
|
136
|
+
if grid_view.GetCellValue(row, column) == value:
|
|
137
|
+
return row
|
|
138
|
+
|
|
139
|
+
return -1
|
|
140
|
+
|
|
141
|
+
def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int]:
|
|
142
|
+
"""Find all indecies of the rows where the given column's value
|
|
143
|
+
match the given value. Returns an empty list if no row is found.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
grid_view (_type_): A SAP GuiGridView object.
|
|
147
|
+
column (str): The name of the column whose value to check.
|
|
148
|
+
value (str): The value to search for.
|
|
149
|
+
|
|
150
|
+
Raises:
|
|
151
|
+
ValueError: If the column name doesn't exist in the grid view.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
list[int]: A list of row indecies where the value matches.
|
|
155
|
+
"""
|
|
156
|
+
if column not in grid_view.ColumnOrder:
|
|
157
|
+
raise ValueError(f"Column '{column}' not in grid_view")
|
|
158
|
+
|
|
159
|
+
rows = []
|
|
160
|
+
|
|
161
|
+
for row in range(grid_view.RowCount):
|
|
162
|
+
# Only scroll when row isn't visible
|
|
163
|
+
if not grid_view.FirstVisibleRow <= row <= grid_view.FirstVisibleRow + grid_view.VisibleRowCount-1:
|
|
164
|
+
grid_view.FirstVisibleRow = row
|
|
165
|
+
|
|
166
|
+
if grid_view.GetCellValue(row, column) == value:
|
|
167
|
+
rows.append(row)
|
|
168
|
+
|
|
169
|
+
return rows
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
if __name__=='__main__':
|
|
175
|
+
import win32com.client
|
|
176
|
+
|
|
177
|
+
SAP = win32com.client.GetObject("SAPGUI")
|
|
178
|
+
app = SAP.GetScriptingEngine
|
|
179
|
+
connection = app.Connections(0)
|
|
180
|
+
session = connection.Sessions(0)
|
|
181
|
+
|
|
182
|
+
table = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
|
|
183
|
+
|
|
184
|
+
rows = find_all_row_indecies_by_value(table, "ZZ_PARTNER", '15879880')
|
|
185
|
+
print(rows)
|
|
186
|
+
table.setCurrentCell(rows[0], "ZZ_PARTNER")
|
|
187
|
+
|
|
188
|
+
# print(get_row(table, 1, True))
|
|
189
|
+
|
|
190
|
+
# scroll_entire_table(table)
|
|
191
|
+
|
|
192
|
+
# data = get_all_rows(table)
|
|
193
|
+
# print(len(data), len(data[0]))
|
|
194
|
+
|
|
195
|
+
# for r in iterate_rows(table):
|
|
196
|
+
# print(r)
|
|
197
|
+
|
|
198
|
+
# print(get_column_titles(table))
|
|
199
|
+
|
|
200
|
+
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"""This module provides static function to handle multiple sessions of SAP.
|
|
2
|
+
Using this module you can spawn multiple sessions and automatically execute
|
|
3
|
+
a function in parallel on the sessions."""
|
|
4
|
+
|
|
4
5
|
import time
|
|
5
6
|
import threading
|
|
6
7
|
from typing import Callable
|
|
8
|
+
import pythoncom
|
|
9
|
+
import win32com.client
|
|
10
|
+
import win32gui
|
|
7
11
|
|
|
8
12
|
def run_with_session(session_index:int, func:Callable, args:tuple) -> None:
|
|
9
13
|
"""Run a function in a sepcific session based on the sessions index.
|
|
10
|
-
|
|
11
14
|
This function is meant to be run inside a seperate thread.
|
|
12
15
|
The function must take a session object as its first argument.
|
|
13
16
|
Note that this function will not spawn the sessions before running,
|
|
@@ -27,7 +30,6 @@ def run_with_session(session_index:int, func:Callable, args:tuple) -> None:
|
|
|
27
30
|
|
|
28
31
|
def run_batch(func:Callable, args:tuple[tuple], num_sessions=6) -> None:
|
|
29
32
|
"""Run a function in parallel sessions.
|
|
30
|
-
|
|
31
33
|
The function will be run {num_sessions} times with args[i] as input.
|
|
32
34
|
The function must take a session object as its first argument.
|
|
33
35
|
Note that this function will not spawn the sessions before running,
|
|
@@ -49,7 +51,6 @@ def run_batch(func:Callable, args:tuple[tuple], num_sessions=6) -> None:
|
|
|
49
51
|
|
|
50
52
|
def run_batches(func:Callable, args:tuple[tuple], num_sessions=6):
|
|
51
53
|
"""Run a function in parallel batches.
|
|
52
|
-
|
|
53
54
|
This function runs the input function for each set of arguments in args.
|
|
54
55
|
The function will be run in parallel batches of size {num_sessions}.
|
|
55
56
|
The function must take a session object as its first argument.
|
|
@@ -63,7 +64,6 @@ def run_batches(func:Callable, args:tuple[tuple], num_sessions=6):
|
|
|
63
64
|
|
|
64
65
|
def spawn_sessions(num_sessions=6) -> list:
|
|
65
66
|
"""A function to spawn multiple sessions of SAP.
|
|
66
|
-
|
|
67
67
|
This function will attempt to spawn the desired number of sessions.
|
|
68
68
|
If the current number of open sessions exceeds the desired number of sessions
|
|
69
69
|
the already open sessions will not be closed to match the desired number.
|
|
@@ -114,16 +114,27 @@ def spawn_sessions(num_sessions=6) -> list:
|
|
|
114
114
|
|
|
115
115
|
return sessions
|
|
116
116
|
|
|
117
|
+
def get_all_SAP_sessions() -> tuple:
|
|
118
|
+
"""Returns a tuple of all open SAP sessions (on connection index 0).
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
tuple: A tuple of SAP GuiSession objects.
|
|
122
|
+
"""
|
|
123
|
+
SAP = win32com.client.GetObject("SAPGUI")
|
|
124
|
+
app = SAP.GetScriptingEngine
|
|
125
|
+
connection = app.Connections(0)
|
|
126
|
+
return tuple(connection.Sessions)
|
|
127
|
+
|
|
117
128
|
class ExThread(threading.Thread):
|
|
118
129
|
"""A thread with a handle to get an exception raised inside the thread: ExThread.error"""
|
|
119
130
|
def __init__(self, *args, **kwargs):
|
|
120
|
-
super(
|
|
131
|
+
super().__init__(*args, **kwargs)
|
|
121
132
|
self.error = None
|
|
122
133
|
|
|
123
134
|
def run(self):
|
|
124
135
|
try:
|
|
125
136
|
self._target(*self._args, **self._kwargs)
|
|
126
|
-
except Exception as e:
|
|
137
|
+
except Exception as e: # pylint: disable=broad-exception-caught
|
|
127
138
|
self.error = e
|
|
128
139
|
|
|
129
140
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""This module provides a single function to conviniently peform the action 'opret-kundekontaker' in SAP."""
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
import win32clipboard
|
|
5
|
+
from ITK_dev_shared_components.SAP import tree_util
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def opret_kundekontakter(session, fp:str, aftaler:list[str] | None,
|
|
9
|
+
art:Literal[' ', 'Automatisk', 'Fakturagrundlag', 'Fuldmagt ifm. værge', 'Konverteret', 'Myndighedshenvend.', 'Orientering', 'Returpost', 'Ringeaktivitet', 'Skriftlig henvend.', 'Telefonisk henvend.'],
|
|
10
|
+
notat:str, lock=None) -> None:
|
|
11
|
+
"""Creates a kundekontakt on the given FP and aftaler.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
session (COM Object): The SAP session to peform the action.
|
|
15
|
+
fp (str): The forretningspartner number.
|
|
16
|
+
aftaler (list[str] | None): A list of aftaler to put the kundekontakt on. If empty or None the kundekontakt will be created on fp-level.
|
|
17
|
+
art (str): The art of the kundekontakt.
|
|
18
|
+
notat (str): The text of the kundekontakt.
|
|
19
|
+
lock (threading.Lock, optional): A threading.Lock object to allow this function to run properly when multithreaded. Defaults to None.
|
|
20
|
+
"""
|
|
21
|
+
session.StartTransaction('fmcacov')
|
|
22
|
+
|
|
23
|
+
# Find forretningspartner
|
|
24
|
+
session.findById("wnd[0]/usr/ctxtGPART_DYN").text = fp
|
|
25
|
+
session.findById("wnd[0]").sendVKey(0)
|
|
26
|
+
|
|
27
|
+
# Click 'Opret kundekontakt-flere
|
|
28
|
+
session.findById("wnd[0]/shellcont/shell").nodeContextMenu("GP0000000001")
|
|
29
|
+
session.findById("wnd[0]/shellcont/shell").selectContextMenuItem("FLERE")
|
|
30
|
+
|
|
31
|
+
# Vælg aftaler
|
|
32
|
+
if aftaler:
|
|
33
|
+
aftale_tree = session.findById("wnd[1]/usr/cntlCONTAINER_PSOBKEY/shellcont/shell/shellcont[1]/shell[1]")
|
|
34
|
+
for aftale in aftaler:
|
|
35
|
+
key, name = tree_util.get_item_by_text(aftale_tree, aftale, True)
|
|
36
|
+
aftale_tree.ChangeCheckBox(key, name, True)
|
|
37
|
+
session.findById("wnd[1]/usr/cntlCONTAINER_PSOBKEY/shellcont/shell/shellcont[1]/shell[0]").pressButton("OK")
|
|
38
|
+
|
|
39
|
+
# Set art
|
|
40
|
+
session.findById("wnd[0]/usr/tabsTSTRIP/tabpT_CA/ssubSUBSR_TSTRIP:SAPLBPT1:0510/cmbBCONTD-CTYPE").Value = art
|
|
41
|
+
|
|
42
|
+
# Go to editor and paste (lock if multithreaded)
|
|
43
|
+
session.findById("wnd[0]/usr/subNOTICE:SAPLEENO:1002/btnEENO_TEXTE-EDITOR").press()
|
|
44
|
+
if lock:
|
|
45
|
+
lock.acquire()
|
|
46
|
+
_setClipboard(notat)
|
|
47
|
+
session.findById("wnd[0]/tbar[1]/btn[9]").press()
|
|
48
|
+
if lock:
|
|
49
|
+
lock.release()
|
|
50
|
+
|
|
51
|
+
# Back and save
|
|
52
|
+
session.findById("wnd[0]/tbar[0]/btn[3]").press()
|
|
53
|
+
session.findById("wnd[0]/tbar[0]/btn[11]").press()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _setClipboard(text:str) -> None:
|
|
57
|
+
win32clipboard.OpenClipboard()
|
|
58
|
+
win32clipboard.EmptyClipboard()
|
|
59
|
+
win32clipboard.SetClipboardText(text)
|
|
60
|
+
win32clipboard.CloseClipboard()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
if __name__ == '__main__':
|
|
64
|
+
from ITK_dev_shared_components.SAP import multi_session
|
|
65
|
+
from datetime import datetime
|
|
66
|
+
|
|
67
|
+
session = multi_session.spawn_sessions(1)[0]
|
|
68
|
+
fp = '25564617'
|
|
69
|
+
aftaler = ['2291987', '2421562', '2311094']
|
|
70
|
+
art = 'Orientering'
|
|
71
|
+
notat = 'Test '+ str(datetime.now())
|
|
72
|
+
|
|
73
|
+
opret_kundekontakter(session, fp, aftaler, 'Automatisk', notat)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""This module provides a functions to open SAP GUI."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import pathlib
|
|
5
|
+
import subprocess
|
|
6
|
+
import time
|
|
7
|
+
from selenium import webdriver
|
|
8
|
+
from selenium.webdriver.common.by import By
|
|
9
|
+
import pywintypes
|
|
10
|
+
from ITK_dev_shared_components.SAP import multi_session
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def login_using_portal(username:str, password:str):
|
|
14
|
+
"""Open KMD Portal in Edge, login and start SAP GUI.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
user: KMD Portal username.
|
|
18
|
+
password: KMD Portal password.
|
|
19
|
+
"""
|
|
20
|
+
driver = webdriver.Chrome()
|
|
21
|
+
driver.implicitly_wait(10)
|
|
22
|
+
driver.get('https://portal.kmd.dk/irj/portal')
|
|
23
|
+
driver.maximize_window()
|
|
24
|
+
|
|
25
|
+
#Login
|
|
26
|
+
user_field = driver.find_element(By.ID, 'logonuidfield')
|
|
27
|
+
pass_field = driver.find_element(By.ID, 'logonpassfield')
|
|
28
|
+
login_button = driver.find_element(By.ID, 'buttonLogon')
|
|
29
|
+
|
|
30
|
+
user_field.clear()
|
|
31
|
+
user_field.send_keys(username)
|
|
32
|
+
|
|
33
|
+
pass_field.clear()
|
|
34
|
+
pass_field.send_keys(password)
|
|
35
|
+
|
|
36
|
+
login_button.click()
|
|
37
|
+
|
|
38
|
+
#Opus
|
|
39
|
+
mine_genveje = driver.find_element(By.CSS_SELECTOR, "div[title='Mine Genveje']")
|
|
40
|
+
mine_genveje.click()
|
|
41
|
+
|
|
42
|
+
#Wait for download and launch file
|
|
43
|
+
_wait_for_download()
|
|
44
|
+
|
|
45
|
+
driver.quit()
|
|
46
|
+
|
|
47
|
+
_wait_for_sap_to_open()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _wait_for_download():
|
|
51
|
+
"""Private function that checks if the SAP.erp file has been downloaded.
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
TimeoutError: If the file hasn't been downloaded within 5 seconds.
|
|
55
|
+
"""
|
|
56
|
+
downloads_folder = str(pathlib.Path.home() / "Downloads")
|
|
57
|
+
for _ in range(10):
|
|
58
|
+
for file in os.listdir(downloads_folder):
|
|
59
|
+
if file.endswith(".sap"):
|
|
60
|
+
path = os.path.join(downloads_folder, file)
|
|
61
|
+
os.startfile(path)
|
|
62
|
+
return
|
|
63
|
+
|
|
64
|
+
time.sleep(0.5)
|
|
65
|
+
raise TimeoutError(f".SAP file not found in {downloads_folder}")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def login_using_cli(username: str, password: str, client='751', system='P02') -> None:
|
|
69
|
+
"""Open and login to SAP with commandline expressions.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
username (str): AZ username
|
|
73
|
+
password (str): password
|
|
74
|
+
client (str, optional): Kommune ID (Aarhus = 751). Defaults to '751'.
|
|
75
|
+
system (str, optional): Environment SID (e.g. P02 = 'KMD OPUS Produktion [P02]'). Defaults to 'P02'.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
command_args = [
|
|
79
|
+
r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe",
|
|
80
|
+
f"-system={system}",
|
|
81
|
+
f"-client={client}",
|
|
82
|
+
f"-user={username}",
|
|
83
|
+
f"-pw={password}"
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
subprocess.run(command_args, check=False)
|
|
87
|
+
|
|
88
|
+
_wait_for_sap_to_open()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _wait_for_sap_to_open() -> None:
|
|
92
|
+
"""Check every second for 10 seconds if the SAP Gui scripting engine is available.
|
|
93
|
+
|
|
94
|
+
Raises:
|
|
95
|
+
TimeoutError: If SAP doesn't start within 10 seconds.
|
|
96
|
+
"""
|
|
97
|
+
for _ in range(10):
|
|
98
|
+
time.sleep(1)
|
|
99
|
+
try:
|
|
100
|
+
sessions = multi_session.get_all_SAP_sessions()
|
|
101
|
+
if len(sessions) > 0:
|
|
102
|
+
return
|
|
103
|
+
except pywintypes.com_error:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
raise TimeoutError("SAP didn't respond within 10 seconds.")
|
|
107
|
+
|
|
108
|
+
def kill_sap():
|
|
109
|
+
"""Kills all SAP processes currently running."""
|
|
110
|
+
os.system("taskkill /F /IM saplogon.exe")
|
|
111
|
+
|
|
112
|
+
if __name__=="__main__":
|
|
113
|
+
# user = "az12345"
|
|
114
|
+
# password = "Hunter2"
|
|
115
|
+
# login_using_portal(user, password)
|
|
116
|
+
# login_using_cli(user, password)
|
|
117
|
+
kill_sap()
|
|
118
|
+
|
|
119
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""This module provides miscellaneous static functions to peform common tasks in SAP."""
|
|
2
|
+
|
|
3
|
+
def print_all_descendants(container, max_depth=-1, indent=0):
|
|
4
|
+
"""Prints the object and all of its decendants recursivly
|
|
5
|
+
to the console.
|
|
6
|
+
|
|
7
|
+
Args:
|
|
8
|
+
container: A SAP GuiContainer object.
|
|
9
|
+
max_depth: The maximum depth of the recursive search. Defaults to -1.
|
|
10
|
+
indent: The indentation level of the printed text.
|
|
11
|
+
This increases for each level of the recursion. Defaults to 0.
|
|
12
|
+
"""
|
|
13
|
+
indent_text = ' |'*indent
|
|
14
|
+
|
|
15
|
+
for child in container.Children:
|
|
16
|
+
print(indent_text, child.Type)
|
|
17
|
+
print(indent_text, child.Name)
|
|
18
|
+
print(indent_text, child.Id)
|
|
19
|
+
|
|
20
|
+
if (hasattr(child, 'Children') and child.Children is not None
|
|
21
|
+
and len(child.Children) != 0
|
|
22
|
+
and (indent < max_depth or max_depth == -1)):
|
|
23
|
+
print_all_descendants(child, max_depth, indent+1)
|
|
24
|
+
else:
|
|
25
|
+
print(indent_text)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __name__=='__main__':
|
|
29
|
+
from ITK_dev_shared_components.SAP import multi_session
|
|
30
|
+
|
|
31
|
+
session = multi_session.spawn_sessions(1)[0]
|
|
32
|
+
|
|
33
|
+
usr = session.FindById('/app/con[0]/ses[0]/wnd[0]/usr')
|
|
34
|
+
|
|
35
|
+
print_all_descendants(usr)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""This module provides static functions to peform common tasks with SAP GuiTree COM objects."""
|
|
2
|
+
|
|
3
|
+
def get_node_key_by_text(tree, text: str, fuzzy=False) -> str:
|
|
4
|
+
"""Get the node key of a node based on its text.
|
|
5
|
+
tree: A SAP GuiTree object.
|
|
6
|
+
text: The text to search for.
|
|
7
|
+
fuzzy: Whether to check if the node text just contains the search text.
|
|
8
|
+
"""
|
|
9
|
+
for key in tree.GetAllNodeKeys():
|
|
10
|
+
t = tree.GetNodeTextByKey(key)
|
|
11
|
+
|
|
12
|
+
if t == text or (fuzzy and text in t):
|
|
13
|
+
return key
|
|
14
|
+
|
|
15
|
+
raise ValueError(f"No node with the text '{text}' was found.")
|
|
16
|
+
|
|
17
|
+
def get_item_by_text(tree, text: str, fuzzy=False) -> tuple[str,str]:
|
|
18
|
+
"""Get the node key and item name of an item based on its text.
|
|
19
|
+
tree: A SAP GuiTree object.
|
|
20
|
+
text: The text to search for.
|
|
21
|
+
fuzzy: Whether to check if the item text just contains the search text.
|
|
22
|
+
"""
|
|
23
|
+
for key in tree.GetAllNodeKeys():
|
|
24
|
+
for name in tree.GetColumnNames():
|
|
25
|
+
t = tree.GetItemText(key, name)
|
|
26
|
+
|
|
27
|
+
if t == text or (fuzzy and text in t):
|
|
28
|
+
return (key, name)
|
|
29
|
+
|
|
30
|
+
raise ValueError(f"No item with the text '{text}' was found.")
|
|
31
|
+
|
|
32
|
+
def check_all_check_boxes(tree) -> None:
|
|
33
|
+
"""Find and check all checkboxes in the tree.
|
|
34
|
+
tree: A SAP GuiTree object.
|
|
35
|
+
"""
|
|
36
|
+
for key in tree.GetAllNodeKeys():
|
|
37
|
+
for name in tree.GetColumnNames():
|
|
38
|
+
if tree.GetItemType(key, name) == 3:
|
|
39
|
+
tree.ChangeCheckBox(key, name, True)
|
|
40
|
+
|
|
41
|
+
def uncheck_all_check_boxes(tree) -> None:
|
|
42
|
+
"""Find and uncheck all checkboxes in the tree.
|
|
43
|
+
tree: A SAP GuiTree object.
|
|
44
|
+
"""
|
|
45
|
+
for key in tree.GetAllNodeKeys():
|
|
46
|
+
for name in tree.GetColumnNames():
|
|
47
|
+
if tree.GetItemType(key, name) == 3:
|
|
48
|
+
tree.ChangeCheckBox(key, name, False)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == '__main__':
|
|
54
|
+
from ITK_dev_shared_components.SAP import multi_session
|
|
55
|
+
|
|
56
|
+
session = multi_session.spawn_sessions(1)[0]
|
|
57
|
+
|
|
58
|
+
tree = session.findById('/app/con[0]/ses[0]/wnd[1]/usr/cntlCONTAINER_PSOBKEY/shellcont/shell/shellcont[1]/shell[1]')
|
|
59
|
+
|
|
60
|
+
# print([tree.GetNodeTextByKey(key) for key in tree.GetAllNodeKeys()])
|
|
61
|
+
# print([tree.GetItemText(key, '&Hierarchy') for key in tree.GetAllNodeKeys()])
|
|
62
|
+
|
|
63
|
+
# print(list(tree.GetColumnNames()))
|
|
64
|
+
|
|
65
|
+
# print(tree.GetItemText(' 2', '&Hierarchy'))
|
|
66
|
+
|
|
67
|
+
# key, name = get_item_by_text(tree, '2291987', True)
|
|
68
|
+
# tree.ChangeCheckBox(key, name, True)
|
|
69
|
+
|
|
70
|
+
check_all_check_boxes(tree)
|
|
71
|
+
uncheck_all_check_boxes(tree)
|
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: ITK-dev-shared-components
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
4
4
|
Summary: Shared components to use in RPA projects
|
|
5
5
|
Author-email: ITK Development <itk-rpa@mkb.aarhus.dk>
|
|
6
6
|
Project-URL: Homepage, https://github.com/itk-dev-rpa/itk-dev-shared-components
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
ITK_dev_shared_components/__init__.py
|
|
5
|
+
ITK_dev_shared_components.egg-info/PKG-INFO
|
|
6
|
+
ITK_dev_shared_components.egg-info/SOURCES.txt
|
|
7
|
+
ITK_dev_shared_components.egg-info/dependency_links.txt
|
|
8
|
+
ITK_dev_shared_components.egg-info/requires.txt
|
|
9
|
+
ITK_dev_shared_components.egg-info/top_level.txt
|
|
10
|
+
ITK_dev_shared_components/SAP/__init__.py
|
|
11
|
+
ITK_dev_shared_components/SAP/gridview_util.py
|
|
12
|
+
ITK_dev_shared_components/SAP/multi_session.py
|
|
13
|
+
ITK_dev_shared_components/SAP/opret_kundekontakt.py
|
|
14
|
+
ITK_dev_shared_components/SAP/sap_login.py
|
|
15
|
+
ITK_dev_shared_components/SAP/sap_util.py
|
|
16
|
+
ITK_dev_shared_components/SAP/tree_util.py
|
|
17
|
+
tests/test_gridview_util.py
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: ITK_dev_shared_components
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
4
4
|
Summary: Shared components to use in RPA projects
|
|
5
5
|
Author-email: ITK Development <itk-rpa@mkb.aarhus.dk>
|
|
6
6
|
Project-URL: Homepage, https://github.com/itk-dev-rpa/itk-dev-shared-components
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "ITK_dev_shared_components"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.6"
|
|
8
8
|
authors = [
|
|
9
9
|
{ name="ITK Development", email="itk-rpa@mkb.aarhus.dk" },
|
|
10
10
|
]
|
|
@@ -17,10 +17,10 @@ classifiers = [
|
|
|
17
17
|
"Operating System :: Microsoft :: Windows",
|
|
18
18
|
]
|
|
19
19
|
dependencies = [
|
|
20
|
-
|
|
20
|
+
"pywin32 >= 306",
|
|
21
|
+
"selenium >= 4.10.0"
|
|
21
22
|
]
|
|
22
23
|
|
|
23
24
|
[project.urls]
|
|
24
25
|
"Homepage" = "https://github.com/itk-dev-rpa/itk-dev-shared-components"
|
|
25
26
|
"Bug Tracker" = "https://github.com/itk-dev-rpa/itk-dev-shared-components/issues"
|
|
26
|
-
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
import os
|
|
3
|
+
from ITK_dev_shared_components.SAP import gridview_util, sap_login, multi_session
|
|
4
|
+
|
|
5
|
+
class test_gridview_util(unittest.TestCase):
|
|
6
|
+
@classmethod
|
|
7
|
+
def setUpClass(cls):
|
|
8
|
+
user, password = os.environ['SAP Login'].split(';')
|
|
9
|
+
sap_login.login_using_cli(user, password)
|
|
10
|
+
session = multi_session.get_all_SAP_sessions()[0]
|
|
11
|
+
session.startTransaction("fmcacov")
|
|
12
|
+
session.findById("wnd[0]/usr/ctxtGPART_DYN").text = "25564617"
|
|
13
|
+
session.findById("wnd[0]").sendVKey(0)
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def tearDownClass(cls):
|
|
17
|
+
sap_login.kill_sap()
|
|
18
|
+
|
|
19
|
+
def setUp(self) -> None:
|
|
20
|
+
session = multi_session.get_all_SAP_sessions()[0]
|
|
21
|
+
self.table = session.findById("wnd[0]/usr/tabsDATA_DISP/tabpDATA_DISP_FC1/ssubDATA_DISP_SCA:RFMCA_COV:0202/cntlRFMCA_COV_0100_CONT5/shellcont/shell")
|
|
22
|
+
|
|
23
|
+
def test_scroll_entire_table(self):
|
|
24
|
+
gridview_util.scroll_entire_table(self.table)
|
|
25
|
+
gridview_util.scroll_entire_table(self.table, True)
|
|
26
|
+
|
|
27
|
+
def test_get_all_rows(self):
|
|
28
|
+
result = gridview_util.get_all_rows(self.table)
|
|
29
|
+
self.assertGreater(len(result), 0)
|
|
30
|
+
self.assertGreater(len(result[0]), 0)
|
|
31
|
+
|
|
32
|
+
def test_get_row(self):
|
|
33
|
+
result = gridview_util.get_row(self.table, 0, False)
|
|
34
|
+
self.assertGreater(len(result), 0)
|
|
35
|
+
|
|
36
|
+
result = gridview_util.get_row(self.table, 0, True)
|
|
37
|
+
self.assertGreater(len(result), 0)
|
|
38
|
+
|
|
39
|
+
def test_iterate_rows(self):
|
|
40
|
+
for row in gridview_util.iterate_rows(self.table):
|
|
41
|
+
self.assertGreater(len(row), 0)
|
|
42
|
+
|
|
43
|
+
def test_get_column_titles(self):
|
|
44
|
+
result = gridview_util.get_column_titles(self.table)
|
|
45
|
+
self.assertGreater(len(result), 0)
|
|
46
|
+
|
|
47
|
+
def test_find_row_index_by_value(self):
|
|
48
|
+
result = gridview_util.find_row_index_by_value(self.table, "TXTU2", "Test Deltrans")
|
|
49
|
+
self.assertNotEqual(result, -1)
|
|
50
|
+
|
|
51
|
+
result = gridview_util.find_row_index_by_value(self.table, "TXTU2", "Foo")
|
|
52
|
+
self.assertEqual(result, -1)
|
|
53
|
+
|
|
54
|
+
with self.assertRaises(ValueError):
|
|
55
|
+
gridview_util.find_row_index_by_value(self.table, "Foo", "Bar")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_find_all_row_indecies_by_value(self):
|
|
59
|
+
result = gridview_util.find_all_row_indecies_by_value(self.table, "TXTU2", "Gebyr")
|
|
60
|
+
self.assertGreater(len(result), 0)
|
|
61
|
+
|
|
62
|
+
result = gridview_util.find_all_row_indecies_by_value(self.table, "TXTU2", "Foo")
|
|
63
|
+
self.assertEqual(len(result), 0)
|
|
64
|
+
|
|
65
|
+
with self.assertRaises(ValueError):
|
|
66
|
+
gridview_util.find_all_row_indecies_by_value(self.table, "Foo", "Bar")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
if __name__ == '__main__':
|
|
75
|
+
unittest.main()
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
from selenium import webdriver
|
|
2
|
-
from selenium.webdriver.common.by import By
|
|
3
|
-
import os
|
|
4
|
-
import pathlib
|
|
5
|
-
import time
|
|
6
|
-
|
|
7
|
-
def login(user, password):
|
|
8
|
-
driver = webdriver.Chrome()
|
|
9
|
-
driver.implicitly_wait(10)
|
|
10
|
-
driver.get('https://portal.kmd.dk/irj/portal')
|
|
11
|
-
driver.maximize_window()
|
|
12
|
-
|
|
13
|
-
#Login
|
|
14
|
-
user_field = driver.find_element(By.ID, 'logonuidfield')
|
|
15
|
-
pass_field = driver.find_element(By.ID, 'logonpassfield')
|
|
16
|
-
login_button = driver.find_element(By.ID, 'buttonLogon')
|
|
17
|
-
|
|
18
|
-
user_field.clear()
|
|
19
|
-
user_field.send_keys(user)
|
|
20
|
-
|
|
21
|
-
pass_field.clear()
|
|
22
|
-
pass_field.send_keys(password)
|
|
23
|
-
|
|
24
|
-
login_button.click()
|
|
25
|
-
|
|
26
|
-
#Opus
|
|
27
|
-
mine_genveje = driver.find_element(By.CSS_SELECTOR, "div[title='Mine Genveje']")
|
|
28
|
-
mine_genveje.click()
|
|
29
|
-
|
|
30
|
-
#Wait for download and launch file
|
|
31
|
-
wait_for_download()
|
|
32
|
-
|
|
33
|
-
driver.quit()
|
|
34
|
-
|
|
35
|
-
#TODO: Wait for if SAP has opened
|
|
36
|
-
|
|
37
|
-
def wait_for_download():
|
|
38
|
-
downloads_folder = str(pathlib.Path.home() / "Downloads")
|
|
39
|
-
for _ in range(10):
|
|
40
|
-
for file in os.listdir(downloads_folder):
|
|
41
|
-
if file.endswith(".sap"):
|
|
42
|
-
path = os.path.join(downloads_folder, file)
|
|
43
|
-
os.startfile(path)
|
|
44
|
-
return
|
|
45
|
-
|
|
46
|
-
time.sleep(0.5)
|
|
47
|
-
raise TimeoutError(f".SAP file not found in {downloads_folder}")
|
|
48
|
-
|
|
49
|
-
if __name__=="__main__":
|
|
50
|
-
user, password = os.environ['Opus Login'].split(';')
|
|
51
|
-
login(user, password)
|
|
52
|
-
|
|
53
|
-
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import subprocess
|
|
2
|
-
def login_cli(username: str, password: str, client='751', system='P02') -> None:
|
|
3
|
-
"""
|
|
4
|
-
Login SAP with commnd line arguments.
|
|
5
|
-
:param username: AZ username
|
|
6
|
-
:param password: password
|
|
7
|
-
:param client: Kommune ID (Aarhus = 751)
|
|
8
|
-
:param system: Environment SID (e.g. P02 = 'KMD OPUS Produktion [P02]')
|
|
9
|
-
:return:
|
|
10
|
-
"""
|
|
11
|
-
subprocess.run(["C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe",
|
|
12
|
-
f"-system={system}",
|
|
13
|
-
f"-client={client}",
|
|
14
|
-
f"-user={username}",
|
|
15
|
-
f"-pw={password}"
|
|
16
|
-
])
|
|
17
|
-
|
|
18
|
-
if __name__ == "__main__":
|
|
19
|
-
login_cli('az12345', 'P4$$w0rd')
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
LICENSE
|
|
2
|
-
README.md
|
|
3
|
-
pyproject.toml
|
|
4
|
-
src/ITK_dev_shared_components/__init__.py
|
|
5
|
-
src/ITK_dev_shared_components.egg-info/PKG-INFO
|
|
6
|
-
src/ITK_dev_shared_components.egg-info/SOURCES.txt
|
|
7
|
-
src/ITK_dev_shared_components.egg-info/dependency_links.txt
|
|
8
|
-
src/ITK_dev_shared_components.egg-info/requires.txt
|
|
9
|
-
src/ITK_dev_shared_components.egg-info/top_level.txt
|
|
10
|
-
src/ITK_dev_shared_components/SAP/multi_session.py
|
|
11
|
-
src/ITK_dev_shared_components/SAP/sap_login.py
|
|
12
|
-
src/ITK_dev_shared_components/SAP/sap_login_cli.py
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
pywin32
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|