Friday, July 24, 2015

How to setup Selenium in Firefox browser in Linux OS


Recently, I had to setup selenium in Firefox browser at office and I thought of sharing how to set up it in Linux environment (CentOS) and problems I have faced while doing it. 

This is all what you need:
  • Java - I have version 1.6.0_18
  • Any IDE - I have Eclipse Indigo

Lets get started.

Step 1: Creating a project in Eclipse

First, create a new Java project in Eclipse.
File -> New -> Project -> Java Project and give a Project Name and location to save the project. I gave the name as "SeleniumForFirefox".

Then, to create a package, right click on the newly created project -> New -> Package and give a package name. I just gave "test".

Next, create a Java class by right clicking on the package -> New -> Class and give a class name. I gave "SeleniumFirefox".

Now, your project structure should look like this.





Step 2: Download the selenium standalone driver

Download the selenium standalone driver from the link below. The Firefox driver is included in this.






Step 3: Add the downloaded JAR file to the library

Right click on the project -> Build Path -> Configure Build Path. Then, you will get a properties window. Click on the Libraries" tab and click on "Add External JARs.." 

Next, select the downloaded JAR file at step 2. If you add it correctly, you will see it added like below. Then click "OK"




Step 4: Write the test script

Now, we are ready to write the test script to run on selenium. Following is a sample code explained with comments.




Step 5: Execute and verify the output

Now, execute the script by clicking on Run button. 

You will see the Firefox browser opens and loads the given URL in the script. Then, it will maximize the browser window and wait for 10 seconds. Then print the following output in the console and close the browser.




This is it. Now, you can try to write more advanced test scripts if you setup and configure selenium correctly.



But, this is not always a bed of roses. You will get errors when you try this and you might not find enough resources in the web to solve it, Following is such error I got while trying it.

When I run the script, Firefox browser will be opened but will not load the page (show a blank screen). Then the following exception is thrown in the console.

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:...

JavaScript error: resource://gre/modules/BookmarkHTMLUtils.jsm, line 897: NS_ERROR_UNEXPECTED: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsINavBookmarksService.removeFolderChildren]
JavaScript error: jar:file:///usr/lib64/firefox/browser/omni.ja!/components/nsBrowserGlue.js, line 1868: NS_ERROR_UNEXPECTED: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIAnnotationService.getItemsWithAnnotation]
1437566151636    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]
1437566151636    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/LightweightThemeManager.jsm: ["LightweightThemeManager"]
1437566151638    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/GMPProvider.jsm
1437566151639    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/PluginProvider.jsm
1437566151639    addons.manager    DEBUG    Starting provider: XPIProvider
1437566151639    addons.xpi    DEBUG    startup
1437566151640    addons.xpi    INFO    Mapping fxdriver@googlecode.com to /tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com
1437566151640    addons.xpi    DEBUG    Ignoring file entry whose name is not a valid add-on ID: /tmp/anonymous1102325584771244753webdriver-profile/extensions/webdriver-staging
1437566151641    addons.xpi    INFO    Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to /usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}
1437566151641    addons.xpi    DEBUG    checkForChanges
1437566151641    addons.xpi    DEBUG    Loaded add-on state from prefs: {"app-profile":{"fxdriver@googlecode.com":{"d":"/tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com","e":false,"v":"2.42.2","st":1437566150000,"mt":1437566150000}},"app-global":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"d":"/usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","e":true,"v":"38.1.0","st":1437414410000,"mt":1435943146000}}}
1437566151642    addons.xpi    DEBUG    getModTime: Recursive scan of {972ce4c6-7e08-4474-a285-3208198ce6fd}
1437566151642    addons.xpi    DEBUG    getInstallState changed: false, state: {"app-profile":{"fxdriver@googlecode.com":{"d":"/tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com","e":false,"v":"2.42.2","st":1437566150000,"mt":1437566150000}},"app-global":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"d":"/usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","e":true,"v":"38.1.0","st":1437414410000,"mt":1435943146000}}}
1437566151643    addons.xpi    DEBUG    No changes found
1437566151643    addons.manager    DEBUG    Registering shutdown blocker for XPIProvider
1437566151643    addons.manager    DEBUG    Provider finished startup: XPIProvider
1437566151643    addons.manager    DEBUG    Starting provider: LightweightThemeManager
1437566151644    addons.manager    DEBUG    Registering shutdown blocker for LightweightThemeManager
1437566151644    addons.manager    DEBUG    Provider finished startup: LightweightThemeManager
1437566151644    addons.manager    DEBUG    Starting provider: GMPProvider
1437566151645    addons.manager    DEBUG    Registering shutdown blocker for GMPProvider
1437566151646    addons.manager    DEBUG    Provider finished startup: GMPProvider
1437566151646    addons.manager    DEBUG    Starting provider: PluginProvider
1437566151646    addons.manager    DEBUG    Registering shutdown blocker for PluginProvider
1437566151646    addons.manager    DEBUG    Provider finished startup: PluginProvider
1437566151646    addons.manager    DEBUG    Completed startup sequence
console.error: 
  [CustomizableUI]
  Custom widget with id loop-button does not return a valid node
console.error: 
  [CustomizableUI]
  Custom widget with id loop-button does not return a valid node
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
1437566152365    addons.manager    DEBUG    Starting provider: <unnamed-provider>
1437566152365    addons.manager    DEBUG    Registering shutdown blocker for <unnamed-provider>
1437566152366    addons.manager    DEBUG    Provider finished startup: <unnamed-provider>

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:118)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)
    at com.selenium.test.SeleniumChrome.main(SeleniumChrome.java:32)
Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(/usr/bin/firefox) on port 7055; process output follows: 
ookmarksService.removeFolderChildren]
JavaScript error: resource://gre/modules/BookmarkHTMLUtils.jsm, line 897: NS_ERROR_UNEXPECTED: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsINavBookmarksService.removeFolderChildren]
JavaScript error: jar:file:///usr/lib64/firefox/browser/omni.ja!/components/nsBrowserGlue.js, line 1868: NS_ERROR_UNEXPECTED: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIAnnotationService.getItemsWithAnnotation]
1437566151636    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]
1437566151636    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/LightweightThemeManager.jsm: ["LightweightThemeManager"]
1437566151638    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/GMPProvider.jsm
1437566151639    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/PluginProvider.jsm
1437566151639    addons.manager    DEBUG    Starting provider: XPIProvider
1437566151639    addons.xpi    DEBUG    startup
1437566151640    addons.xpi    INFO    Mapping fxdriver@googlecode.com to /tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com
1437566151640    addons.xpi    DEBUG    Ignoring file entry whose name is not a valid add-on ID: /tmp/anonymous1102325584771244753webdriver-profile/extensions/webdriver-staging
1437566151641    addons.xpi    INFO    Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to /usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}
1437566151641    addons.xpi    DEBUG    checkForChanges
1437566151641    addons.xpi    DEBUG    Loaded add-on state from prefs: {"app-profile":{"fxdriver@googlecode.com":{"d":"/tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com","e":false,"v":"2.42.2","st":1437566150000,"mt":1437566150000}},"app-global":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"d":"/usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","e":true,"v":"38.1.0","st":1437414410000,"mt":1435943146000}}}
1437566151642    addons.xpi    DEBUG    getModTime: Recursive scan of {972ce4c6-7e08-4474-a285-3208198ce6fd}
1437566151642    addons.xpi    DEBUG    getInstallState changed: false, state: {"app-profile":{"fxdriver@googlecode.com":{"d":"/tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com","e":false,"v":"2.42.2","st":1437566150000,"mt":1437566150000}},"app-global":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"d":"/usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","e":true,"v":"38.1.0","st":1437414410000,"mt":1435943146000}}}
1437566151643    addons.xpi    DEBUG    No changes found
1437566151643    addons.manager    DEBUG    Registering shutdown blocker for XPIProvider
1437566151643    addons.manager    DEBUG    Provider finished startup: XPIProvider
1437566151643    addons.manager    DEBUG    Starting provider: LightweightThemeManager
1437566151644    addons.manager    DEBUG    Registering shutdown blocker for LightweightThemeManager
1437566151644    addons.manager    DEBUG    Provider finished startup: LightweightThemeManager
1437566151644    addons.manager    DEBUG    Starting provider: GMPProvider
1437566151645    addons.manager    DEBUG    Registering shutdown blocker for GMPProvider
1437566151646    addons.manager    DEBUG    Provider finished startup: GMPProvider
1437566151646    addons.manager    DEBUG    Starting provider: PluginProvider
1437566151646    addons.manager    DEBUG    Registering shutdown blocker for PluginProvider
1437566151646    addons.manager    DEBUG    Provider finished startup: PluginProvider
1437566151646    addons.manager    DEBUG    Completed startup sequence
console.error: 
  [CustomizableUI]
  Custom widget with id loop-button does not return a valid node
console.error: 
  [CustomizableUI]
  Custom widget with id loop-button does not return a valid node
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
1437566152365    addons.manager    DEBUG    Starting provider: <unnamed-provider>
1437566152365    addons.manager    DEBUG    Registering shutdown blocker for <unnamed-provider>
1437566152366    addons.manager    DEBUG    Provider finished startup: <unnamed-provider>

Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: host: 'colkvm17', ip: '10.30.1.109', os.name: 'Linux', os.arch: 'i386', os.version: '2.6.32-504.16.2.el6.x86_64', java.version: '1.6.0_18'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:130)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)
    at com.selenium.test.SeleniumChrome.main(SeleniumChrome.java:32)
Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
ookmarksService.removeFolderChildren]
JavaScript error: resource://gre/modules/BookmarkHTMLUtils.jsm, line 897: NS_ERROR_UNEXPECTED: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsINavBookmarksService.removeFolderChildren]
JavaScript error: jar:file:///usr/lib64/firefox/browser/omni.ja!/components/nsBrowserGlue.js, line 1868: NS_ERROR_UNEXPECTED: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIAnnotationService.getItemsWithAnnotation]
1437566151636    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]
1437566151636    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/LightweightThemeManager.jsm: ["LightweightThemeManager"]
1437566151638    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/GMPProvider.jsm
1437566151639    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/PluginProvider.jsm
1437566151639    addons.manager    DEBUG    Starting provider: XPIProvider
1437566151639    addons.xpi    DEBUG    startup
1437566151640    addons.xpi    INFO    Mapping fxdriver@googlecode.com to /tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com
1437566151640    addons.xpi    DEBUG    Ignoring file entry whose name is not a valid add-on ID: /tmp/anonymous1102325584771244753webdriver-profile/extensions/webdriver-staging
1437566151641    addons.xpi    INFO    Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to /usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}
1437566151641    addons.xpi    DEBUG    checkForChanges
1437566151641    addons.xpi    DEBUG    Loaded add-on state from prefs: {"app-profile":{"fxdriver@googlecode.com":{"d":"/tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com","e":false,"v":"2.42.2","st":1437566150000,"mt":1437566150000}},"app-global":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"d":"/usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","e":true,"v":"38.1.0","st":1437414410000,"mt":1435943146000}}}
1437566151642    addons.xpi    DEBUG    getModTime: Recursive scan of {972ce4c6-7e08-4474-a285-3208198ce6fd}
1437566151642    addons.xpi    DEBUG    getInstallState changed: false, state: {"app-profile":{"fxdriver@googlecode.com":{"d":"/tmp/anonymous1102325584771244753webdriver-profile/extensions/fxdriver@googlecode.com","e":false,"v":"2.42.2","st":1437566150000,"mt":1437566150000}},"app-global":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"d":"/usr/lib64/firefox/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}","e":true,"v":"38.1.0","st":1437414410000,"mt":1435943146000}}}
1437566151643    addons.xpi    DEBUG    No changes found
1437566151643    addons.manager    DEBUG    Registering shutdown blocker for XPIProvider
1437566151643    addons.manager    DEBUG    Provider finished startup: XPIProvider
1437566151643    addons.manager    DEBUG    Starting provider: LightweightThemeManager
1437566151644    addons.manager    DEBUG    Registering shutdown blocker for LightweightThemeManager
1437566151644    addons.manager    DEBUG    Provider finished startup: LightweightThemeManager
1437566151644    addons.manager    DEBUG    Starting provider: GMPProvider
1437566151645    addons.manager    DEBUG    Registering shutdown blocker for GMPProvider
1437566151646    addons.manager    DEBUG    Provider finished startup: GMPProvider
1437566151646    addons.manager    DEBUG    Starting provider: PluginProvider
1437566151646    addons.manager    DEBUG    Registering shutdown blocker for PluginProvider
1437566151646    addons.manager    DEBUG    Provider finished startup: PluginProvider
1437566151646    addons.manager    DEBUG    Completed startup sequence
console.error: 
  [CustomizableUI]
  Custom widget with id loop-button does not return a valid node
console.error: 
  [CustomizableUI]
  Custom widget with id loop-button does not return a valid node
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
1437566152365    addons.manager    DEBUG    Starting provider: <unnamed-provider>
1437566152365    addons.manager    DEBUG    Registering shutdown blocker for <unnamed-provider>
1437566152366    addons.manager    DEBUG    Provider finished startup: <unnamed-provider>

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:118)
    ... 7 more



After searching and trying out various other ways, I figured out this is happening because of a compatibility issue in selenium web driver with the Firefox browser we use. 
Before using the selenium standalone server version 2.46.0,  I used 2.42.2 with the Firefox browser version 38. Then, I changed the selenium server to the latest as of now (2.46.0) and this error was then solved.

So, my current versions are as follows.
  • Selenium standalone server version - 2.46.0
  • Firefox Browser version - 38
  • CentOS - 6.6 (The OS version does not play a huge role in this case. But, this is a factor that we should consider when setting up selenium in other browsers such as Chrome)

So, when you get errors like this, first thing you should keep in mind is to check the compatibility between the web driver and the browser version.

Happy Testing!!! :)







1 comment: