0

firstly i must state, that i am new to Selenium testing framework and not very experienced in programming. I need to do client-side web testing and i think selenium is the way to go and i am trying to figure an architecture. At the moment i am struggeling with recovering from an exception.

So i am subscribing to an event, if any error occurs on my Main method:

class Program
{
    static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException += ExceptionHandler.CurrentDomainOnUnhandledException;

        var test = new Login.LoginPageTests();
        test.DoLogin();
    }
}

So if anything is thrown, it logs it and should return to baseURL.

class ExceptionHandler
{
    internal static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
    {
        var screenShot = TestHelpers.TakeScreenShot();
        var stack = new StackTrace();
        var callingMethod = stack.GetFrame(1).GetMethod().Name;
        var e = (Exception)unhandledExceptionEventArgs.ExceptionObject;

        Console.WriteLine("{0} : {1}\nscreenshot: {2}", callingMethod, e.Message, screenShot);
        var driver = TestHelpers.Driver;
        driver.Navigate().GoToUrl(TestHelpers.BaseUrl);
    }
}

But now, let's say i am inside test method and exception is thrown, it still crashes everything. Why is that? How can continue running next test?

EDIT: TestHelpers.Driver is:

internal class TestHelpers
{
    internal const string BaseUrl = @"http://someUrl:8103";

    private static IWebDriver _driver;
    public static IWebDriver Driver
    {
        get
        {
            if (_driver == null)
            {
                _driver = new FirefoxDriver();
                _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
                _driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(30));
            }
            return _driver;
        }
    }
Erki M.
  • 5,022
  • 1
  • 48
  • 74
  • You need to decide about what you can do with an exception, whether you can recover from it, whether you still intend to carry on. Also, invest in using something like NUnit, because you can then separate your tests. In your instance, if it fails during the login process, what you do expect to happen? I'd expect it to fall over and die. If I can't login in, I can't do anything else. – Arran Jan 15 '13 at 10:50
  • Well yes, the failing login is severe case, but on other cases i would like to move to main page and start with next test. What i have found out from here: http://stackoverflow.com/questions/186854/how-to-prevent-an-exception-in-a-background-thread-from-terminating-an-applicati AppDomain.UnhandledException ... provides no means of preventing the application from shutting down. The exceptions also have non-settable IsTerminating flag set. Does that mean that i have to catch all the exceptions that i can think of? What I wanted was something like one method to handle all without try/catch. – Erki M. Jan 16 '13 at 09:59

1 Answers1

1

as far as i know, Nunit has something similar to Junit's TestWatcher, google for this.