Specify Screenshot name and a path UITest

brajovickbrajovick Member
edited June 2018 in General

Hi, if anyone could help with this issue. Trying to name the screenshot and specify the saving path but it's not working. I appreciate all suggestions.

    public void TakeSS()
        string name = TestContext.CurrentContext.Test.Name.ToString();
        string[] name1 = name.Split('(');
        name = name1[0];
        FileInfo fileInfo = app.Screenshot(name + "_" + string.Format("{0:dd-MM-yyyy_HH-mm-ss}", DateTime.Now));
        string destinationPath = string.Format(@baseDir + "Report\\{0}", fileInfo.Name);

It's always naming the screenshot like "screenshot-1.png" , so this line "app.Screenshot(name + "_" + string.Format("{0:dd-MM-yyyy_HH-mm-ss}", DateTime.Now));" is not doing I thought it would do. Also it's not saving screenshots (app.Screenshot) and test fails while trying to copy not existing file...
I've already added .EnableLocalScreenshots() in AppInitializer and don't have anything on my mind why it won't save the screenshot.
Any guesses? Thanks :)



  • seanydaseanyda GBMember ✭✭✭✭✭

    I would suggest looking at this PR on the Media Plugin for screenshots. It has all the logic you need to pull this off.


  • inikityukinikityuk Member
    edited November 2018

    My way of doing that is to override CurrentDirectory path.

    public void SetPathForLocalScreenshots()
    //if run in a cloud, not our problem to look after screenshots
    if (TestEnvironment.IsTestCloud) return;

            //relative path where we would like to save screenshots
            var relativePathToSave = "ScreenshotsFolder";
            var dirToSave = Path.Combine(Directory.GetCurrentDirectory(), relativePathToSave);
        //make sure path exist, otherwise exception
            if (!Directory.Exists(dirToSave))
            //override current directory context

    After that any call to:
    will save screenshots to your desired location.

    NOTE: you still need to rename them, if you passing screenshot name, something like that:

    //for local run, save screenshot
    var fi = App.Screenshot(title);
    //new name for screenshot
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"{title.Replace(" ", "_")}.png");
    //delete old file, if exist
    //move file with new name

    NOTE: Directory.GetCurrentDirectory() -->> will return your new path here, because you called " Directory.SetCurrentDirectory(dirToSave);" before that.

  • Thank you @inikityuk !

Sign In or Register to comment.