2012-11-30 69 views
1

我在尝试在Firefox上无条件运行watir测试时出现Errno :: ENOSPC错误。其实这个错误的原因是我从一个非root用户登录它,当我运行我的测试时,它会尝试为'tmp'文件夹中的临时firefox配置文件创建目录。因为它不使用'sudo',所以会出现此错误。更改由Firefox创建临时配置文件的默认文件夹

如果我在'tmp'中做'mkdir xyz',它会给出'没有设备空间'的错误,与上面相同。

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

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

感谢您的帮助!

回答

2

我相信你必须修补selenium-webdriver,因为看起来没有内建的方式来指定包含临时配置文件的目录。

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

背景

Seleium-webdriver的(和因此的Watir-的webdriver)使用该方法创建临时Firefox配置directory in \selenium-webdriver-2.26.0\lib\selenium\webdriver\firefox\profile.rb

它在tmpdir library中定义。对于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'.