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

10 posts, 0 answers
  1. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 08 Feb 2013 Link to this post

    Hello.

    I want to customize exception messages when element not found.
    For example i want to write an element path.
    Is it possible?
  2. Asta
    Asta avatar
    29 posts
    Member since:
    Nov 2011

    Posted 08 Feb 2013 Link to this post

    Yes, by using try catch

    try{
    // Some code

                        }
                        catch (NullReferenceException)
                        {
                         s.Write("Element not found"); 
                        }
  3. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 08 Feb 2013 Link to this post

    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);
            }
  4. Asta
    Asta avatar
    29 posts
    Member since:
    Nov 2011

    Posted 08 Feb 2013 Link to this post

    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
  5. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 08 Feb 2013 Link to this post

    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.
  6. Asta
    Asta avatar
    29 posts
    Member since:
    Nov 2011

    Posted 08 Feb 2013 Link to this post

    Be smart.. Use google :)
  7. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 08 Feb 2013 Link to this post

    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.
  8. Asta
    Asta avatar
    29 posts
    Member since:
    Nov 2011

    Posted 11 Feb 2013 Link to this post

    What Gallio have that NUnit don't have?
  9. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 11 Feb 2013 Link to this post

    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.
  10. Asta
    Asta avatar
    29 posts
    Member since:
    Nov 2011

    Posted 11 Feb 2013 Link to this post

    You can create script whose will refactor the code.
Back to Top