Forum Discussion

efuller's avatar
efuller
Occasional Contributor
8 years ago

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:

 

 

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?

  • 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.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    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.

  • baxatob's avatar
    baxatob
    Community Hero

    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))