Beispiel #1
0
        /// <summary>
        /// Take screenshot of the currently open page.
        /// </summary>
        /// <param name="screenshotFileName">Screenshot's full name.</param>
        /// <param name="format">Image format.</param>
        /// <remarks>There is a known issue with taking screenshots in IE:
        /// https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/7256#issuecomment-192176024
        /// Further reading:
        /// http://jimevansmusic.blogspot.com/2014/09/screenshots-sendkeys-and-sixty-four.html
        /// </remarks>
        public void SaveAs(string screenshotFileName, ScreenshotImageFormat format = ScreenshotImageFormat.Jpeg)
        {
            Logger.Exec(Logger.LogLevel.Debug, screenshotFileName, format);
            var fInfo = new FileInfo(screenshotFileName);

            // Update the name with a timestamp and a driver type.
            screenshotFileName = UpdateFilename(fInfo.Name);
            // Replace invalid path chars.
            var pathChars     = Path.GetInvalidPathChars();
            var stringBuilder = new StringBuilder(screenshotFileName);

            foreach (var item in pathChars)
            {
                stringBuilder.Replace(item, '.');
            }
            screenshotFileName = stringBuilder.ToString();
            // Create a directory if necessary.
            if (!fInfo.Directory.Exists)
            {
                fInfo.Directory.Create();
            }
            // Save the screenshot.
            _screenshot.SaveAsFile(
                $"{Path.Combine(fInfo.DirectoryName, screenshotFileName)}.{format.ToString().ToLower()}", format);
        }
Beispiel #2
0
        private static void TakeScreenshotPrivate(string strFilename)
        {
            var objRectangle = Screen.PrimaryScreen.Bounds;
            var objBitmap    = new Bitmap(objRectangle.Right, objRectangle.Bottom);
            var objGraphics  = System.Drawing.Graphics.FromImage(objBitmap);
            var hdcSrc       = GetDC(0);
            var hdcDest      = objGraphics.GetHdc();

            BitBlt(hdcDest.ToInt32(), 0, 0, objRectangle.Right, objRectangle.Bottom, hdcSrc, 0, 0, 0xcc0020);
            objGraphics.ReleaseHdc(hdcDest);
            ReleaseDC(0, hdcSrc);

            var strFormatExtension = ScreenshotImageFormat.ToString().ToLower();

            if (Path.GetExtension(strFilename) != ("." + strFormatExtension))
            {
                strFilename = strFilename + "." + strFormatExtension;
            }

            if (strFormatExtension == "jpeg")
            {
                BitmapToJpeg(objBitmap, strFilename, 80L);
            }
            else
            {
                objBitmap.Save(strFilename, ScreenshotImageFormat);
            }

            _strScreenshotFullPath = strFilename;
        }
 public static string GetExtension(this ScreenshotImageFormat format)
 {
     if (format == ScreenshotImageFormat.Jpeg)
     {
         return(".jpg");
     }
     else
     {
         return(format.ToString().ToLower().Prepend("."));
     }
 }
        public string TirarPrint(string nomeArquivo = "screen", ScreenshotImageFormat formatoImagem = ScreenshotImageFormat.Jpeg)
        {
            var projectPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            nomeArquivo = nomeArquivo + "." + formatoImagem.ToString().ToLower();
            var fileLocation = Path.Combine(projectPath, nomeArquivo);

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

            ss.SaveAsFile(fileLocation, ScreenshotImageFormat.Png);
            return(fileLocation);
        }
Beispiel #5
0
        /// <summary>
        /// Saves a screenshot to a file. Overwrites the already existing file and creates non-existent directories.
        /// </summary>
        /// <param name="fileName">The file name to save the screenshot to. May include relative or absolute path.</param>
        /// <param name="format">A format to save the image to.</param>
        /// <returns>An instance of this class.</returns>
        public UIPage SaveScreenshot(string fileName, ScreenshotImageFormat format = ScreenshotImageFormat.Png)
        {
            if (!Path.HasExtension(fileName))
            {
                fileName = Path.ChangeExtension(fileName, format.ToString().ToLower());
            }
            if (!Path.IsPathRooted(fileName))
            {
                fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fileName);
            }
            Directory.CreateDirectory(Path.GetDirectoryName(fileName));

            Driver.GetScreenshot().SaveAsFile(fileName, format);

            return(this);
        }
Beispiel #6
0
        public static void PrintScreen(string fileName, ScreenshotImageFormat imageFormat, string path = null)
        {
            if (String.IsNullOrEmpty(path))
            {
                path = ConfigurationManager.AppSettings["DefaultImagePath"];
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var        file = path + fileName + "." + imageFormat.ToString();
            Screenshot ss   = ((ITakesScreenshot)driver).GetScreenshot();

            ss.SaveAsFile(file);
        }
Beispiel #7
0
        /// <summary>
        /// To capture a screenshot during execution
        /// </summary>
        /// <param name="webDriver">The WebDriver</param>
        /// <param name="directory">The directory file path</param>
        /// <param name="fileNameWithoutExtension">Filename without extension</param>
        /// <param name="imageFormat">Optional Screenshot Image format parameter; Default imageFormat is PNG</param>
        /// <returns>Path to the log file</returns>
        public static string CaptureScreenshot(this IWebDriver webDriver, string directory, string fileNameWithoutExtension, ScreenshotImageFormat imageFormat = ScreenshotImageFormat.Png)
        {
            Screenshot screenShot = ((ITakesScreenshot)webDriver).GetScreenshot();

            // Make sure the directory exists
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // Calculate the file name
            string path = Path.Combine(directory, fileNameWithoutExtension + "." + imageFormat.ToString());

            // Save the screenshot
            screenShot.SaveAsFile(path, imageFormat);

            return(path);
        }
Beispiel #8
0
        public static string TakeScreenshot(string x)
        {
            var feature  = FeatureContext.Current.FeatureInfo.Title.ToString().Trim().Replace(" ", "");
            var scenario = ScenarioContext.Current.ScenarioInfo.Title.ToString().Trim().Replace(" ", "");

            var tempo            = string.Format("{0:dd-MM-yyyy-hh-mm-ss}", DateTime.Now);
            var dateTime         = GeneralHelpers.GetCurrentDate("dd-MM-yyyy");
            var localArquivoBase = AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "Evidências");

            localArquivoCompleto = string.Format("{0}\\{1}\\{2}\\{3}", localArquivoBase, feature, scenario, dateTime);

            if (!Directory.Exists(localArquivoCompleto))
            {
                Directory.CreateDirectory(localArquivoCompleto);
            }

            ScreenshotImageFormat extensaoArquivo = ScreenshotImageFormat.Jpeg;

            if (numProposta != null)
            {
                arquivos.AddRange(Directory.GetFiles(localArquivoCompleto));

                var nome = localArquivoCompleto + "\\" + numProposta + ".Jpeg";
                if (arquivos.Contains(nome))
                {
                    fileName = null;
                }
                else
                {
                    fileName = numProposta;
                }
            }
            else
            {
                List <string> arquivos = new List <string>(Directory.GetFiles(localArquivoCompleto));
                var           nome     = localArquivoCompleto + "\\" + "Print" + tempo + ".Jpeg";
                if (arquivos.Contains(nome))
                {
                    fileName = null;
                }
                else
                {
                    fileName = "Print" + tempo;
                }
                arquivos.Clear();
            }

            if (fileName != null)
            {
                nomeArquivoCompleto = string.Format("{0}\\{1}.{2}", localArquivoCompleto, fileName, extensaoArquivo.ToString());

                try
                {
                    ((ITakesScreenshot)DriverFactory.INSTANCE)
                    .GetScreenshot()
                    .SaveAsFile(nomeArquivoCompleto, extensaoArquivo);
                }
                catch (Exception ex)
                {
                    var mensagemErro = string.Format("{0}\r\n{1}\r\n\r\n{2}",
                                                     "Ocorreu um erro ao tirar o print da evidência.",
                                                     ex.Message,
                                                     ex.StackTrace);

                    Console.WriteLine(mensagemErro);
                }
            }

            return(nomeArquivoCompleto);
        }
Beispiel #9
0
        public static string MakeScreenShot(IWebDriver driver, string testName = "screen", ScreenshotImageFormat imageFormat = ScreenshotImageFormat.Png)
        {
            var projectPath  = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var screen       = ((ITakesScreenshot)driver).GetScreenshot();
            var fileLocation = $"{projectPath}{Path.DirectorySeparatorChar}{testName}.{imageFormat.ToString().ToLower()}";

            screen.SaveAsFile(fileLocation, imageFormat);
            return(fileLocation);
        }
 private static bool AreEqual(string extension,
                              ScreenshotImageFormat imageFormat)
 {
     return(extension.Equals(imageFormat.ToString(), StringComparison.InvariantCultureIgnoreCase));
 }
Beispiel #11
0
 /// <summary>
 /// Formata o nome do arquivo
 /// </summary>
 /// <param name="nomeArquivo">Nome do arquivo</param>
 /// <param name="pasta">O caminho da pasta, onde ele vai baixado</param>
 /// <returns>Retorna uma string com o nome do arquivo formatado</returns>
 public static string FormatFileName(string nomeArquivo, string pasta, ScreenshotImageFormat format)
 {
     return($"{FormatName(nomeArquivo, pasta)}_{GetCurrentDateTime()}.{format.ToString().ToLower()}");
 }
Beispiel #12
0
 public static string GetExtension(this ScreenshotImageFormat format)
 {
     return(format == ScreenshotImageFormat.Jpeg
         ? ".jpg"
         : format.ToString().ToLowerInvariant().Prepend("."));
 }