Ask a Question

Close Browser before Test

sirping
Occasional Contributor

Close Browser before Test

I need a Python script that closes Chrome if it is open before a Test

 

Currently I have a function looking like this

 

def closeBrowserIfOpen():
    p = Sys.WaitBrowser("chrome", 0)
    if (p.Exists):
        p.Close()
    else:
        Log.Message("Browser Closed")

 

 

But even though I have 0 as the timeout parameter, the script still waits for the Browser for about 3 seconds. How can I get rid of this delay completely? I just want it to execute instantly. In the docu it says:

 

Timeout

The number of milliseconds to wait until the browser process becomes available. If Timeout is 0, the method returns immediately. If Timeout is -1, the wait time is specified by the Auto-wait timeout project setting.

 

But this is not true, since there is still a timeout of about 3 seconds while executing this code.

 

Btw: My Auto-wait timout is 40000.

3 REPLIES 3
npaisley
Staff

Hi @sirping!

 

Looks like your 'p' variable is using a Wait method, which will take a few seconds to find the process. 

 

You can simply swap this out with the updated code below. This will be the fastest way to find and close Chrome.

p = Sys.Process("Chrome")

 

I hope this helps!


Regards,
Nick

Solutions Engineer @ SmartBear
sirping
Occasional Contributor

@npaisley 

I want to close the browser, if it is open. But if it is not open I want to do nothing. This code would fail if the Chrome Browser would not be open

rraghvani
Contributor

Have a look at Close Web Browsers 

cancel
Showing results for 
Search instead for 
Did you mean: