This is a migrated thread and some comments may be shown as answers.

Customize System.NullReferenceException: Object reference not set to an instance of an object.

9 Answers 108 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Anton
Top achievements
Rank 1
Anton asked on 08 Feb 2013, 11:14 AM
Hello.

I want to customize exception messages when element not found.
For example i want to write an element path.
Is it possible?

9 Answers, 1 is accepted

Sort by
0
Asta
Top achievements
Rank 1
answered on 08 Feb 2013, 01:13 PM
Yes, by using try catch

try{
// Some code

                    }
                    catch (NullReferenceException)
                    {
                     s.Write("Element not found"); 
                    }
0
Anton
Top achievements
Rank 1
answered on 08 Feb 2013, 01:18 PM
Hello Asta,
I know this way, but it applicable only for one FindElement.
I want to catch all exceptions.

For example:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(
               OnException);
 
public static void OnException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception exception = (Exception)e.ExceptionObject;
            Console.Error.WriteLine(exception.Message);
            Environment.Exit(1);
        }
0
Asta
Top achievements
Rank 1
answered on 08 Feb 2013, 01:39 PM
There you go...


DoAction(Action action){
try{ action();}
catch{ a.Write("Errror!");}
}
 
[Test]
public void Add(){
DoAction(Add);
}
 
And google could help you Click Here
0
Anton
Top achievements
Rank 1
answered on 08 Feb 2013, 02:49 PM
Probably did you mean smth like this?

public static void DoAction(Action action)
        {
            try
            {
                action();
            }
            catch(NullReferenceException e)
            {
                Console.WriteLine("adfadfaf");
            }
 
        }
 
[Test]
        public void Test0001()
        {
            DoAction(Test001WrongLogin);
         }
 
[Test]
        public void Test001WrongLogin()
        {
            ActiveBrowser.NavigateTo(Configuration.Instance.Url);
            Find.ByExpression<HtmlInputText>(LoginActions.LoginField).Text = "asd321";
        }

It's work but i need to rewrite all my tests and it's not a global handling. I can also use try{} catch{} in main test but it's not solving my problem.

I'm searching in google but found only Application.ThreadException event wich available only for WebForms.
AppDomain.CurrentDomain.UnhandledException  as i wrote above handle only Gallio(MbUnit) exceptions.
0
Asta
Top achievements
Rank 1
answered on 08 Feb 2013, 02:51 PM
Be smart.. Use google :)
0
Anton
Top achievements
Rank 1
answered on 08 Feb 2013, 03:38 PM
Seriously? )

I'm google it for a 5 hours and i didn't find solutions even for Selenium or another tool.

I'm find some solutions for Nunit using EventListener but i'm using Gallio.
0
Asta
Top achievements
Rank 1
answered on 11 Feb 2013, 07:46 AM
What Gallio have that NUnit don't have?
0
Anton
Top achievements
Rank 1
answered on 11 Feb 2013, 09:12 AM
Asta, I think that it is not place for "holly war".

I have few thousand of tests and Framework written with MbUnit using XML data binding and other feautures.
I thnik swith to NUnit and rewrite framework and tests is not good for first solution.
0
Asta
Top achievements
Rank 1
answered on 11 Feb 2013, 09:40 AM
You can create script whose will refactor the code.
Tags
General Discussions
Asked by
Anton
Top achievements
Rank 1
Answers by
Asta
Top achievements
Rank 1
Anton
Top achievements
Rank 1
Share this question
or