1

I have built a Xamarin.UITest suite and I'm testing locally on a real Android device. I notice that one or two tests fail, but not necessarily the same tests. Whenever I try to re-run such tests to see why they're failing, they pass! Therefore I need to add functionality to see what's on the screen when a test fails. I don't seem to be able to find a guide specifically for this - just bits and pieces...

I've added EnableLocalScreenshots() to my StartApp() call, but I'm not sure of the next steps. So I have some questions:

  1. Do I need to specify the location of where the screenshots are saved, or is this done automatically?

  2. Do I need to explicitly write code to take a screenshot when there's an AssertionException, or is this what the screenshot functionality does by default?

  3. If I need to write code to take a screenshot when there's an AssertionException, please can you point me to an example of this code so I can add it to my suite?

Thank you

EDIT: Re: Take screenshot on test failure + exceptions

I have tried the following in my Hooks.cs file:

[OneTimeTearDown]
public void OneTimeTearDown()
{
    if (TestContext.CurrentContext.Result.Outcome == ResultState.Error || TestContext.CurrentContext.Result.Outcome == ResultState.Failure)
    {
        App.Screenshot(TestContext.CurrentContext.Test.Name);
    }
}

Upon debugging this method, I find that it is never called. Not even if I use TearDown and AfterScenario attributes. So great - that Intellisense likes my code, bad - because it never gets called. It shouldn't be this hard!

I am using Specflow in this suite, could that be anything to do with why I'm getting this issue? This is why I can't implement the solution on the above thread as follows because Specflow manages the NUnit tests...

UITest(() =>
{
    // Do your test steps here, including asserts etc.
    // Any exceptions will be caught by the base class
    // and screenshots will be taken
});
Zuno
  • 433
  • 1
  • 7
  • 17
  • Possible duplicate of [Take screenshot on test failure + exceptions](https://stackoverflow.com/questions/33320912/take-screenshot-on-test-failure-exceptions) – pix Sep 11 '19 at 09:56

1 Answers1

2

Ok so for me this was the answer.

Added [Binding] attribute above class (dumb error on my part)...

[Binding]
    class TestInitialise : BasePage

Added [AfterScenario()] above my screenshot method, and made it a static method...

[AfterScenario()]
public static void TakeScreenshot()

Specified where to save the screenshot...

App.Screenshot(TestContext.CurrentContext.Test.Name).CopyTo(@"C:\Users\me\Desktop\" + TestContext.CurrentContext.Test.Name + ".png");

So I'm guessing App.Screenshot(string title) simply takes a screenshot and holds it in memory, but you actually need to save it somewhere explicitly to get it, rather than assuming it just saves to a default location.

That's that then!

Zuno
  • 433
  • 1
  • 7
  • 17