1

尝试在 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。

感谢你的帮助!

4

2 回答 2

3

我相信您必须修补 selenium-webdriver,因为似乎没有内置方法来指定包含临时配置文件的目录。

背景

Seleium-webdriver(因此是 watir-webdriver)directory in \selenium-webdriver-2.26.0\lib\selenium\webdriver\firefox\profile.rb使用以下方法创建临时 Firefox 配置文件:

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'.
于 2012-11-30T17:59:20.923 回答
2

设置环境变量TMPDIR足以让 ruby​​ 和 selenium 将它们的临时文件写入您想要的位置。

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).

所以,如果你在你的 shell 中:

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

于 2015-03-18T12:56:20.007 回答