In Following article we are going to see how to take screenshot in selenium webdriver. I am using c# with VS2010.
Screenshot is a very important factor while testing in selenium. When ever any important functions need to be tested, we should take screenshot after testing . We may use this to take screenshot after fail a test case or, to validate a test from previous reference or, to keep log of test results. lets start.
[Please follow my introductory selenium setup post to know about the basic selenium setup.]
Step 1 : After initializing driver , make a screenshot type variable (myScreen).
private Screenshot myScreen;
Step 2 : In the test method, After a test, assign myScreen by Casting the driver with ITakesScreenshot interface and call GetScreenshot()
myScreen = ((ITakesScreenshot)driver).GetScreenshot();
Step 3: Save the screenshot that we have taken.
myScreen.SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
So, For better understanding , I am pasting my full test code here
[Test]
public void TestHome()
{
driver.Navigate().GoToUrl(path);
Assert.AreEqual("Software Development Outsourcing. Offshore Software Development Company - Kaz Software", driver.Title);
myScreen = ((ITakesScreenshot)driver).GetScreenshot();
myScreen.SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Note : We can skip step1, 2 and 3 by doing all together inside the test method.
((ITakesScreenshot) driver).GetScreenshot().SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
After the test method execution, my screenshot was found in f:\
In here we have used the
1. GetScreenshot() - takes no parameter but returns a OpenQA.Selenium.Screenshot object containing the image.
2. SaveAsFile("FullPathWithFileName", [System.Drawing.Imaging.ImageFormat object indicating file format]) -Takes two parameter but gives no output.
A Screenshot type object(myScreen from step 1) have extra following property
-myScreen.AsByteArray
We can use it to get the value of the screenshot image as an array of bytes.
-myScreen.AsBase64EncodedString
We can use it to get current System.Object as String (System.String type)
....Thanks...:)...
Screenshot is a very important factor while testing in selenium. When ever any important functions need to be tested, we should take screenshot after testing . We may use this to take screenshot after fail a test case or, to validate a test from previous reference or, to keep log of test results. lets start.
[Please follow my introductory selenium setup post to know about the basic selenium setup.]
Step 1 : After initializing driver , make a screenshot type variable (myScreen).
private Screenshot myScreen;
Step 2 : In the test method, After a test, assign myScreen by Casting the driver with ITakesScreenshot interface and call GetScreenshot()
myScreen = ((ITakesScreenshot)driver).GetScreenshot();
Step 3: Save the screenshot that we have taken.
myScreen.SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
So, For better understanding , I am pasting my full test code here
[Test]
public void TestHome()
{
driver.Navigate().GoToUrl(path);
Assert.AreEqual("Software Development Outsourcing. Offshore Software Development Company - Kaz Software", driver.Title);
myScreen = ((ITakesScreenshot)driver).GetScreenshot();
myScreen.SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Note : We can skip step1, 2 and 3 by doing all together inside the test method.
((ITakesScreenshot) driver).GetScreenshot().SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
After the test method execution, my screenshot was found in f:\
In here we have used the
1. GetScreenshot() - takes no parameter but returns a OpenQA.Selenium.Screenshot object containing the image.
2. SaveAsFile("FullPathWithFileName", [System.Drawing.Imaging.ImageFormat object indicating file format]) -Takes two parameter but gives no output.
A Screenshot type object(myScreen from step 1) have extra following property
-myScreen.AsByteArray
We can use it to get the value of the screenshot image as an array of bytes.
-myScreen.AsBase64EncodedString
We can use it to get current System.Object as String (System.String type)
....Thanks...:)...
Well, I also don't know about this but after see your post I catch lots of good information on this. Thanks for sharing us!
ReplyDeleteExpert Wordpress Developer | Hire .NET Developer
Thanks god...:)
DeleteThanks..
ReplyDeleteI want to take the screenshot of a particular webelement or object rather than whole page, do you have any suggestion on this? thanks!
ReplyDeleteAs, you can not take individual screenshot by default from selenium, you need to use either raw java code or other 3rd party library. it is easy to use Robot framework with java for taking specific screenshot. You have to mentioned the size and points.
ReplyDeleteSmall tips.. If I were you, I would have extend selenium code and modify to satisfy my need as selenium has object locator and it has screen shot taking library...:)..
Thanks and can you pls provide an example!!
ReplyDeletesend me mail, I will provide you either code/ link of code..
DeleteHi Shantanu, What are the references you have used to write these codes? I have used the following using the using block:
ReplyDeleteusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Selenium;
using TechTalk.SpecFlow;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using Xunit;
using System.Drawing.Imaging;
Please suggest if I will need to add some references into my C# project. Working to create a small tool and need to play with some screenshots.
Also, this is a nice blog. Good to find a Bangladeshi contributor in the block.
Kollol.
SQAE.
Jaxara IT Ltd.
Thanks bhia... Name spaces are
Deleteusing OpenQA.Selenium; // for Screenshots
Add System.Drawing.dll in project's reference to get the image format (System.Drawing.Imaging.ImageFormat.Jpeg)
Others are okay