cancel
Showing results for 
Search instead for 
Did you mean: 

Close specific browser script error

SOLVED
efuller
Occasional Contributor

Close specific browser script error

Hello Everyone,

I created a script to check for specific web browsers exist, and if so to close them. Otherwise, just add a log message to say the browser wasn't open. Unfortunately I'm getting odd log entries saying steps failed when they technically didn't (To my knowledge). Can you take a look at the script and let me know if you can find a reason for the error?

 

def CheckForChromeBrowser():
  b = Sys.Browser("Chrome")
  if b.Exists:
    b.Terminate()
    Log.Message("Chrome browser was detected. Successfully closed browser.")
  elif b.Exists != True:
    Log.Message("Chrome browser was not detected. No action needed.")
  else:
    Log.Error("Unable to determine if Chrome browser is running. Action failed.")

def CheckForIEBrowser():
  b = Sys.Process("iexplore")
  if b.Exists:
    b.Terminate()
    Log.Message("IE browser was detected. Successfully closed browser.")
  elif b.Exists != True:
    Log.Message("IE browser was not detected. No action needed.")
  else:
    Log.Error("Unable to determine if IE browser is running. Action failed.")

 

This is what appears on the log when the specific browser it is looking for doesn't exist:

 

ss-1.png

 

I noticed the code is failing the step because this line of code:

 

b = Sys.Browser("Chrome")

So, TestComplete is looking for that object to exist and because it doesn't the step is failed. Obviously I'm just trying to assign that variable and not say to look for it. I also noticed if I just type, 

 

if Sys.Browser("Chrome").Exists:
    Sys.Browser("Chrome").Terminate()

TestComplete is still expecting the object to exist and failing the step because it doesn't. Can anyone think of a way around this?

1 ACCEPTED SOLUTION

Accepted Solutions
AlexKaras
Community Hero

Re: Close specific browser script error

Hi,

 

A little bit to add to what was said by @baxatob to ease your confusion 🙂 :

TestComplete can get a reference to any tested object using either 'direct' syntax (like Sys.Browser()) or 'indirect' one (like Sys.WaitBrowser()). Basically, any get/search function has its WaitXXX complimentary one.

The difference is that when 'direct' object addressing is used, this tells TestComplete that you are sure that the addressed object must exist. This is the reason for TestComplete to report a problem if directly referenced object cannot be found.

In case of 'indirect' addressing via the WaitXXX(), this means that the sought for object may not exist and the error is not reported if the object is not found.

As for .Exists property: if the object was found when been 'directly' referenced, .Exists obviously equals to True.

But if you got a reference to, say, var b = Sys.Browser("chrome") when it was running and than closed it, then b.Exists will evaluate to False.

 

That is why if you are not certain whether or not some object exists at the given moment, you should try to get a reference to it using corresponding WaitXXX() function, check the value of .Exists and proceed appropriately.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

3 REPLIES 3
baxatob
Community Hero

Re: Close specific browser script error

Hi,

 

Correct syntax - Sys.Browser("chrome")

 

I use the following method:

 

def check_redundant_browser_process(process):
    Sys.Refresh()
    if Sys.WaitProcess('{}'.format(process)).Exists:
        Sys.Browser(process).Close()
        Sys.Refresh()
        if Sys.WaitProcess('{}'.format(process)).Exists:
           Sys.WaitProcess('{}'.format(process)).Terminate()
        Log.Message("The redundant '{}' process was killed".format(process))
efuller
Occasional Contributor

Re: Close specific browser script error

maxresdefault.jpg

AlexKaras
Community Hero

Re: Close specific browser script error

Hi,

 

A little bit to add to what was said by @baxatob to ease your confusion 🙂 :

TestComplete can get a reference to any tested object using either 'direct' syntax (like Sys.Browser()) or 'indirect' one (like Sys.WaitBrowser()). Basically, any get/search function has its WaitXXX complimentary one.

The difference is that when 'direct' object addressing is used, this tells TestComplete that you are sure that the addressed object must exist. This is the reason for TestComplete to report a problem if directly referenced object cannot be found.

In case of 'indirect' addressing via the WaitXXX(), this means that the sought for object may not exist and the error is not reported if the object is not found.

As for .Exists property: if the object was found when been 'directly' referenced, .Exists obviously equals to True.

But if you got a reference to, say, var b = Sys.Browser("chrome") when it was running and than closed it, then b.Exists will evaluate to False.

 

That is why if you are not certain whether or not some object exists at the given moment, you should try to get a reference to it using corresponding WaitXXX() function, check the value of .Exists and proceed appropriately.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors