0

Due to a project requirement I had to move from Selenium 2.0 to Watir and Cucumber framework. Earlier we were using Java and often set the default file path as:

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/browsers/chromedriver.exe");

Now I am trying to set up something similar in Ruby also, but being new to Ruby, I'm failing to get the root directory of the project. It only gets the path of the file which the current user is working using __FILE__ or Dir.pwd.

My Watir project structure is like this:

root
-config
-features
  -pages
  -step_definitions
  -support
      -hooks.rb
-browsers
...
... 

I want to specify in hooks.rb that if the parameter passed is "Chrome", then run the Chrome browser, and when it is "FF" run the Firefox browser, but it always gives me the current working directory path and I need the root directory.

I am on WINDOWS 7, Using Ruby version 1.9.3p551.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
paul
  • 4,333
  • 16
  • 71
  • 144

2 Answers2

1

Ruby's File class has several different methods that are useful for what you're trying to do, so read the documentation for absolute_path, expand_path, realdirpath and realpath.

I'd recommend starting with absolute_path:

Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. If the given pathname starts with a “~” it is NOT expanded, it is treated as a normal directory name.

I built the path from your hooks.rb to the root directory in my tests directory on my Desktop, and added this code to hooks.rb:

PATH_TO_ROOT = File.absolute_path('../..', File.dirname(__FILE__))

Running it shows:

PATH_TO_ROOT # => "/Users/ttm/Desktop/tests/ruby/root"

You'll always have to maintain the '../..' relative string but that is easier than hard-wiring your code with a fixed absolute path.

I tend to put code similar to this in any project that has multiple directories, especially when I'm calling library files. Write your code correctly, and you can do it once assigning the value to a constant, and you can then reference that constant wherever you need to know the absolute path to the installation.


book = Roo::Excel.new(File.join(File.absolute_path('../..', File.dirname(__FILE__)),"config/data/test.xls"))

It's a lot easier than that:

path_to_book = File.absolute_path('../../config/data/test.xls', File.dirname(__FILE__))
path_to_book # => "/Users/ttm/Desktop/tests/ruby/root/config/data/test.xls"

File.dirname(__FILE__) is the anchor for your relative path. Simply define the relative path to the file you want and let Ruby do the rest.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • `book = Roo::Excel.new(File.join(File.absolute_path('../..', File.dirname(__FILE__)),"config/data/test.xls"))` finally worked for me. Thanks – paul Dec 31 '14 at 08:39
  • Close, but not simple enough. See the added content in the answer. – the Tin Man Jan 01 '15 at 17:24
0

"Relative path to your project directory" talks about relative pathing in Ruby.

The answer to your question is going to be specific to the code that you are trying to use in your hooks.rb. If you could post a sample of what code you are using for this from your hooks.rb, that would be great. But the answer to your question is likely going to be some combination of the following:

File.join(File.dirname(__FILE__), '../../')

Where the first parameter is your current directory (root\features\support) and the second parameter is going to be the relative path to your root directory ('../../').

Community
  • 1
  • 1
Jake S
  • 41
  • 8