You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

How Firefox Profile solved our basic automation problems

0.00 avg. rating (0% score) - 0 votes
             
What is FirefoxProfile?
 
Firefox saves your personal information such as bookmarks, passwords, and user preferences in a set of files called your profile, which is stored in a separate location from the Firefox program files. You can have multiple Firefox profiles, each containing a separate set of user information. – Reference: https://support.mozilla.org .
In automation terms, Firefox Profile is used to set properties for the WebDriver. We Used firefox profile to make our scripts execute on various versions of Firefox. It can also be used to handle ssl certificates or changing the user agents of the browser. Firefox profile also provides methods to handle downloaded files using automation. The above mentioned are few benefits which we have implemeneted using Firefox Profile in our Naukri Site automation.
 
Illustrated below I’ll explain each of it one by one and how it benefitted our automation scripts?
 
1. Automation Testing on different versions of Firefox using Firefox Profile: 
For executing our scripts on different     Firefox versions, we use Firefox Profile class. Below is an example to illustrate how we can execute our scripts on different firefox versions:    
String BrowserVersion =browserType.split(“FF”)[1].trim();
            FirefoxProfile profile = new FirefoxProfile();
            driver = new FirefoxDriver(new FirefoxBinary(new File(“Firefox Version Path”+BrowserVersion+”\firefox.exe”)), profile);
In this code we have created an object of Firefox Profile. And using the path where other Mozilla versions are kept we can ask the driver to launch the particular firefox version. Here BrowserVersion is referred to version of Firefox we want to launch. For Example we can set BrowserVersion to FF12 so driver will launch Firefox 12.
 

2. Handling SSL Certificate Errors:    

Sometimes while executing our scripts we encounter certain SSL certificate pages due to which our scripts fail on several occasions. This can happen when we try to access any website using https instead of http. To overcome this problem we can use Firefox Profile method. Below is the sample code to handle SSL certificate issue:
String BrowserVersion =browserType.split(“FF”)[1].trim();
                FirefoxProfile profile = new FirefoxProfile();
                profile.setAcceptUntrustedCertificates(true);
                profile.setAssumeUntrustedCertificateIssuer(false);
                driver = new FirefoxDriver(new FirefoxBinary(new File(“Firefox Version Path”+BrowserVersion+”\firefox.exe”)), profile);
In this code we have used the Firefox Profile method setAcceptUntrustedCertificates and setAssumeUntrustedCertificateIssuer. Once values have been set to these methods we launch the driver. If browser encounters any SSL certificate then it will continue the execution of the script without halt.

3. Javascript Disable Case:    

Javascript Disable testing is basically done for testing the server side validations. Sometimes we need to verify a functionality without using Javascript. This can be done manually or using Selenium. Below is the example how we can do this with Firefox Profile.
Disabling Javascript in Firefox:
FirefoxProfile p = new FirefoxProfile();
p.setPreference(“javascript.enabled”, false);
WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File(“Path”+BrowserVersion+”\firefox.exe”)), p);
driver.manage().window().maximize() ;
Above code will first create an instance of firefox profile and using setPreference function we can set the javascript as disabled. Now, again to launch a particular firefox driver we use the option of creating an object of FirefoxBinary class.
4. Executing the script on Android, Iphone and Windows User agent:
Now a days every one is going on mobile. Executing our scripts on Android, Iphone and Windows is made easy with Firefox Profile. Below , I’ll illustrate how we can execute our scripts on Android, Iphone and Windows using simple firefox profile method.
To Execute our scripts on Android user agent :
FirefoxProfile profile = new FirefoxProfile();
            try {
                String directory = System.getProperty(“user.dir”);
                directory = directory.replace(“\”, “\\”);
                profile.addExtension(new File(directory+”\user_agent_switcher-0.7.3-fx+sm.xpi”));
            } catch (IOException e) {
                e.printStackTrace();
            }
            profile.setPreference(“general.useragent.override”, “Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.105 Mobile Safari”);
            driver = new FirefoxDriver(new FirefoxBinary(new File(Common_Resources_Directory + “\MozillaVersions\Mozilla Firefox18\firefox.exe”)), profile);
The code above will allow the firefox to change user agent to Android using its function setPreference. Now here I have mentioned Common_Resources_Directory which is a user declared variable to store the path of the Firefox Exe. The line “Common_Resources_Directory + “\MozillaVersions\Mozilla Firefox18\firefox.exe”” is the path where you keep the firefox exe. You can change this according to your need.
profile.addExtension method add the user agent to the existing firefox driver.
To Execute our scripts on Iphone user agent:
    FirefoxProfile profile = new FirefoxProfile();
            try {
                String directory = System.getProperty(“user.dir”);
                directory = directory.replace(“\”, “\\”);
                profile.addExtension(new File(directory+”\user_agent_switcher-0.7.3-fx+sm.xpi”));
            } catch (IOException e) {
                e.printStackTrace();
            }
            profile.setPreference(“general.useragent.override”, “Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A405 Safari/600.1.4”);
            driver = new FirefoxDriver(new FirefoxBinary(new File(Common_Resources_Directory + “\MozillaVersions\Mozilla Firefox18\firefox.exe”)), profile);
To Execute our scripts on Windowsphone user agent:
FirefoxProfile profile = new FirefoxProfile();
            try {
                String directory = System.getProperty(“user.dir”);
                directory = directory.replace(“\”, “\\”);
                profile.addExtension(new File(directory+”\user_agent_switcher-0.7.3-fx+sm.xpi”));
            } catch (IOException e) {
                e.printStackTrace();
            }
            profile.setPreference(“general.useragent.override”, “Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch IEMobile/11.0; NOKIA; 909) like Gecko”);
            driver = new FirefoxDriver(new FirefoxBinary(new File(Common_Resources_Directory + “\MozillaVersions\Mozilla Firefox18\firefox.exe”)), profile);


5. Downloading the files using Firefox Profile :    

In a website sometimes we allow user to download certain files. We can handle this using Firefox Profile. For this we have to set a path variable so as to tell the driver where exactly we want to download the files. Below is the sample code for the same:
 
profile.setPreference(“browser.download.folderList”,2); profile.setPreference(“browser.download.manager.showWhenStarting”, false); profile.setPreference(“browser.download.dir”, downloadPath); profile.setPreference(“browser.helperApps.neverAsk.openFile”,”application/excel”);
profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”,”application/excel”);
profile.setPreference(“browser.helperApps.alwaysAsk.force”, false);
profile.setPreference(“browser.download.manager.showAlertOnComplete”,false); profile.setPreference(“browser.download.manager.closeWhenDone”, false);
Conclusion: Firefox Profile has a variety of options which we can do small wonders in our scripts. There are still many things which are unexplored in Firefox Profile. May be this help you overcome small problems in your automation scripts.
Happy Automation Folks!!!