Best way to code closing a browser(app) triggered after a complete windows copy event

Asked by Mel Adajar

I'm trying to figure out what is the best way to code this set of events: ( windows environment, running FireFox )

-after executing a web application (report), wait for report to finish, then:
Perform a File > Save Page As > newfilename > Save > close Firefox

So far I have it working with this code starting at the waiting of the report:

wait(50) --high to give me time if needed for a slow report
click(File.png)
click(SavePageAs.png)
exists(FilenameBox.png,0)
paste("MyNewFile")
type(Key.ENTER)
wait(5.0)
exists(SaveButton.png), 0)
click(getLastMatch())
wait(30.)
closeApp("Firefox")
closeApp("cmd")

Intermittent issues so far:
-Sometimes when it saves the file, it takes sometime to finish the copy of the file to a remote folder. Firefox pops up a window and shows copy progress of my "MyNewFile" to its target folder.
-So if it takes long or passes my wait(30) threshold, it will then close the app before I need it too

I would like to make it more robust where I am waiting for the copy to finish. Should I be capturing that window?
What is a good way to handle this event? Once I know the copy is done using Firefox, then I want to close the app.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Calle Rundgren (c-rundgren) said :
#1

I would use a while()-loop in order to wait until the save is finished. It can look like this.

while exists(Image which appears on screen) #the Image shown during the save.
    wait(5) #wait and checks the screen once every 5s
App.close("Firefox") #use App.close() instead of closeApp()
App.close("cmd")

For more information: http://sikuli.org/docx/tutorials/surveillance/surveillance.html

Revision history for this message
RaiMan (raimund-hocke) said :
#2

Calle's suggestion is the principal approach, to solve such a situation.

I prefer this variant:
while exists(image, 0): wait(1)

advantage: this loop will end latest about 2 second after the progress window vanished. Calle's will take 3 to 8 seconds (waiting time 3 seconds plus 5 seconds).

if you only want to wait, you might try:
waitVanish(image, FOREVER)

The above loop is equivalent, but only needed, if you want to do additional things while waiting.

But I think there is an additional challenge:
After clicking the save button, it might take some time for the progress window to come up.
So supposing, that it gets visible in all cases, I would do it this way:

while not exists(image, 0): wait(1) # waits for it
while exists(image, 0): wait(1) # waits for it to vanish

---- some general comments on your script:

-- whenever possible, one should use keyboard shortcuts instead of clicking menu entries
click(File.png)
click(SavePageAs.png)

could be substituted by:
type("s", KEY_CTRL+KEY_SHIFT)

-- usage of exists()
exists(FilenameBox.png,0)

simply does "nothing": it makes one try to find the image and comes back if found or not.
what makes sense here would be:

while not exists(FilenameBox.png,0): wait(1)

this would wait for the file dialog to be ready for the following paste.

-- same for this sequence:
wait(5.0)
exists(SaveButton.png), 0)
click(getLastMatch())

better:
while not exists(SaveButton.png, 0): wait(1)
click(getLastMatch())

--- make scripts robust:
have a look at faq 1607

Revision history for this message
Calle Rundgren (c-rundgren) said :
#3

Thank you RaiMan, I should have known about the faster way to solve the situation. Well well, I won't forget now ;)

Can you help with this problem?

Provide an answer of your own, or ask Mel Adajar for more information if necessary.

To post a message you must log in.