cancel
Showing results for 
Search instead for 
Did you mean: 

Exception handling using python

SOLVED
Highlighted
Contributor

Exception handling using python

I'm trying to use Try block to catch element not found exception and Log a message whenever there is an exception.

But for some reason, when a exception occurs the control does not seem to get into the except block and the log message is not printed. But since i have a try block, even if a error occurs in the if statement the code does not stop getting executed.

      try:
        if page.FindChildByXPath(NoApprovals).VisibleOnScreen == True:
          Queue[i]=0
          Log.Message("found")
      except Exception as e:
        Log.Message("Element not found")

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Exception handling using python

I'm not sure this is exactly a Python problem.  I see two potential issues with the code.

 

1) What if FindChildByXPath actually returns an empty stub object?  That object does not have the "VisibleOnScreen" property.  Errors like that don't raise as "exceptions", those are object recognition errors that need to be handled differently.  What you should do is check "Exists", not "VisibleOnScreen", if Exists is true, then you can do any other checks you want.  

2) You're doing a search by XPath.  VisibleOnScreen is a property that TestComplete applies to distinct objects.  Finding an object by XPath does not ALWAYS return an object that TestComplete can interact with as a UI object so it might not even have the VisibleOnScreen property. Again... this would be an object recognition error, but with a different root cause.  This would not raise an exception and would not go into the except block. 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available

View solution in original post

6 REPLIES 6
Community Manager

Re: Exception handling using python

Python guru, any suggestions?

 

@sriram_sig, please take a look at this article as well:

https://support.smartbear.com/testcomplete/docs/scripting/handling-exceptions.html#Python

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Hero

Re: Exception handling using python

I'm not sure this is exactly a Python problem.  I see two potential issues with the code.

 

1) What if FindChildByXPath actually returns an empty stub object?  That object does not have the "VisibleOnScreen" property.  Errors like that don't raise as "exceptions", those are object recognition errors that need to be handled differently.  What you should do is check "Exists", not "VisibleOnScreen", if Exists is true, then you can do any other checks you want.  

2) You're doing a search by XPath.  VisibleOnScreen is a property that TestComplete applies to distinct objects.  Finding an object by XPath does not ALWAYS return an object that TestComplete can interact with as a UI object so it might not even have the VisibleOnScreen property. Again... this would be an object recognition error, but with a different root cause.  This would not raise an exception and would not go into the except block. 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available

View solution in original post

Contributor

Re: Exception handling using python

Thanks for your response. Actually the if statement in my code was not returning an exception since it was returning an empty stub object. Also i wanted to know the best way in testcomplete to store my object names and its corresponding xpath like a key-value pair, when using selenium with java i used to store these values in a properties file. so loooking for something similar in testcomplete

Community Hero

Re: Exception handling using python

You mean.... like.... NameMapping?

 

While, it doesn't use XPath (in fact, in the TestComplete universe, XPath should only be used if the object is not available/visible to the main object browser), it provides an object repository with object hierarchy and properties providing the identification factors.  I know you're probably coming from Selenium... but this is a different tool with a built in object recognition feature...  Rather than re-invent the wheel, you might find that to work better.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Contributor

Re: Exception handling using python

Thanks for your suggestion, i absolutely agree with it.

I have a scenario where i'm having a simple web page with multiple links in it and one of the testcase for me is to verify if these links with matching text exists. when i try adding these links as objects in testcomplete, all but except one of the link falls under a different hierachy. Is there a way by which i can just have one generic function to verify if a link exists by passing the page and object as parameters. Since i might need to verify links present in different pages

Community Hero

Re: Exception handling using python

There's a way to do so using a web accessibility checkpoint.  @LinoTadros  demonstrated it at the 2017 Connect conference...  I have the notes around here, somewhere.... 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
New Here?
Join us and watch the welcome video:
Top Kudoed Authors