Site Loader
Auckland, New Zealand
Taking screenshot of failures in test cases or unexpected window popup during test execution is very important since we might need them (Screenshot) to understand at the end of long running test execution on why the test case got failed. The screenshots of failures are either stored in a Database tables or as a file in local or remote machine(depending upon your requirement). I have already posted taking screenshot and storing it in database table for Coded UI Test in Visual Studio. 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. The instance of Screenshot class has method to save image in file with specific image format or it also has property to return images a bytearray, which can be used to store image in the database table directly (Instead of writing additional code to convert image as bytearray did in Coded UI Test) Here is the code to store screenshot image in an external file
Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
Here is the code to which returns image as bytearray
Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
byte[] image = file.AsByteArray;
This code will come handy to deal with your test case investigation after test execution. Thanks, Karthik KK

Post Author: Karthik kk

2 Replies to “Taking Screenshot of Page in Selenium”

  1. How to save the screenshot with pagename,please find the below code
    public static void takeSnapShot(String pageName)
    ITakesScreenshot screenshotDriver = BrowserFactory.Driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    String fp = @”C:\Screenshots\LDScreenshots” + “_” + DateTime.Now.ToString(“dd_MMMM_hh_mm_ss_tt”) + “.png”;
    String filename = Path.Combine(pageName,fp);
    LOGGER.Info(“TestCase Failed and Screenshot Captured “);

  2. The taking screenshot works fine, but it does not captures the title of the browser(complete browser screenshot including the web url place holder). Could you please help me in this regard.

Leave a Reply

Your email address will not be published. Required fields are marked *