3
votes

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.

4

4 Answers

5
votes

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.

2
votes

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}    
0
votes

If it is not working then try with mime-type - application/octet-stream for CSV file.

fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
-1
votes

You can return profile path from create_profile function and then use it open browser. Make sure to delete directory profile path in teardown test/suite

    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

Use path in open browser keyword

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