2

Given that with the way Firefox now works, it's somewhat infeasible to specify the Save As filename no matter what kind of link, redirect or mime type watir-webdriver encounters in a completely non-interactive/non-GUI way.

What might be a simple way to find out the name of the file that's just been saved?

Hopefully something more reliable than "detect latest file created in directory" since I have independent processes necessarily using the same folder simultaneously, including other webdrivers.

Promptless download already setup (as much as possible using many foreseen file types) in profile using this method: Firefox 4 with watir webdriver: Need help using helperApps.neverAsk to save CSV without prompting

In my setup a Bash script calls Ruby which does the watir-webdriver stuff, launches the browser etc. then exits back to the Bash process. I'd prefer if Ruby learned the filename it just saved right away, since with its env it knows best how I want to rename the file, rather than leaving it to some spooky lsof/strace to figure out post mortem. But whatever works.

UPDATE

Since the answer seems to be not really, can anyone suggest short Ruby code that finds the latest file saved given a directory, checks if indeed it was written within the last 2 seconds, and renames it to my variable $acctno.csv ?

Community
  • 1
  • 1
Marcos
  • 4,796
  • 5
  • 40
  • 64
  • 1
    I think latest file in your directory is the only way to go at the moment. You could simply make sure you create a new download directory for each webdriver (using a GUID or similar), to ensure you aren't using the same folder and getting the wrong file. – Alister Scott Feb 22 '12 at 21:49
  • Ok. Revising question for Ruby workaround. – Marcos Feb 23 '12 at 10:07

2 Answers2

2

what about creating a directory using the current date-time as part of the name when the test run starts. You could also write this out to an environment variable if some later action needs to know where the files were put

Then when something has to write out a file use the name of the test or step, or something else that lets you trace it back to what wrote out the file. or just an incrementing number and have it create some kind of log file that would tell you what each number is, or what created it etc.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
  • Sure, or I was thinking when I need to create isolated folders, to name after that Ruby session's PID rather than timestamp since it's easier to trace/grab on to and still pretty unique. Already my framework is pretty aware of its process tree, for cleanup purposes & data processing isolation – Marcos Feb 23 '12 at 22:47
2

Try this, but replace Dir.pwd with your directory name

f = Dir.entries(Dir.pwd).reject{|f|File.ftype(f)!='file'}.sort_by{|f| File.mtime(f)}.last
File.rename(f, "#{$acctno}.csv") if Time.now-File.mtime(f)<2
Dave McNulla
  • 2,006
  • 16
  • 23
  • Excellent! Only I switched `mtime` to `ctime` since there are pre-existing files actively being updated in that same directory--keeping their modtimes fresh, and since I already know my target webpage always generates new unique filenames containing timestamp. So I intend the rename to overwrite like `mv -f`. Thanks! – Marcos Feb 23 '12 at 22:40
  • I wasn't sure what the best time attribute was - glad ctime worked out. – Dave McNulla Feb 23 '12 at 23:23
  • This has been giving me unexplained `in 'ftype': No such file or directory - SomeOldUnrelatedSimpleFile.png (Errno::ENOENT)` fatal errors so I switched to `f = get_last_created("myDir")` from [get_last_modified](http://snippets.dzone.com/posts/show/544) – Marcos Feb 25 '12 at 14:16
  • I got so sick or errors on that version also, that I made my own: `def get_last_created(dir) return File.open(dir +'/'+ IO.read("|ls -A1c " + dir).split.first, "r") end` and `def get_last_modified(dir) return File.open(dir +'/'+ IO.read("|ls -A1t " + dir).split.first, "r") end` These use ls but JUST WORK. The native Ruby methods kept crapping out at (temporarily)broken symlinks, or files that exist only briefly--between ruby's array building and then sorting/comparing entries--since my folder is being continuously synchronized and monitored. Spent so much time till I figured that out. – Marcos Mar 01 '12 at 18:21
  • Worked on my computer (hehe). Did you consider re-writing the call and sending in the source (with a problem description)? That might save some other souls. – Dave McNulla Mar 01 '12 at 22:02
  • Not sure where to do that. But mostly I'm sure it'd get rejected for relying on programs external to Ruby (ls/bash) – Marcos Mar 05 '12 at 09:31