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);
        fileInfo.CopyTo(destinationPath);
    }

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 :)

Tagged:

Answers

  • 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.

    https://github.com/jamesmontemagno/MediaPlugin/pull/489

  • 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))
            {
                Directory.CreateDirectory(dirToSave);
            }
            //override current directory context
            Directory.SetCurrentDirectory(dirToSave);
    
        }
    

    After that any call to:
    App.Screenshot();
    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");
    if(File.Exists(filePath))
    {
    //delete old file, if exist
    File.Delete(filePath);
    }
    //move file with new name
    fi.MoveTo(filePath);

    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.