Search code examples
c#visual-studioseleniummstesttestcontext

c# - selenium - MSTest - Unable to add a result file ( using testcontext.AddResultFile ) to the report using testcontext


I am using MSTest - C# - Selenium to run a suite of tests. I am taking a screenshot if there is a failed step and wish to upload ( attach ) it with the trx file,

All is good when I run it as a single test of a couple of tests and the results are with the attachment.

But when I run the tests in Parallel, I am not able to see the result file with the attachment even though the test is FAILED

I get the below in the trx file

TestContext Messages: Value cannot be null. Parameter name: path

The code I am using to attach the file to the trx file is

Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
string fileName = fullFilePath + "Screenshot_" + driver.testContext.TestName + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss")+".png";
screenShot.SaveAsFile((fileName), ImageFormat.Png);                
driver.testContext.AddResultFile(fileName);

Any pointers where I am going wrong. I saw a couple of links while googling and they mention this is a known issue. Is there something I can do as a work around to circumvent this issue.

Any pointers will be very helpful. Thanks


Solution

  • My 2 cents...

    The below code solved the issue...

                Directory.CreateDirectory(MyWebDriver.testContext.TestResultsDirectory);
                Screenshot screenShot = ((ITakesScreenshot)MyWebDriver).GetScreenshot();
                string fileName = MyWebDriver.testContext.TestResultsDirectory+"\\Screenshot_" + MyWebDriver.testContext.TestName + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss")+".png";
                screenShot.SaveAsFile((fileName), ImageFormat.Png);                
    

    Please note that MyWebDriver is an extension of Selenium WebDriver tailored for my project requirements.

    Refer the below link for the cause of this issue.

    https://connect.microsoft.com/VisualStudio/feedback/details/1062039/value-cannot-be-null-parameter-name-path-in-test-result-when-running-tests-in-parallel-and-adding-files-to-the-test-context-using-addresultfile