简体   繁体   中英

How do you open a zip file using watir-webdriver?

My test suite has a cucumber front end with a ruby backend, running the latest version of watir-webdriver and its dependencies atop the latest version of OSX. My cucumber environment is setup to execute in Firefox.

The export feature of our app creates a zip file but to test the import feature, I need the contents of the zip file.

My actual test needs to unpack that zip file and select the individual files in it for use in testing the import feature of our web application.

Can anyone point me to a reference that can help me figure out how to write that?

Based off my experience, you download this file the same way that a normal user might. So first off, you just click the download button or whatever and then can access the file wherever it is and check out its contents.

Assuming the downloads just go to your Downloads folder by default, there is some simple code you can use to select the most recently downloaded item:

fn = Dir.glob("~/Downloads/*.zip").max { |a,b| File.ctime(a) <=> File.ctime(b)}

Then just use the unzip shell command to unzip the file. No reason to add another gem into the mix when you can just use generic shell commands.

`unzip #{fn}`

Then, you'd use Dir.glob again to get the filenames of everything inside the unzipped files folder. Assuming the file was named "thing.zip", you do this:

files = Dir.glob("~/Downloads/thing/*")

If you want to files to be downloaded directly to your project folder, you can try this. This also prevents the popup from asking you if you really want to save the file which is handy. I think this still works but haven't used it in some time. The above stuff works for sure though.

profile = Selenium::WebDriver::Firefox::Profile.new    
download_dir = Dir.pwd + "/test_downloads"
profile['browser.download.dir'] = download_dir
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/zip"
b = Watir::Browser.new. :firefox, :profile => profile

I ended up adding the rubyzip gem at https://github.com/rubyzip/rubyzip the solution is on that link but i modified mine a little bit. I added the following to my common.rb file. see below:

require 'Zip'

  def unpack_zip

  test_home='/Users/yournamegoeshere/SRC/watir_testing/project files'

  sleep(5) #<--manually making time to hit the save download dialog

  zip_file_paths = []
  Find.find(test_home) do |path|
    zip_file_paths << path if path =~ /.*\.zip$/
  end

  file_name=zip_file_paths[0]

  Zip::File.open(file_name) do |zip_file|

    # Handle entries one by one
    zip_file.each do |entry|

      # Extract to file/directory/symlink
      puts "Extracting #{entry.name}"
      entry.extract(test_home + "/" + entry.name)

      # Read into memory
      content = entry.get_input_stream.read
    end

    # Find specific entry
    entry = zip_file.glob('*.csv').first
    puts entry.get_input_stream.read
  end
end

This solution works great!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM