I am getting Errno::ENOSPC error while trying to run watir tests headlessly on firefox. Actually the cause of this error is that I am logged it from a non-root user and when I run my tests, it tries to create directory for temporary firefox profile in 'tmp' folder. Because it doesn't use 'sudo', it is giving this error.
If I do 'mkdir xyz' in 'tmp', it gives 'No space in device' error, which is same as above.
How can I change the default profile folder (which is '/tmp') where webdriver is trying to create temporary profile? I want webdriver to create the temporary profile by itself but in the folder which I can set.
I am using Linux, ruby 1.9.2p320, selenium-webdriver 2.26.0 and watir-webdriver 0.6.1.
Appreciate your help!
I believe you have to monkey patch selenium-webdriver, as there does not appear to be a built in way to specify the directory that includes the temporary profiles.
Background
Seleium-webdriver (and therefore watir-webdriver) creates the temporary Firefox profile directory in \selenium-webdriver-2.26.0\lib\selenium\webdriver\firefox\profile.rb using the method:
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
The temporary folder is created by the:
Dir.mktmpdir("webdriver-profile")
Which is defined in the tmpdir library. For the Dir.mktmpdir method, the second optional parameter defines the parent folder (ie where to create the temporary profile). If no value is specified, as in this case, the temporary folder is created in Dir.tmpdir, which in your situation is the 'tmp' folder.
Solution
To change where the temporary folder is created, you can monkey patch the layout_on_disk method to specify your desired directory in the call to Dir.mktmpdir. It would look like:
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'.
Setting the environment variable TMPDIR is enough to make ruby, and selenium, write their temporary files where you want.
From 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).
So, if you're in your shell:
export TMPDIR="/whatever/you/want/"
Related
I'm currently working on a script (command line tool) for work to help me manage expose consoles.
At first I was passing three arguments to the script each time I used it to login into the consoles, for example:
$ nexose-magic.rb -u user -p password -i 192.168.1.2 --display-scans
It's not very efficient, so I created a config.yml file that stores console information in a hash.
$ nexpose-magic.rb -c console --display-scans
I believe the tool will be useful to admins out there, so I'd like to share it in a gem. I can't figure out how to get my config.yml file to work with a gem install..It can't find the config.yml file! It's easy to point it at the relative path in my development directory, but once I create a gem that relative path isn't so relative anymore. How do I point nexpose-magic.rb at the config.yml file?
Is there a better way to handle something like this?
You can create a gem that include a configure class. This class has a load method that will take a directory as an argument. Then, you can pass the directory where you are currently working.
A nice way of preparing your gem is to create a Configuration singleton class in your gem:
require 'singleton'
class Configuration
include Singleton
attr_accessor :config, :app_path
def load(app_path)
#app_path = app_path
#load the config file and store the data
#config = YAML.load_file( File.join(#app_path,'config','config.yml'))
end
end
In your main class :
module MyFancyGem
class << self
#define a class method that configure the gem
def configure(app_path)
# Call load method with given path
config.load(app_path)
end
# MyFancyGem.config will refer to the singleton Configuration class
def config
MyFancyGem::Configuration.instance
end
end
end
Usage:
-Working directory
- my_new_script
- Gemfile
- config/
- config.yml
In my_new_script :
require 'bundler'
Bundler.setup(:default)
require 'my_fancy_gem'
MyFancyGem.configure(File.join(File.dirname(__FILE__),"./")) #here, you define the path
MyFancyGem.hello_world
I hope that's clear enough. I was actually about to write a blog post to explain this particular point (I hope in a more complete version of it). Let me know if you're interested !
I have a little project (non Rails) and I'm using RSpec for testing. In order to load models I'm using:
require_relative "../lib/checkout"
However I'm encountering the problem with loading config files, for instance, the test no longer locates my "items.csv" when the following:
CSV.foreach("items.csv") do |row|
Note that the problem occurs only when spec is run from the spec directory, i.e:
rspec checkout_spec.rb
Running it from the project root is fine:
rspec spec/checkout_spec.rb
Any help would be appreciated.
CSV.foreach uses the current directory to find the file. You should probably use File.expand_path to get to an absolute path to items.csv to avoid this problem.
Edited to add an example
Assuming that the file is at root/items.csv, and the ruby file with this code is at root/lib/file.rb, you could write
path = File.expand_path File.join(File.dirname(__FILE__), '..', 'items.csv')
CSV.foreach path do |row|
# rest of code...
I am trying to follow this tutorial:
http://net.tutsplus.com/tutorials/ruby/singing-with-sinatra/
Got stuck in "We’ll also make use of a “view file”, which allows us to split the markup for a view into a separate file. "
I have my basics.rb file running fine.
And My files are stored as follows:
Desktop/RubyForm/basics.rb
Desktop/RubyForm/view/form.erb
However, now when i go to http://localhost:9393/form , I am greeted with:
Errno::EIO at /form
Input/output error - <STDERR> file: lint.rb location: write line: 398
sinatra.error
Errno::ENOENT: No such file or directory -
/Users/HelenasMac/Desktop/views/form.erb
UPDATE! : Got the form to work right after running ruby basics.rb and going to http://localhost:4567/form .
However, after I run "shotgun basics.rb" , I have to go to
http://localhost:9393/form, and that's when the form doesn't show up.
What am I doing wrong? Disclaimer: mega beginner to ruby and using the terminal.
Thanks in advance!
If you cannot get shotgun to work then the new recommended way to reload Sinatra seems to be rerun.
To use it:
> gem install rerun
> cd /Users/HelenasMac/Desktop/RubyForm
> rerun ruby basics.rb
Explicity Set a Views Directory
Unless you're using inline template for your views with enable :inline_templates, you may need to explicitly define a template directory if the default values aren't working for you. The docs describe how to set your views directory as follows:
:views - view template directory
A string specifying the directory where view templates are located. By default, this is assumed to be a directory named “views” within the application’s root directory (see the :root setting). The best way to specify an alternative directory name within the root of the application is to use a deferred value that references the :root setting:
set :views, Proc.new { File.join(root, "templates") }
You may also need to explicitly set :root, and make sure that both :root and :views make sense from your current working directory.
I am writing a Ruby script which was supposed to be a small thing but has grown quite large, way to large to have everything crammed into one source file. So I am trying to separate the project into different files. I have four classes and I want to put each in its own separate source file.
What I did:
I moved all of the classes into their own files so now I have this
proj/GoogleChart.rb
proj/BarChart.rb
proj/PieChart.rb
proj/GroupedBarChart.rb
Now that they are in other files I am getting uninitialized constant GoogleChart (NameError) in all of my subclasses on the line where I inherit from GoogleChart, i.e.
require 'GoogleChart'
BarChart < GoogleChart
Can anyone tell me what is wrong?
Thanks
EDIT
Using ruby version 1.8.4
Also I have tried using the absolute path:
require 'C:/Documents and Settings/proj/GoogleChart.rb' and this is still producing a NameError
In Ruby 1.8.x, the . is part of your load path. So you should at least try to debug that by including something like:
puts $:
require 'GoogleChart'
class BarChart < GoogleChart
end
and load that in an IRB session:
Open the session in your directory proj.
Enter there require 'BarChart'
Look at the result.
For me it is:
c:\apps\ruby\test\proj>irb
irb(main):001:0> require 'BarChart'
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8/i386-msvcrt
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8/i386-msvcrt
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8
C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8/i386-mingw32
.
=> true
So the require is successful for me, and the . is part of the path (as it should). As you can see, I am working with Ruby 1.8.7, I don't know if anything has changed since 1.8.4 that is relevant here.
So please describe exactly how you run your file:
Have you opened a shell to run the file?
What is the current working directory of that shell?
Do you run by double-clicking it?
It only works when you are in your proj directory and run there (with ruby in your shell path) ruby BarChart.rb.
I think I'm missing something here. I have a directory like this:
myapp
|-lib
|-package1
|-dostuff.rb
|-package2
|-dostuff.rb
From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:
require 'lib/package1/dostuff'
returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.
What am I forgetting? The two filenames don't have to be the same but that's how they are to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)
If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.
In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.