Selenium
WebDriver provides the option to capture screenshots during a test execution.
This is very important when we need to investigate different failures in test
cases or unexpected window popup during test execution or in some cases to
track the different stages of the run. Sometimes we need the screenshots to
understand at the end of long running test execution on why the test case got
failed.We can save the screenshots in a local folder of our computer. In
Selenium Webdriver the screen shots are taken using Screenshot class. The
ITakesScreenshot interface has method called GetScreenshot() which takes
screenshot of the page on the screen.
Here is the
Code to take screenshot of a page.
public class ScreenShotClass { public static string Save(IWebDriver driver, string ScreenShotFileName) { var folderLocation = (@"");//Path of the folder to save the file if (!System.IO.Directory.Exists(folderLocation)) { System.IO.Directory.CreateDirectory(folderLocation); } var screenShot = ((ITakesScreenshot)driver).GetScreenshot(); var fileName = new StringBuilder(folderLocation); fileName.Append(ScreenShotFileName); fileName.Append(DateTime.Now.ToString("_dd-mm-yyyy_mss")); fileName.Append(".jpeg"); screenShot.SaveAsFile(fileName.ToString(), ScreenshotImageFormat.Jpeg); return fileName.ToString(); } } #endregion
Comments
Post a Comment