5

For example, consider the following script:

require 'rubygems' 
require 'watir-webdriver' 

browser = Watir::Browser.new(:firefox)
browser.goto("http://tinypic.com/") 

file = "C:\\\\assets\\image.jpg"
browser.file_field(:name,"the_file").set(file)  
browser.close

Which will work, I will be able to add the file into the file_field but I always have to have image.jpg in C:\\assets and I don't want this to always be enforced. I'd like to search the underlying OS and find the path for the file I want to use and I'd like the script to be able to run on Windows and *nix systems. Relying on hard-coding the file's location as a string is somewhat limiting.

It should help to know that the files I wish to use will be under source control and will always be in a specific location in relation to the test script - the directory will always be something like ../../assets.

5
  • 1
    This might be good on Stack Overflow, since it's essentially a coding question. May 25, 2011 at 22:57
  • Yeah, fair point. Stackoverflow is where I used to submit these types of questions. Although this site is intended to include browser automation questions so I'd hope that an answer would also be possible to find here. :) May 25, 2011 at 23:57
  • I like it here, too. There is some overlap between SE sites. I just wasn't seeing many answers at that time :) May 27, 2011 at 17:23
  • 1
    I'd prefer this kind of question be placed on SO. There's more of the watir people monitoring that site, and also better assistance for aspects of these questions that end up dealing with things like Ruby, JQuery, etc May 30, 2011 at 1:01
  • 1
    There are more Watir people monitoring SO at the moment, but seeing as how Selenium was merged into SQA, it only makes sense that Watir questions should also be here. There is overlap, for sure. In fact, EVERY question on SQA could be on SO or Programmers instead - the idea is to bring them together to bring the philosophy of testing from Programmers to the technical aspects from SO under one roof. They're called growing pains for a reason :-)
    – corsiKa
    May 30, 2011 at 3:19

4 Answers 4

5

create a path provider where you test data is located. in a file create a method that returns expanded path

def datapath filename
  File.expand_path(File.join(File.dirname(__FILE__), filename))
end

use it in your test

browser.file_field(:name,"the_file").set(datapath(filename))
1

One approach that my work is to extract paths like this to a config file of some point that could be set depending on where you run the script from. Then you can read the value out of the config at start up. Another approach I have taken in C# is to have a directory that is part of my project that gets built out with the project so if I have dependencies they are build out as part of the project so they are always in a path relative with the binary.

0
@modal = @browser.driver.switch_to.alert   #Switch to open windows modal
key_to_send = "C:\\Users\\singhku\\Calabash_doc.pdf"  #Path and name of file
@modal.send_keys(key_to_send)

require 'win32ole'
wsh = WIN32OLE.new('Wscript.Shell')
wsh.AppActivate('Choose File to Upload')  #Name of the modal that is open
wsh.SendKeys('{ENTER}')
-1
file = "#{Dir.pwd}" + '/assets/image.jpg'

Make sure to add the folder inside your frame-work and commit it just so if you are running it on Linux os it will find it as well.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.