Marko Jovanović
Marko Jovanović

Reputation: 2664

Xamarin.UITest Screenshot location

I've got a problem with Xamarin.UITest, specifically screenshot feature. It is not working as expected.

I'm trying to copy "created" screenshot to another directory, but I get the following error:

Message: System.IO.FileNotFoundException : Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\screenshot-1.png'.

I'm using this piece of code to copy image file:

var screen = app.Screenshot("Welcome screen.");
screen.CopyTo(@"C:\Users\someuser\Desktop\screenshotTest.png");

How to specify first path/location for screenshots, because the original path probably needs admin privileges, that I don't have.

Upvotes: 5

Views: 2990

Answers (5)

gords
gords

Reputation: 61

My screenshots are saving to C:\Users\username\AppData\Local\Temp

Try this code

[TearDown]
public void Teardown()
{
   SaveScreenshotIfTestFails();
}

private void SaveScreenshotIfTestFails()
{
   if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
   {
      var testName = TestContext.CurrentContext.Test.Name;
      var filename = $"{testName}.png";
      var file = app.Screenshot(testName);
      var dir = file.DirectoryName;
      File.Delete(dir + "\\" + filename);
      file.MoveTo($"{testName}.png");
   }
}

Upvotes: 0

Amartya Deshmukh
Amartya Deshmukh

Reputation: 72

Use MoveTo() insted of CopyTo().

var screenshot = app.Screenshot($"{DateTime.Now}_{platform}");
screenshot.MoveTo($@"{Destination}\{screenshot.Name}.{screenshot.Extension}");

Upvotes: 0

Roy John
Roy John

Reputation: 29

Screenshots are saved to the Current Directory. Change it via Directory.SetCurrentDirectory.

Upvotes: -1

Blake
Blake

Reputation: 61

Screenshots saved with App.Screenshot() are located in your test project's directory: MyTestProject"\bin\Debug folder where the first screenshot is named screenshot-1.

Upvotes: 6

Marko Jovanović
Marko Jovanović

Reputation: 2664

Half solution to the problem: I downgraded NUnit from 3.11.0 to 2.7.0, so it works OK.

Upvotes: 3

Related Questions