0

I am using following code to download CSV file and its working properly on MAC but on Windows its not working.

On window it download file but it save it in download folder i.e. default path

    def self.launch(browser=:firefox, profile=nil)
        profile = Selenium::WebDriver::Firefox::Profile.new
        path = File.join(File.join(Dir.pwd), 'csv_files')
        FileUtils.rm_rf(path) if  Dir.exists? path
        Dir.mkdir(path)
        profile['browser.download.dir'] = path
        profile['browser.download.folderList'] = 2
        profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/csv'
        profile['pdfjs.disabled'] = true
        $watir_browser = UITest.new_browser_session browser, profile
        $driver = $watir_browser.wd
        return $watir_browser
      end

Please suggest me if any changes are required.

2
  • They are default settings. If you try to Save As, then you can set your custom path. Commented Aug 5, 2014 at 11:54
  • @Sham I am using custom path in my code Commented Aug 5, 2014 at 12:23

2 Answers 2

1

Problem

When you do the line:

path = File.join(File.join(Dir.pwd), 'csv_files')

The path will be:

'some/path/csv_files'

In Windows, the "/" needs to be "\":

'some\path\csv_files'

Solution

What you can do is replace the slashes when the platform is Windows:

path = File.join(File.join(Dir.pwd), 'csv_files')
path.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
Sign up to request clarification or add additional context in comments.

Comments

1

Please try this else let me know.

relative_path = File.expand_path File.dirname(__FILE__)

OR

require 'open-uri'
require 'selenium-webdriver'
 path = File.join(File.join(Dir.pwd), 'csv_files')
    Dir.mkdir(path) unless File.exists?(path)
    modified_path = path.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
    File.delete(File.join(modified_path, 'test.csv')) if File.exist?(File.join(modified_path, 'test.csv'))
    open(File.join(modified_path, 'test.csv'), 'wb') do |file|
      file << open('http://example.com').read
    end

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.