Perform action on Ranorex.ElementNotFound exception

Class library usage, coding and language questions.
richard.rea
Posts: 4
Joined: Tue Apr 30, 2013 12:53 pm

Perform action on Ranorex.ElementNotFound exception

Post by richard.rea » Tue Apr 30, 2013 1:07 pm

Hi there

I have been using Ranorex Studio for a while now but have come across a task I want it to perform and can not seem to find out how to do so and was wondering if the people here would be able to assist.

I am Using Ranorex through a Visual Studio C# solution that uses a Ranorex repository. Tests are defined in the solution using SpecFlow and then run automatically with NUnit on schedules.

A lot of the time when the tests do fail it's due to a Ranorex.ElementNotFound exception happening because the application has crashed or hung etc but we think it would be useful if we were able to get a screenshot of what is on the screen at this time.

Is it possible to get Ranorex to perform a custom action upon an exception occuring (in this case, grabbing a screenshot and maybe giving it a meaningful name) when it is being run through SpecFlow and NUnit like this? I have been searching around and can not find a way to do this myself.

Any help would be appreciated.

Thanks in advance

Richard Rea

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Perform action on Ranorex.ElementNotFound exception

Post by Ciege » Tue Apr 30, 2013 4:00 pm

If you surround your code in a try catch block then you can call the Ranorex screenshot method in the catch...

Something like this:

Try
{
//my code
}
catch
{
Screenshot
}
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

richard.rea
Posts: 4
Joined: Tue Apr 30, 2013 12:53 pm

Re: Perform action on Ranorex.ElementNotFound exception

Post by richard.rea » Tue Apr 30, 2013 4:12 pm

Hi Ciege

Thanks for the suggestion but this was what I was trying to avoid having to do because at the minute, we have well over 300 tests and each one will try to use the repository multiple times which means making hundreds (maybe even thousands) of try catch statements.

I was hoping there would be an easier/cleaner way to do this like just one exception catching method that could control what happens when Ranorex.ElementNotFound exception occurs.

Does no such thing exist and will I just have to resort to thousands of try catches?

Thanks

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Perform action on Ranorex.ElementNotFound exception

Post by Ciege » Tue Apr 30, 2013 4:30 pm

Well, do you want to do it *right* or do you want to do it *fast*? :D

Are you using the Ranorex TestSuite with TestCases methodology to run all your tests? If so you could add an error check and screenshot to the TearDown regions...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

richard.rea
Posts: 4
Joined: Tue Apr 30, 2013 12:53 pm

Re: Perform action on Ranorex.ElementNotFound exception

Post by richard.rea » Tue Apr 30, 2013 5:21 pm

Ideally both. Haha.

I don't think we use the Ranorex test suite, no. We are only using Ranorex for the repositories. The actual tests themselves are run through SpecFlow and NUnit. There are some tear down functions defined for the SpecFlow scenarios but I think they are just defaults.

If I put a screen capture in the tear down, would that wait until the end of the test to capture the screenshot or would it capture the screenshot immediately when the exception occurs?

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Perform action on Ranorex.ElementNotFound exception

Post by Ciege » Tue Apr 30, 2013 5:43 pm

I'm not sure about your question regarding the TearDown section as I do not use Test Suites either...

All of my tests are self contained methods that I compile and run at build time (CI) or on demand through my own launcher. Every test method has one or several try/catch blocks implemented to catch any expected or unexpected exceptions and deal with them as required. A tenet of good test automation is to write your code as you would any other software project by predicting expected results and capturing and dealing with unexpected results in such a way that tests can continue (if wanted) and result logs hold complete and accurate details of what happens (especially when errors occur) so that you can review the logs and replicate the steps manually to verify why failures have occurred.

I'm not exactly sure if you are up to it, or if this will even solve your issue "easily" but you could attempt to write a second helper thread that specifically watches for thrown exceptions and screenshots & halts testing when one occurs. I do this when checking for unexpected exceptions thrown by my AUT. But I am not sure if a second thread can listen for Ranorex exceptions on the first thread.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

richard.rea
Posts: 4
Joined: Tue Apr 30, 2013 12:53 pm

Re: Perform action on Ranorex.ElementNotFound exception

Post by richard.rea » Tue Apr 30, 2013 6:40 pm

Hmm...that does sound awesome but I'm not sure if it would work with the set up I'm using here because Ranorex is not running the test the whole way through and is only being used to in tiny methods called by SpecFlow to validate the presence of certain GUI elements and so forth so there is not a main thread of it running all the time that could start a second thread to watch it.

I'm still a bit new to Ranorex, so I apologise if I'm mistaken here but here is a very brief description of the way our tests run:

Specflow scenarios -> Specflow Step Definitions -> API methods performing basic checks using repository items

I think I'm just going to have to go with your original idea of using try catch statements and then dumping screenshots.

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Perform action on Ranorex.ElementNotFound exception

Post by Ciege » Tue Apr 30, 2013 7:23 pm

richard.rea wrote:I think I'm just going to have to go with your original idea of using try catch statements and then dumping screenshots.
Personally I think that is the best way to go...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...