How to take a screenshot in selenium webdriver?

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

public void TestHome()
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
We can use it to get the value of the screenshot image as an array of bytes.

We can use it to get current System.Object as String (System.String type)


What is Load Testing? How to do it

In this article we are going to see what is Load testing and how to do it. I will try to discuses about strategy to perform Load testing. In my other Load test tool specific post, I will explain how to implement a load test.

Load testing is the process of putting demand on a system or device and measuring its response. In case of software, the demands will be the functions which are needed to be performed and our system will have to perform those. For example,A load test will define and measure a computer/ peripheral/ server/network /application's maximum level of work of its specifications.

The primary goal of load testing are -
-To define the maximum amount of work a system can handle without significant performance degradation
-For comparing capabilities and accuracies of a system with other systems (under controlled lab conditions).
-To get idea about the system on how it functions in real life.
-To get different type of report for having a overall idea on a new or legacy system.

There are mainly two type of load testing can be performed

A. Longevity testing : This will measure the system's ability on how long the system performs on standard operations(standard load). The main focus will be on how long(the time) the system is stable and consistent.

B.Volume testing : This will measure the system's ability on how the system is stable on heavy operation(heavy load) in standard time. The main focus will be the volume of the operations(load) that system can handle. This load may be performed by multiple users.

Both have pinpoint bottlenecks, bugs and component limitations. EX-A mobile may have a fast processor but a limited amount of RAM (memory). Load testing can provide us with a general idea of
-how many applications or processes can be run simultaneously while maintaining the rated level of performance.
-How long the mobile will be stable with operating standard applications.
-How many users can use the application's certain function (if it is a mobile app using web service)

To perform a load test you may go through following steps
-Define the functions to be perform for the load test .
-Define the time for performing the functions.
-Make a bottom line standard system behavior to define the bugs/issues(like , set a time for perform some operation or set some operations to run for certain period of time)
-Define the measuring parameters(what to monitor)
-Choose suitable reports(grid results, charts, graphs etc) to monitor the results.
-Choose alert/marking type to determine alarm points(defining bugs).
-Choose tools meeting the above requirements.

For different scenarios, load testing measuring parameter can be different. 
For a desktop/mobile application (not using data network)
-Use of processor/memory/time on certain functions.
For a web application/service/data server/applications using internet(desktop/mobile)
-Amount of time on certain functions.
-Number of users concurrently can use certain functions.
-Number of functions can be used in certain time

Some load test tools are :
a. JMeter
b. Load UI
c. Load Runner
d. Visual Studio Load test
e. Eclipse Load test tool 

Some Load testing areas are like
-Performing extreme process using functions(like complex logic operation, tree operations)
-Writing and reading data to and from a hard disk continuously
-Downloading a series of large files from the Internet
-Log in to a same system with large number of users.
-Exchanging big number and big size of mail in a mail server. 
-Downloading a series of large files from the Internet.
-Running multiple applications on a computer or server simultaneously.
-Assigning many jobs to a printer in a queue.
- Large amount of permission concurrently occurs as accepted/rejected/filtered.  

.... I will try to increase the more load test areas incrementally....


[Note : Load testing is a part of a performance testing. I will cover performance testing in separate post]

How to run selenium webdriver in Google Chrome?

In this article we are going to see necessary steps for running selenium web driver code in Google Chrome. I am using c# code with VS2010.

Step 1 : Install Google Chrome in your PC from this link

Step 2 : Install Google Chrome Driver from this link

Step 3 : After adding dll in properties of the project , we have to add the references in the class.
using OpenQA.Selenium.Chrome;[For basic idea on how to set up selenium environment, see the post]

Step 4:
Then we have to initiate the driver object as Google Chrome Driver.
private IWebDriver driver  = new ChromeDriver();

Now this driver can be used Just Like as other Firefox or IE drivers.

We can initiate Chrome Driver in 6 ways(overridden).

1.With no parameter
2.With a path [need when running remotely]
3. With an Option
4. With a path and an Option
-ChromeDriver("stringPath", options)
5. With a path, an option and Timeout(The maximum amount of time to wait for each command.)
-ChromeDriver("stringPath",options, cmdTout)
6. With an option, timeout and a specific driver Service.
- ChromeDriver(service, options,cmdTout)

Note : In every case ,
-"stringPath" is the path to the directory containing ChromeDriver.exe. Ex "C:\Users\ssarker\Downloads"
- option  is an object of OpenQA.Selenium.Chrome.ChromeOptions
- cmdTout is an object of System.TimeSpan
- service is an object of OpenQA.Selenium.DriverService

Chrome driver will be found in this link. Install windows edition. You can get more detail here.
Note :
It is better to use with string. I use following code for initiating chrome driver(with chrome driver location).
public IWebDriver driver = new ChromeDriver(@"C:\") ;
This string can be other remotely located PC accessible over LAN.
There might be error for version on chrome console(i found, expected version is less then found version). To avoid the error, download latest web driver and chrome.

I will continuously update note section incrementally(after I find and solve problems regarding running in Chrome)


How to change user agent in Firefox by selenium webdriver

In this article , we will see the function to change user agent of Firefox using selenium webdriver. I am using c# with vs 2010.
From my administrating Firefox post, we know that we can change preferences. I have another post describing different user agent(how to change user agent in jmeter) . So, lets change the agent from webdriver so that when ever we need we can use Firefox to change its agent to test different behavior of a web application.

First declare a Firefox profile. Then change general.useragent.override with a value(desire user agent string, any from here or here )

public static FirefoxProfile myFireProfile = new FirefoxProfile();

myFireProfile.SetPreference("general.useragent.override", "our_string");

public IWebDriver driver= new FirefoxDriver(myFireProfile);

So, our Firefox driver initializes with new user agent .


How to work with Proxy in Selenium WebDriver

In this article we are going to see the functions for the configuration of proxy while testing a web application(under different proxy) . The are various property to control proxy settings of a browser. In here I am using c# with VS 2010.

Selenium webdriver has build-in class to maintain proxy settings.  We have to initialize a proxy object.We can initialize in 2 ways.

private Proxy myProxy = new Proxy();

Another one with proxy settings string as dictionary(multiple settings initialization) .
new Proxy(Dictionary settings)

A proxy object has seven properties . These properties are used for several purposes.

-To Get or set the value(string) of the proxy for the FTP protocol.

-To Get or set the value(string) of the proxy for the HTTP protocol.

-To Get or set a value(Boolean) to know proxy uses autodetection.

-To Gets or sets the value(string) for when no proxy is specified.It will overwrite the proxy settings.

-To Get or set the URL(as string) used for proxy Auto Configuration. 

 -To Get or set the value(string) of the proxy for the SSL protocol.

-To Get or set the type of proxy(enum).
mProxyKind = myProxy.Kind;

Note : There is an Enum(ProxyKind) for identifying the proxy type.
The ProxyKind enum members are :

Member name
Value Description
Direct 0
Direct connection, no proxy (default on Windows).
Manual 1
Manual proxy settings (e.g., for httpProxy).
ProxyAutoConfigure 2
Proxy autoconfiguration from URL.
AutoDetect 4
Use proxy autodetection.
System 5
Use the system values for proxy settings (default on Linux).
Unspecified 6
No proxy type is specified.

In here, These are property functionality of a proxy object. I will provide example with proxy configuration in separate post where we can see different configurations for different proxy(like NTLM, CC proxy, Win Gate etc).


Administrating Firefox in Selenium Webdriver

In this following article, we are going to see different functions in  selenium webdriver for Firefox Administration. I am using c# with VS2010.

When Firefox run in PC, it always run under a profile.What is Firefox profile? Firefox saves our personal information such as  bookmarks/passwords/user preferences/Extension settings  in a set of files called your profile, which is stored in a separate location from the Firefox program files(as user data). In windows 7, it is in "\Users\\AppData\Local\Mozilla\Firefox\Profiles"(in my pc "\Users\ssarker\AppData\Local\Mozilla\Firefox\Profiles"). You can have multiple Firefox profiles, each containing a separate set of user information. The Firefox Profile Manager allows us to create, remove, rename, and switch profiles.
To start Firefox driver with a Firefox profile, we have to first initiate a profile

private static FirefoxProfile myFireProfile = new FirefoxProfile();//I use static to use in all over program.

Then declare my driver to use myFireProfile
public IWebDriver driver= new FirefoxDriver(myFireProfile);

Note :We can initiate a profile in 3 ways.
new myFireProfile();
new myFireProfile("Profile Directory");
new myFireProfile("Profile Directory", true);// in here true/false is a Boolean field indicating  delete profile Source On Clean. If it is true, On clean function, the profile will be deleted.

Let see different functions
-To Get/Set accept untrusted certificates(it is a Boolean property).
myFireProfile.AcceptUntrustedCertificates = true;// or false
Note : Spatially needed for testing Https untrusted sites. 

-To Install Firefox Extensions( this should be the XPI file's path for a add on)
 myFireProfile.AddExtension("path to extension ");

 -To Get or Set a Boolean value for Firefox to execute commands always without focus .
 myFireProfile.AlwaysLoadNoFocusLibrary = true;//false

-To Clean this Firefox profile
 Note : It is necessary when we will test with anonymous profile.

-To Get or Set a Boolean value to enable / disable native events.
myFireProfile.EnableNativeEvents = true;//false

-To Get or Set the port(in integer valu) on which the profile connects to the WebDriver extension
 myFireProfile.Port = 5;//integer vale
 Note : It is spatially needed for multiple profile Firefox running.

-To Get the directory containing the profile. This provide string output for path.

 -To Set proxy preferences for a profile. I will write different post for working with proxy.
 Note : It takes a Proxy object(myProxy). To run Firefox under pa profile with a proxy, we need to initiate a proxy before setting to a profile.

 -To Convert the profile into a base64-encoded string.

Note : It gives string as output. When we need to save our Firefox profile info as string, we need that.

Firefox Configuration/Preference Changing:
What is Firefox configuration? If we write about:config in Firefox address bar, we will get a configuration tab (with a warning message, ignore that) .
In here , we can add, delete or modify the preferences of Firefox.
Every entry has Two element, a. Preference Name, b. Preference Value.
Status and Type columns are showing the property state and what type of value accepted by configuration.
By preference names, we can easily understand the functionality. So, lets see the methods of selenium web driver framework to add, edit, delete those preference and how to use them.

-To Set a preference in the profile.
myFireProfile.SetPreference("preferenceNameAsString", true);/Boolean value
myFireProfile.SetPreference("preferenceNameAsString", 6);//Integer Value
myFireProfile.SetPreference("preferenceNameAsString", "ValueAsString");

Note : In here the three overloading methods used for Boolean, Integer, string type value insertion of

-To Write this in-memory representation of a profile to disk

Note : In Firefox configuration, browser.helperApps.neverAsk.saveToDisk is a comma-separated list of MIME types to save to disk without asking what to use to open the file. And – The last directory used for saving a file[ from the “What should (browser) do with this file?” dialog.] . So, whenever we call write do disk, it will save the profile to the path of and the file type will be the value of browser.helperApps.neverAsk.saveToDisk.