0

I have a selenium automation suite and the test cases has the capability to take a screenshot if a test fails.

All the test are unit tests and are datadriven. What I meant by data driven is that the test gets executed for each of the data row. please see sample code below

    [TestMethod]
    [TestCategory("UITest"), TestCategory("Flow")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\allFlows.xml", "flow", DataAccessMethod.Sequential)]
    public void MyTest()
    {
       //TEST CODE
    }

The above code is datadriven by the number of dataflows in allFlows.xml

The issue I am facing is when a test fails,a screenshot gets generated and I am able to see the screenshot attached to each of the test ( including the pass tests) I believe this is making the trx file heavy and ugly

Say for example I have 1 tests there is 5 flow data in allFlow.xml. So the total test executed is 5 and lets say that 2 tests failed.

The report says that 3 test passed and 2 test failed. I have now 2 screenshots for two failed tests.

The issue is the screenshot is getting attached to all the 3 passed test in addition to the 2 failed test.

Any idea how to resolve this issue? Do I need to live with it as it is a microsoft feature/ bug??

The issue is similar to this C# - .NET - MSTest - Test context - Add result file - Selenium Screenshots - Issue while viewing the result file

and I remember a similar issue was raised by someone and there was no resolution for this?

Is this a MSTest feature?

Any help would be great.

SCREENSHOT CODE IS BELOW

                Directory.CreateDirectory(testContext.TestResultsDirectory);
                Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
                String CurrentIteration = testContext.DataRow.Table.Rows.IndexOf(testContext.DataRow).ToString();
                string fileName = testContext.TestResultsDirectory+ "\\Screenshot_" + testContext.TestName + "_RowNumber " + CurrentIteration + "_" + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss") + ".png";
                screenShot.SaveAsFile((fileName), ImageFormat.Png);
                Wait.WaitBetweenSteps(3000);  

Thanks

*********************************EDIT**********************************

 [TestClass]
public class Driver
{
    IWebDriver driver;
    private TestContext testContext;
    public TestContext TestContext
    {
        get { return testContext; }

        set { testContext = value; }
    }


    [ClassInitialize]
    public static void ClassInit(TestContext testContext)
    {
        DriverData driverData = new DriverData();
        driverData.CreateTestData();
    }

    [TestInitialize]
    public void Init()
    {
    //Some Init Code
    }

    [TestMethod]
    [TestCategory("UITest"), TestCategory("Flow")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\allFlows.xml", "flow", DataAccessMethod.Sequential)]
    public void MyTest()
    {
       //TEST CODE
        Flow flow = new TestDataUtilities().generateFlowFromDataRow(testContext.DataRow);
       //Here if Failed then call GetScreenShot(testContext);
       try{
       //TEST CODE
       }
       catch(Exception){
       GetScreenShot(testContext);
       }
    }
}
            GetScreenShot(testContext)
            {
            Directory.CreateDirectory(testContext.TestResultsDirectory);
            Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
            String CurrentIteration = testContext.DataRow.Table.Rows.IndexOf(testContext.DataRow).ToString();
            string fileName = testContext.TestResultsDirectory+ "\\Screenshot_" + testContext.TestName + "_RowNumber " + CurrentIteration + "_" + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss") + ".png";
            screenShot.SaveAsFile((fileName), ImageFormat.Png);
            Wait.WaitBetweenSteps(3000);  
            }       
Community
  • 1
  • 1
Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62
  • You're not sharing your code. Where do you capture the screen? – derloopkat Aug 01 '16 at 11:25
  • @derloopkat - Thanks for your comment. Have added the screenshot code. – Timothy Rajan Aug 01 '16 at 11:45
  • Sorry, I don't mean how but the location of that code. – derloopkat Aug 01 '16 at 11:50
  • ok...the location of the code is in another class file by name utilties.cs and I have a method getscreenshot() which gets the screenshot. I pass the testcontext to the getscreenshot method. I hope I have given you the info. Else, please let me know. Once again a very big thanks for helping me. – Timothy Rajan Aug 01 '16 at 11:56
  • The problem is we need to see your code. Stackoverflow rules says questions asking debugging should include the minimum code for reproducing the problem. In that specific case, I'd check where context object is setting TestName. If this value is not updated and context is a field then you'd be getting these symptoms. – derloopkat Aug 01 '16 at 12:11
  • @derloopkat - Thanks again. Have modified the question with the max input which I can give. Thanks again for your patience and help. – Timothy Rajan Aug 01 '16 at 12:55
  • @derloopkat - Also, I am not setting any TestName in any of my code. I am just saving the file in the testContext.TestResultsDirectory – Timothy Rajan Aug 01 '16 at 13:01

0 Answers0