Skip to content

Commit fdab2e3

Browse files
Merge pull request #332 from AutomationSolutionz/ProcessID_Generate_Windows
Stored the application's process id while launching
2 parents 9f989b3 + 5ba7c08 commit fdab2e3

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

Framework/Built_In_Automation/Desktop/Windows/BuiltInFunctions.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636

3737
python_folder = []
38+
current_pid_list = []
3839
for location in subprocess.getoutput("where python").split("\n"):
3940
if "Microsoft" not in location:
4041
python_folder.append(location)
@@ -386,6 +387,7 @@ def Element_only_search(
386387
):
387388
# max_time is built in wait function. It will try every seconds 15 times.
388389
try:
390+
389391
if Parent_Element is not None:
390392
ParentElement = Parent_Element
391393
element_index = -1
@@ -405,6 +407,7 @@ def Element_only_search(
405407

406408
)
407409
all_elements += tmp_elements
410+
408411
if all_elements:
409412
return all_elements
410413
else:
@@ -1675,6 +1678,8 @@ def _get_main_window(WindowName):
16751678
MainWindowsList = AutomationElement.RootElement.FindAll(
16761679
TreeScope.Children, Condition.TrueCondition
16771680
)
1681+
1682+
found_windows = []
16781683
for MainWindowElement in MainWindowsList:
16791684
try:
16801685
if WindowName[2] == "pid":
@@ -1686,12 +1691,19 @@ def _get_main_window(WindowName):
16861691
else:
16871692
NameS = MainWindowElement.Current.Name
16881693
if _found(WindowName, NameS):
1689-
CommonUtil.ExecLog(sModuleInfo, "Switching to window: %s" % NameS, 1)
1690-
autoit.win_activate(NameS)
1691-
return MainWindowElement
1694+
if MainWindowElement.Current.ProcessId in current_pid_list:
1695+
CommonUtil.ExecLog(sModuleInfo, "Switching to window: %s" % NameS, 1)
1696+
CommonUtil.ExecLog(sModuleInfo, f"pid matched: {MainWindowElement.Current.ProcessId}", 5)
1697+
autoit.win_activate(NameS)
1698+
return MainWindowElement
1699+
else:
1700+
found_windows.append(MainWindowElement)
16921701
except:
16931702
pass
1694-
1703+
if len(found_windows) > 0:
1704+
CommonUtil.ExecLog(sModuleInfo, "Switching to window: %s" % found_windows[0].Current.Name, 1)
1705+
autoit.win_activate(found_windows[0].Current.Name)
1706+
return found_windows[0]
16951707
return None
16961708
except Exception:
16971709
CommonUtil.Exception_Handler(sys.exc_info())
@@ -2096,6 +2108,8 @@ def _open_inspector(inspector, args):
20962108
@logger
20972109
def Run_Application(data_set):
20982110
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
2111+
global current_pid_list
2112+
current_pid_list = []
20992113
try:
21002114
args = {"shell": True, "stdin": None, "stdout": None, "stderr": None}
21012115
launch_cond = ""
@@ -2142,6 +2156,7 @@ def Run_Application(data_set):
21422156
subprocess.Popen(cmd, **args)
21432157
# Desktop_app = os.path.basename(Desktop_app)
21442158
else:
2159+
#last_start_time = time.time()
21452160
autoit.send("^{ESC}")
21462161
time.sleep(0.5)
21472162
autoit.send(Desktop_app)
@@ -2154,6 +2169,10 @@ def Run_Application(data_set):
21542169
# Desktop_app += ".exe"
21552170
CommonUtil.ExecLog(sModuleInfo, "Waiting for the app to launch for maximum %s seconds" % wait, 1)
21562171
s = time.time()
2172+
for process in psutil.process_iter(['pid', 'name']):
2173+
if Desktop_app.lower() in process.info['name'].lower():
2174+
current_pid_list.append(process.info['pid'])
2175+
21572176
while time.time() - s < wait:
21582177
# if len(pygetwindow.getWindowsWithTitle(Desktop_app)) > 0: # This is case in-sensitive
21592178
if pygetwindow.getActiveWindow() is None or Desktop_app in pygetwindow.getActiveWindow().title: # This is case sensitive
@@ -2165,7 +2184,6 @@ def Run_Application(data_set):
21652184
else:
21662185
CommonUtil.ExecLog(sModuleInfo, "Could not find any launched app with title: %s however continuing" % Desktop_app, 2)
21672186
return "passed"
2168-
21692187
if maximize:
21702188
win = pygetwindow.getWindowsWithTitle(Desktop_app)[0]
21712189
win.maximize()

0 commit comments

Comments
 (0)