2012-12-24

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
 myFireProfile.Clean();
 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.
 myFireProfile.ProfileDirectory

 -To Set proxy preferences for a profile. I will write different post for working with proxy.
myFireProfile.SetProxyPreferences(myProxy);
 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.
myFireProfile.ToBase64String();

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
myFireProfile.WriteToDisk()

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 browser.download.dir – 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 browser.download.dir and the file type will be the value of browser.helperApps.neverAsk.saveToDisk.

...Thanks....:)...