Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
2.1k views
by Contributing Tester (48 points)
edited by
I am using C# Selenium Web Driver with Visual Studio 2015, NUnit test freamwork

I want to take screenshot with the name of TestMethod name only when Test method FAIL, But when I used TestMethid Name ( NUnit.Framework.TestContext.CurrentContext ),  will not found any screenshot in machine.

    //Call when TestMethid End:

  [OneTimeTearDown]

        public void TearDown()

        {

            var currentContext = NUnit.Framework.TestContext.CurrentContext;

            if (currentContext.Result.Outcome != NUnit.Framework.Interfaces.ResultState.Success)

            {

                var testName = currentContext.Test.Name;

                String filename = "Screenshots\\" + testName + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".png";

                Console.WriteLine("filename: " + filename);

                log.Info("filename: " + filename);

                UtilityHelper.TakeScreenshot(_driver, filename);

            }

}

//Screenshot Mwthod:

  public static String TakeScreenshot(IWebDriver driver, String filename)

        {

             // Take screenshot

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();

            // Create directory if doesn't exist

            Directory.CreateDirectory(Path.GetDirectoryName(filename));

            // Save file            

            ss.SaveAsFile(filename, System.Drawing.Imaging.ImageFormat.Png);

            Log.InfoFormat("Screenshot saved at: {0}", filename);

            return filename;

        }

Please help me.

1 Answer

0 like 0 dislike
by Contributing Tester (48 points)
 
Best answer

I hav eused Below code:

 private TestContext _context = null;
        public TestContext Context
        {
            get
            {
                return this._context;
            }
            set
            {
                this._context = value;
                DriverUtils.Instance.Context = this._context;
            }
        }

 

Context.FullyQualifiedTestClassName}.{Context.TestName}

by Contributing Tester (48 points)
It is resolved :-)
...