How to set preferences for FireFox in Robot Framework

13,177

Solution 1

I have written following python code to create profile:

def create_profile(path):
    from selenium import webdriver
    fp =webdriver.FirefoxProfile()
    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()

Using above function in testcase as follows:

${random_string}    generate random string  3       
${path} Catenate    SEPARATOR=\\    ${TEMPDIR}  ${random_string}
${profile}= create_profile  ${path}
open browser    ${app_url}  ff  ff_profile_dir=${profile}

It saves the excel file to the location specified in the path variable.

Solution 2

Your keyword should return the path to created Firefox profile:

def create_profile(path):
    from selenium import webdriver
    fp =webdriver.FirefoxProfile()
    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

And only then you can use it:

${profile_path}  Create Profile    ${path}
Open Browser    ${app_url}  ff  ff_profile_dir=${profile_path}    
Share:
13,177
Zeinab Abbasimazar
Author by

Zeinab Abbasimazar

Looking to attain a challenging and responsible position as a software engineer and software analyst in telecommunication and software industry which effectively utilizes my personal, professional and educational skills and experiences. I’m also looking forward to learn and experience more on big data concepts/solutions.

Updated on June 28, 2022

Comments

  • Zeinab Abbasimazar
    Zeinab Abbasimazar almost 2 years

    I'm trying to write a test case in robot framework to download an excel file automatically from a web-site. I want to set preferences for my browser using robot scripts to download files automatically in my desired destination directory without asking me!

    I have tried this solution; but it didn't work.

    I also tried to set an existing firefox profile as this says which works fine, but I want to be capable of automatically adjusting preferences.

    Any idea?

    As @Sachin said I wrote a python script to set preferences for FireFox as well:

    from selenium import webdriver
    class WebElement(object):
        @staticmethod
        def create_ff_profile(path):
            fp = webdriver.FirefoxProfile()
            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
    

    And used it in Robot scenario:

    *** Settings ***
    Library                 Selenium2Library
    Library                 Selenium2LibraryExtensions
    Library                 OperatingSystem
    Library                 ../../../Libraries/WebElement.py
    *** Variables ***
    ${profileAddress}       C:\\Users\\user\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\VdtJKHal.default
    ${destinationUrl}       http://www.principlesofeconometrics.com/excel.htm
    ${browserType}          firefox
    ${downloadDir}          C:\\Users\\user\\Desktop
    ${acceptedTypes}        text/csv/xls/xlsx
    ${itemXpath}            //*[text()="airline"]
    *** Test Cases ***
    My Test Method
        log to console  Going to open browser with custome firefox profile!
        ${profile} =    create_ff_profile   ${downloadDir}
        Open Browser    ${destinationUrl}   ${browserType}  ff_profile_dir=${profile}
        Maximize Browser Window
        Click Element   xpath=${itemXpath}
        Sleep   10
        Close Browser
    

    But I got error TypeError: coercing to Unicode: need string or buffer, FirefoxProfile found in method _make_browser of library _browsermanagement.py.

    I edited the code and removed return fp and then changed the Robot test case like this:

    And used it in Robot scenario:

    *** Test Cases ***
    My Test Method
        log to console  Going to open browser with custome firefox profile!
        create_ff_profile   ${downloadDir}
        Open Browser    ${destinationUrl}   ${browserType}  ff_profile_dir=${profileAddress}
        Maximize Browser Window
        Click Element   xpath=${itemXpath}
        Sleep   10
        Close Browser
    

    It removed the exception and set my preferences as well, but I still need to pass the profile address.

  • Zeinab Abbasimazar
    Zeinab Abbasimazar over 8 years
    From what library did you get create_profile keyword?
  • Sachin Nikam
    Sachin Nikam over 8 years
    I have written this in python file and included as Library in suite.
  • Zeinab Abbasimazar
    Zeinab Abbasimazar over 8 years
    Oh, yes! I get it now! I'll try it!
  • Sachin Nikam
    Sachin Nikam over 8 years
    please mark answer as helpful if your problem solved with this solution :)
  • Sachin Nikam
    Sachin Nikam over 8 years
    Yes, as its a custom profile created. You need to pass it else it will not have preferences set in the profile.
  • Zeinab Abbasimazar
    Zeinab Abbasimazar over 8 years
    I have to pass the profile directory address, not the profile!
  • Sachin Nikam
    Sachin Nikam over 8 years
    Yes, path to the directory
  • Zeinab Abbasimazar
    Zeinab Abbasimazar about 8 years
    your answer is completely identical to Sachin's
  • Zeinab Abbasimazar
    Zeinab Abbasimazar almost 8 years
    fp.path is identical to the path argument; so, why bother ourselves with an extra layer? BTW I didn't use create_profile method; it's someone else's answer.
  • Kirill Kliushkin
    Kirill Kliushkin almost 8 years
    "fp.path" is a path to created ff profile. "path" is a path to folder to download files to. They are not the same.