Re: [selenium-users] How to get snapshot only when Nunit test fail?

2,102 views
Skip to first unread message

Lutfi Dughman

unread,
Apr 14, 2011, 7:23:33 PM4/14/11
to seleniu...@googlegroups.com
ok, do this, this is code in java but stil you will get the concept


try {
     Assert.That(driver.Title, Is.EqualTo(titreDuSiteWeb));
} catch(AssertionError e) {
     driver.GetScreenshot().SaveAsFile("c:\test.jpg",ImageFormat.Jpeg);
}

if it doesn't fail, the code in the catch statement will not be executed.

This is NOT th eoptimal solution, because this will only apply to this tests, what if you have more tests and you want do the same thing to them too??? ahh, if you wanna know that you will have to ask for it ;)


On Thu, Apr 14, 2011 at 2:33 PM, jobou <jonatan...@gmail.com> wrote:
Hi,

I want to get a snapshot where a Assert fail ?

I dont find any way to do that functionnality?

       [Test]
       public void VerifierQueLeSiteWebSaiAdNetEstAccessible()
       {
           driver.Navigate().GoToUrl(webPageLogin);

           ShowPageTitle();
           const string titreDuSiteWeb = "SaiAdNet - Identification";
           Assert.That(driver.Title, Is.EqualTo(titreDuSiteWeb));
           driver.GetScreenshot().SaveAsFile("c:\test.jpg",
ImageFormat.Jpeg);
       }

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


jobou

unread,
Apr 14, 2011, 2:33:29 PM4/14/11
to Selenium Users

Daniel Wagner-Hall

unread,
Apr 15, 2011, 10:07:58 PM4/15/11
to Selenium Users
If you are using NUnit 2.5.7+, you could do this with a TearDown
method, along the lines of:

[TearDown]
public void SnapshotOnFailure()
{
if (TestContext.Status == TestStatus.Failed)
{
driver.GetScreenshot().SaveAsFile(...);
}
}

This will take a snapshot after any test in the same class as the
method fails. You can add it to a common superclass of your tests to
apply it to all of your tests.

You can access the name of the test that was running with the
TestContext.TestName property (e.g. for file names).

Lutfi Dughman

unread,
Apr 15, 2011, 10:46:52 PM4/15/11
to seleniu...@googlegroups.com
yes, but he only wants to take a snapshot when the test fails.

Daniel Wagner-Hall

unread,
Apr 16, 2011, 11:13:49 PM4/16/11
to Selenium Users
Yes, the "if (TestContext.Status == TestStatus.Failed)" check ensures
that no action is taken if the test passed...

On Apr 16, 3:46 am, Lutfi Dughman <lutf...@gmail.com> wrote:
> yes, but he only wants to take a snapshot when the test fails.
>

Lutfi Dughman

unread,
Apr 17, 2011, 12:05:49 AM4/17/11
to seleniu...@googlegroups.com
oh right, should pay more attention to the code next time.

David Lai

unread,
Apr 4, 2014, 1:24:58 PM4/4/14
to seleniu...@googlegroups.com, dawa...@gmail.com
I'm wondering if there's a way I can do this using a base test.  But problem is a base tests's tear down runs after the subclass's teardown.  So there could be some mucking around before it gets run.  I'm looking for something equivalent to JUnits TestWatcher/TestRule, and event listener that'll fire right at point of falure.

erki manniste

unread,
Apr 5, 2014, 2:10:26 PM4/5/14
to seleniu...@googlegroups.com, dawa...@gmail.com
You have to implement a nunit addin. Check here :  http://www.nunit.org/index.php?p=nunitAddins&r=2.5.10 .Try something like:


using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using NUnit.Core;
using NUnit.Core.Extensibility;
using OpenQA.Selenium;
 
namespace WebDriverTest.Helpers
{
    [NUnitAddin(Name = "Test watcher")]
    public class TestWatcher : IAddin, EventListener
    {
        private IWebDriver _driver;
        public bool Install(IExtensionHost host)
        {
            IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
            if (listeners == null)
                return false;
 
            listeners.Install(new TestWatcher());
            return true;
        }
 
 
        public void RunFinished(Exception exception)
        {
        }

............... skipped methods.....
 
        public void TestFinished(TestResult result)
        {
            if (result.IsError)
            {
                var screenshot = ((ITakesScreenshot) _driver).GetScreenshot();
                var file = string.Format("{0}_{1}.png", result.Name, DateTime.Now.ToFileTimeUtc());
                screenshot.SaveAsFile(Path.Combine(TestHelpers.Path, file), ImageFormat.Png);
            }
        //    new TestHelpers().Close();
        }
 
        public void UnhandledException(Exception exception)
        {
            Debug.WriteLine("OMG, caught unhandled exception: {0}", exception);
        }
    }
}

.........

BR,
erki.

Ričardas Šliapikas

unread,
May 22, 2015, 1:14:44 AM5/22/15
to seleniu...@googlegroups.com, dawa...@gmail.com
How do you get reference to failed test's WebDriver? In your example WebDriver will always be null.

Jgrabow1985

unread,
Oct 20, 2016, 2:27:41 PM10/20/16
to Selenium Users, dawa...@gmail.com
Yes I have the same question... how can you use webdriver to take a screenshot after the failure event?
Reply all
Reply to author
Forward
0 new messages