2
votes

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 ?

2
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
Ok. Revising question for Ruby workaround.Marcos

2 Answers

2
votes

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.

2
votes

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