更改由 Firefox 创建临时配置文件的默认文件夹



我在尝试在火狐浏览器上无头运行watir测试时收到Errno::ENOSPC错误。实际上,此错误的原因是我是从非root用户那里记录的,当我运行测试时,它会尝试在" tmp"文件夹中为临时Firefox配置文件创建目录。因为它不使用"sudo",所以它会给出此错误。

如果我在"tmp"中执行"mkdir xyz",它会给出"设备中没有空间"错误,与上述相同。

如何更改 webdriver 尝试创建临时配置文件的默认配置文件文件夹(即"/tmp")?我希望 webdriver 自己创建临时配置文件,但在我可以设置的文件夹中。

我正在使用Linux,ruby 1.9.2p320,selenium-webdriver 2.26.0和watir-webdriver 0.6.1。

感谢您的帮助!

我相信

你必须猴子补丁硒网络驱动程序,因为似乎没有内置的方式来指定包含临时配置文件的目录。

背景

Seleium-webdriver(以及watir-webdriver)使用以下方法创建临时的Firefox配置文件directory in selenium-webdriver-2.26.0libseleniumwebdriverfirefoxprofile.rb

def layout_on_disk
    profile_dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir("webdriver-profile")
    FileReaper << profile_dir
    install_extensions(profile_dir)
    delete_lock_files(profile_dir)
    delete_extensions_cache(profile_dir)
    update_user_prefs_in(profile_dir)
    profile_dir
end

临时文件夹由以下人员创建:

Dir.mktmpdir("webdriver-profile")

这是在 tmpdir 库中定义的。对于Dir.mktmpdir方法,第二个可选参数定义父文件夹(即创建临时配置文件的位置)。如果未指定任何值,如本例所示,则在 Dir.tmpdir 中创建临时文件夹,在您的情况下是"tmp"文件夹。

溶液

要更改临时文件夹的创建位置,您可以猴子修补 layout_on_disk 方法,以在调用 Dir.mktmpdir 时指定所需的目录。它看起来像:

require 'watir-webdriver'
module Selenium
module WebDriver
module Firefox
class Profile
    def layout_on_disk
        #In the below line, replace 'your/desired/path' with
        #  the location of where you want the temporary profiles
        profile_dir = @model ?
            create_tmp_copy(@model) : 
            Dir.mktmpdir("webdriver-profile", 'your/desired/path')
        FileReaper << profile_dir
        install_extensions(profile_dir)
        delete_lock_files(profile_dir)
        delete_extensions_cache(profile_dir)
        update_user_prefs_in(profile_dir)
        profile_dir
    end
end
end
end
end
browser = Watir::Browser.new :ff
#=> The temporary directory will be created in 'your/desired/path'.

设置环境变量 TMPDIR 足以使红宝石和硒将它们的临时文件写入您想要的位置。

从 http://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html :

Dir.tmpdir‘s return value might come from environment variables (e.g. $TMPDIR).

所以,如果你在你的壳里:

export TMPDIR="/whatever/you/want/"

最新更新