简体   繁体   English

如何在 Robot Framework 中设置 FireFox 的首选项

[英]How to set preferences for FireFox in Robot Framework

I'm trying to write a test case in robot framework to download an excel file automatically from a web-site.我正在尝试在机器人框架中编写一个测试用例,以从网站自动下载一个 excel 文件。 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.我还尝试设置一个现有的 Firefox 配置文件,因为说明它工作正常,但我希望能够自动调整首选项。

Any idea?任何的想法?

As @Sachin said I wrote a python script to set preferences for FireFox as well:正如@Sachin 所说,我编写了一个 python 脚本来设置 FireFox 的首选项:

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 .但是我收到错误TypeError: coercing to Unicode: need string or buffer, FirefoxProfile found在库_browsermanagement.py方法_make_browser_browsermanagement.py

I edited the code and removed return fp and then changed the Robot test case like this:我编辑了代码并删除了return fp ,然后像这样更改了 Robot 测试用例:

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.它删除了异常并设置了我的首选项,但我仍然需要传递配置文件地址。

I have written following python code to create profile:我编写了以下 python 代码来创建配置文件:

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.它将excel文件保存到路径变量中指定的位置。

Your keyword should return the path to created Firefox profile:您的关键字应返回创建的 Firefox 配置文件的路径:

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}    

如果它不起作用,请尝试使用 mime-type - application/octet-stream for CSV 文件。

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

You can return profile path from create_profile function and then use it open browser.您可以从 create_profile 函数返回配置文件路径,然后使用它打开浏览器。 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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM