[1.1.4] Windows: app focus and close only work if app instance has an associated window

Bug #1811361 reported by Seyed Mohammad Hossein Amirkhalili
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SikuliX
In Progress
Medium
RaiMan

Bug Description

---------------------- problem and workaround
someApp.focus() and someApp.close() will only work, if the app object created with App("some.exe").open() will have a window associated after startup.

-------------------- workaround
In complex cases like portable Chrome, there is no associated window with the main process and hence it is only possible to focus the app with a part of the window title, which in turn allows to get the app object with the window-owning process, that in turn is focusable and closeable.

------------------------------------------------------------

1.1.4-SNAPSHOT-2019-01-10_09:21/Windows10.0/Java8(64)1.8.0_191-b12

Windows 10 64bit

I used this script to open two portable chrome instances and then tried to change the focus to them or close them with .focus() and .close() method but nothing happens:

open_instances = {} # a dictionary to keep record of opened browsers {name:app}

def browser(target, action = True ):
    temp = r"C:\Users\Test\Desktop\Chromes\f" + str(target) + r"\f" + str(target) + r".exe"
    if action: #for opening
        open_instances["f"+str(target)] = App(temp)
        open_instances["f"+str(target)].open(10)
    else: # for closing
        open_instances["f"+str(target)].close()
        del open_instances["f"+str(target)]
        wait(1)

def main():
    for i in range(1,3): # trying to open the instances which works fine
        browser(i)
        wait(2)
    for i in range(1,3): # trying to close same instances but it does not work. I even tried the .focus() but no success either
        browser(i, False)

main()

RaiMan (raimund-hocke)
summary: - app focus and close does not work
+ [1.1.4] Windows: app focus and close does not work anymore (since 1st
+ Jan '19)
Changed in sikuli:
status: New → Fix Committed
importance: Undecided → High
assignee: nobody → RaiMan (raimund-hocke)
milestone: none → 1.1.4
Revision history for this message
RaiMan (raimund-hocke) wrote :

Tested with the latest build (> #180):
everything works as expected.

Changed in sikuli:
status: Fix Committed → Opinion
importance: High → Medium
Revision history for this message
Seyed Mohammad Hossein Amirkhalili (hosami) wrote :

1.1.4-SNAPSHOT-2019-01-13_11:36/Windows10.0/Java8(64)1.8.0_191-b12

I just used this simple script to test it. On my end still nothing happens when want to focus or close the app:

temp = r"C:\Users\Test\Desktop\Chromes\f1\f1.exe"

a = App(temp)
print a
a.open(10) #works
wait (5)
a.focus() #does not focus. I switched to another app to see if it can bring back the focus and nothing.
a.close() #nothing happens here either.

Revision history for this message
RaiMan (raimund-hocke) wrote :

This is my test:

temp = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
print "before open"
App.logOn() # prints detailed logs
a = App(temp)
a.open(10)
print "after open"
App.open("notepad") # to steel focus
wait(3)
a.focus()
print "after focus"
wait(3)
a.closeByKey(3) # see comment
print "after close"
App.logOff()

--- comment
closeByKey does a "natural" close (focus and issue alt-f4).
... but close works also (chrome does not like to be killed ;-)

and again: everything works as expected.

Revision history for this message
Seyed Mohammad Hossein Amirkhalili (hosami) wrote :

I tested your script and it worked perfectly fine.

Then I tried my code again with your entries like below: (I think I know the reason for this: I am using portable version of chrome. Does this tell you anything? Could it be the reason? )

temp = r"C:\Users\Test\Desktop\Chromes\f1\f1.exe"
App.logOn()
a = App(temp)
print a
a.open(10) #worked
print "after open"
App.open("notepad") # to steel focus # worked
wait(3)
a.focus()
print "after focus"
wait(3)
a.closeByKey(3) # see comment
print "after close"
App.logOff()

then It stopped and gave me this log:

[AppLog] App.create: [-1:f1]
[-1:f1 ()] C:\Users\Test\Desktop\Chromes\f1\f1.exe

[AppLog] App.open: [1068:f1 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f1\f1.exe

[AppLog] App.focus: no window for [1068:f1 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f1\f1.exe
after open
[AppLog] App.create: [-1:notepad]

[AppLog] App.open: [17516:notepad (Untitled - Notepad)] notepad

[AppLog] App.focus: [17516:notepad (Untitled - Notepad)] notepad

[AppLog] App.focus: no window for [1068:f1 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f1\f1.exe
after focus

[AppLog] App.focus: no window for [1068:f1 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f1\f1.exe

[error] script [ Untitled ] stopped with error at line --unknown--
[error] Error caused by: Traceback (most recent call last): File "C:\Users\Test\AppData\Local\Temp\Sikulix_1621692664\sikuli-6955607908584789750.py", line 12, in <module> a.closeByKey(3) # see comment at org.sikuli.script.App.closeByKey(App.java:638)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException

Revision history for this message
Seyed Mohammad Hossein Amirkhalili (hosami) wrote :

I found more evidence that the issue is related to Portable version of chrome. This is another test I runned:

This time, I first opened an instance of chrome and then tried to open another one (both portable version and located in two different and separate folders):

as you can see, Sikuli will not open chrome12 since it thinks it is already opened! However the one is opened is actually Chrome1. They are in different folders and both portable.

code:

temp = r"C:\Users\Test\Desktop\Chromes\f12\f12.exe" # another chrome already opened
App.logOn()
a = App(temp)
print a
a.open(10) #works
print "after open"
App.open("notepad") # to steel focus # worked fine.
wait(3)
a.focus() # did not work
print "after focus"
wait(3)
a.closeByKey(3) # see comment
print "after close"
App.logOff()

Log:

[AppLog] App.create: [-1:f12]
[-1:f12 ()] C:\Users\Test\Desktop\Chromes\f12\f12.exe

[AppLog] App.open: already running: [20776:f12 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f12\f12.exe
[AppLog] App.focus: no window for [20776:f12 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f12\f12.exe
after open
[AppLog] App.create: [-1:notepad]

[AppLog] App.open: [16404:notepad (Untitled - Notepad)] notepad

[AppLog] App.focus: [16404:notepad (Untitled - Notepad)] notepad

[AppLog] App.focus: no window for [20776:f12 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f12\f12.exe
after focus

[AppLog] App.focus: no window for [20776:f12 (OleMainThreadWndName)] C:\Users\Test\Desktop\Chromes\f12\f12.exe

[error] script [ Untitled ] stopped with error at line --unknown--
[error] Error caused by: Traceback (most recent call last): File "C:\Users\Test\AppData\Local\Temp\Sikulix_1621692664\sikuli-2032410969421491184.py", line 12, in <module> a.closeByKey(3) # see comment at org.sikuli.script.App.closeByKey(App.java:638)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException

Revision history for this message
RaiMan (raimund-hocke) wrote : Re: [1.1.4] Windows: app focus and close should work for more than one instance running of same some-name.exe

ok, understood.
see changed bug description.

Due to private priorities will only be fixed towards end of Feb '19.

summary: - [1.1.4] Windows: app focus and close does not work anymore (since 1st
- Jan '19)
+ [1.1.4] Windows: app focus and close should work for more than one
+ instance running of same some-name.exe
Changed in sikuli:
status: Opinion → In Progress
description: updated
Revision history for this message
Seyed Mohammad Hossein Amirkhalili (hosami) wrote :

any update on this?

Revision history for this message
RaiMan (raimund-hocke) wrote :

Uuuups, lost in space;-)

... I have to scroll down in the list of bugs, to see it;-)

Since for the next time, my main dev machine is Windows 10, there is a chance for movement.

Revision history for this message
RaiMan (raimund-hocke) wrote :
Revision history for this message
RaiMan (raimund-hocke) wrote :

Ok, made a test with 2 copies of the portable Chrome:
They do not report having a Window, hence focus() will not work as currently implemented.

[10156:chrome (Neuer Tab - Google Chrome)] C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[10080:chrome1 (null)] C:\Chromes\Chrome1\chrome1.exe
[13612:chrome2 (null)] C:\Chromes\Chrome2\chrome2.exe

the first is the normal Chrome, the 2 others are the portables.

Does not look very promising.

Revision history for this message
RaiMan (raimund-hocke) wrote :

ok, the case is clear now.

Chrome is not a compound app, but a bunch of subprocesses.

With normal Chrome, the started exe will be the application pid and have the browser window.

With portable Chrome, the started exe will not have the window, but one of the subprocesses named chrome.exe.

To evaluate such complex constructs is far beyond the scope of SikuliX.

Conclusion: someApp.focus() will only work, if the app object created with App("some.exe").open() will have a window associated after startup.

In such cases as yours, there is no associated window and hence it is only possible to focus the app with a part of the window title, which in turn allows to get the app object with the window-owning process, that in turn is focusable and closeable.

RaiMan (raimund-hocke)
description: updated
Changed in sikuli:
status: In Progress → Confirmed
importance: Medium → Undecided
description: updated
summary: - [1.1.4] Windows: app focus and close should work for more than one
- instance running of same some-name.exe
+ [1.1.4] Windows: app focus and close only work if app instance has an
+ associated window
RaiMan (raimund-hocke)
Changed in sikuli:
status: Confirmed → In Progress
importance: Undecided → Medium
milestone: 1.1.4 → 2.1.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.