How to set preferences for FireFox in Robot Framework
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}
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, 2022Comments
-
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 over 8 yearsFrom what library did you get
create_profile
keyword? -
Sachin Nikam over 8 yearsI have written this in python file and included as Library in suite.
-
Zeinab Abbasimazar over 8 yearsOh, yes! I get it now! I'll try it!
-
Sachin Nikam over 8 yearsplease mark answer as helpful if your problem solved with this solution :)
-
Sachin Nikam over 8 yearsYes, as its a custom profile created. You need to pass it else it will not have preferences set in the profile.
-
Zeinab Abbasimazar over 8 yearsI have to pass the profile directory address, not the profile!
-
Sachin Nikam over 8 yearsYes, path to the directory
-
Zeinab Abbasimazar about 8 yearsyour answer is completely identical to Sachin's
-
Zeinab Abbasimazar almost 8 years
fp.path
is identical to thepath
argument; so, why bother ourselves with an extra layer? BTW I didn't usecreate_profile
method; it's someone else's answer. -
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.