How to get Exception message thrown by one of the specflow scenario's methods

I am using specflow for automating my test. As expected, when any of the [Given-when-then] methods say do not find and element, they throw exception. I want to get the error message of this exception in [afterscenario] method called after each scenario. Is this possible?

for example below is the error message I need to catch

Answers


You are looking for ScenarioContext.Current.TestError. I tried looking through the SpecFlow documentation, but I think I found this property by just drilling down into the ScenarioContext.Current property using Intellisense in Visual Studio.

using TechTalk.SpecFlow;

namespace Your.Project
{
    [Binding]
    public class CommonHooks
    {
        [AfterScenario]
        public void AfterScenario()
        {
            Exeception lastError = ScenarioContext.Current.TestError;

            if (lastError != null)
            {
                if (lastError is OpenQA.Selenium.NoSuchElementException)
                {
                    // Test failure cause by not finding an element on the page
                }
            }
        }
    }
}

Need Your Help

How can I cancel a paypal recurring payment from my website?

paypal paypal-sandbox paypal-subscriptions

I'm currently integrating the paypal recurring payment process in my website (thanks to https://www.paypal.com/uk/cgi-bin/webscr?cmd=_pdn_subscr_techview_outside) and, for the moment, it works.

Apply background-color in ckeditor textarea

javascript css ckeditor

I'm having some problems to apply a background-color in the textarea of a ckeditor instance.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.