Search code examples
seleniumfirefoxwebdriverfirefox-addonrobotframework

How to enable adblock with seleniumlibrary in robotframework


I would like to enable uBlock Origin plugin as ads on speedtest.net during robot framework test execution as ads completly jeopardize test result.

So far I have created a Python script to create a profile with extension

from selenium import webdriver

class WebDriverProfile:
    def create_profile_with_adblock(self, path):
        fp =webdriver.FirefoxProfile()
        fp.add_extension(extension='d:/pathtoextension/ublock_origin-1.24.0-an+fx.xpi')
        fp.set_preference("browser.download.folderList",2)
        fp.set_preference("browser.download.manager.showWhenStarting",False)
        fp.set_preference("browser.download.dir",path)
        fp.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/csv')
        fp.update_preferences()
        return fp.path

Also the following robot script to call the function:

*** settings ***
Library         Selenium2Library
library         Process
library         Dialogs
Library         WebDriverProfile.py

*** Variables ***
${URL}           https://speedtest.net

*** Test Cases ***
Test with speedtest.net
    ${FF_PROFILE}=          Create Profile With Adblock         ${TEMPDIR}\\testdirff
    Open Browser            ${URL}                              browser=ff    ff_profile_dir=${FF_PROFILE}
    Execute Manual Step     Is adblocker enabled?

The browser will open, but adblocker won't be there.

Anyone could help on this please?


Solution

  • Create webdriver instead of firefox profile and install add-on.

    Python extension:

    from selenium import webdriver
    
    class WebDriverProfile:
        def create_web_driver_with_addons(self):
            extension_location='d:\\path_to_adblock\\ublock_origin-1.24.0-an+fx.xpi'
            browser = webdriver.Firefox()
            browser.install_addon(extension_location, temporary=True)
            browser.get('http://speedtest.net')
    

    Robot script:

    *** settings ***
    Library         Selenium2Library
    library         Process
    library         Dialogs
    Library         WebDriverProfile.py
    
    *** Variables ***
    ${URL}           https://speedtest.net
    
    *** Test Cases ***
    Test with speedtest.net
        create web driver with addons
        Execute Manual Step     Is adblocker enabled?