如何在 Robot Framework 中设置 FireFox 的首选项 [英] How to set preferences for FireFox in Robot Framework

查看:34
本文介绍了如何在 Robot Framework 中设置 FireFox 的首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在机器人框架中编写一个测试用例,以从网站自动下载 excel 文件.我想使用机器人脚本为我的浏览器设置首选项,以便在我想要的目标目录中自动下载文件,而无需询问我!

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.

我还尝试将现有的 Firefox 配置文件设置为 this 表示哪个工作正常,但我希望能够自动调整首选项.

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.

有什么想法吗?

正如@Sachin 所说,我编写了一个 python 脚本来为 FireFox 设置首选项:

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

但是我在库 _browsermanagement.py 的方法 _make_browser 中遇到错误 TypeError: coercing to Unicode: need string or buffer, FirefoxProfile found.

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

我编辑了代码并删除了return fp,然后像这样更改了机器人测试用例:

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.

推荐答案

我已经编写了以下 python 代码来创建配置文件:

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}

它将excel文件保存到路径变量中指定的位置.

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

这篇关于如何在 Robot Framework 中设置 FireFox 的首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆