ExecuteAutomation

Taking Screenshot of Page in Selenium

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();
file.SaveAsFile(@"C:\image.png",ImageFormat.Png);
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