Save Screenshot to folder in root directory VSTS

259 Views Asked by At

I have a simple function that takes screenshots while running Xunit test. The test is straight forward.

 ITakesScreenshot screenshotHandler = PropertiesCollection.driver as ITakesScreenshot;
            Screenshot screenshot = screenshotHandler.GetScreenshot();
            screenshot.SaveAsFile(@"C:\Users\Slim\Screenshots\" + filename + ".png", ImageFormat.Png);

And it works, but we are using VSTS and when somebody else is using the test, it breaks since the path is no longer valid. C:\Users\Slim\Screenshots\ It is possible to change the path of the code to the path where you have the files locally but that is not good practice of course :)

I have tried to use AppDomain.CurrentDomain.BaseDirectory but with no luck.

ITakesScreenshot screenshotHandler = PropertiesCollection.driver as ITakesScreenshot;
            Screenshot screenshot = screenshotHandler.GetScreenshot();
            screenshot.SaveAsFile(AppDomain.CurrentDomain.BaseDirectory + //Screenshots//" + filename + ".png", ImageFormat.Png);

Any good advice's?

1

There are 1 best solutions below

0
On

You can take a look at the SpecialFolders enumeration.

It handles the access to paths such as Program Data, Program Files, App Data, etc. And the users accounts are correctly handled if you use a folder that is user specific, such as LocalApplicationData or RoamingApplicationData, which respectively map to C:\users\MyUser\AppData\Local and C:\users\MyUser\AppData\Local\Roaming.

On the opposite side, you have for instance CommonApplicationData which usually maps to C:\ProgramData, which is common for all users.

So, to sum up, you could do something like:

string myFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
screenshot.SaveAsFile(Path.Combine(myFolder, filename + ".png"), ImageFormat.Png);