Beispiel #1
0
        /// <summary>
        /// Takes a screenshot of the given WebDriver's current page.
        /// </summary>
        /// <returns>The string of the file path for the screenshot taken.</returns>
        public static string TakeScreenshot()
        {
            // Declare an empty object to hold the return value
            string returnValue = null;

            // Log Before Action
            Log.BeforeAction();

            // Perform the action
            try
            {
                // Get the screenshot folder, set in TestBase.Setup() using App.config value
                string screenshotFolder = Context.Get("screenshotFolder").ToString();
                // Create directory (if it does not exist)
                string date      = DateUtilities.GetCurrentDate();
                string directory = screenshotFolder + date;
                if (screenshotFolder.Contains("/"))
                {
                    directory = directory + "/";
                }
                else
                {
                    directory = directory + "\\";
                }
                // Create a sub-directory using today's date
                Directory.CreateDirectory(directory);
                // Build a file name (*NOTE: The folder must already exist and allow writes to it)
                string time = DateUtilities.GetCurrentTime();
                returnValue = directory + time + ".jpg";
                // Take a screenshot
                Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
                screenshot.SaveAsFile(returnValue, ScreenshotImageFormat.Jpeg);
                // Logging - After action success
                Log.Success("File Name: " + returnValue);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                //Assert.Fail(e.Message); // Do not fail the test if a screenshot can not be saved
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
            // Return the filename of the screenshot
            return(returnValue);
        }