Question

I've written some Ruby code (connected with Cucumber) that will go to a website and click a file that I'd like to download. The browser I'm using for this is Google Chrome.

Typically, when you go to download a file in Chrome, it doesn't ask for permission. However, when I run the code I made, it says:

"This type of file can harm your computer. Do you want to keep file_name.exe anyway?" It gives 2 options, "keep" or "discard". I have to click keep.

Obviously, you don't want all executables to just start downloading; however, this particular website/file should always be trustworthy.

Is there a command in Ruby or Cucumber that allows you to click the "keep" button automatically? This could just be a general "click at this pixel" or something. Or is there a way to mark a particular website in Chrome as safe. You can't inspect the element because it's not part of the website, but, instead, part of the browser. Preferably without having to download other software.

With this being said, this suggests that if it is possible, it should also be possible to automate an installation (as in clicking next -> next -> etc) for you. Hopefully this is correct?

Thanks in advance.

Was it helpful?

Solution 2

I actually switched to using Sikuli, which worked pretty well. Thanks for the help, though.

OTHER TIPS

You can implement it in any browser. But, for Google Chrome, here is the solution -

profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "Absolute or relative path to your download directory"

browser = Selenium::WebDriver.for :chrome, :profile => profile

You haven't specified which gem you use for browser. But, even if you use watir-webdriver, you can use the same profile you created above with watir-webdriver.

browser = Watir::Browser.new :chrome, :profile => profile

Do you really need or want the browser to download the file? Are you really testing the browser's download feature, or do you want to verify that the server can serve the file and that it is what you expect?

I found the idea of setting up a default directory and having to check for the file clumsy, fragile and prone to errors, especially when setting up on a new host, especially for tests that run in multiple browsers.

My solution is to just use Ruby (or whatever language) features to download the file directly, and then validate that it is the file it's supposed to be. I'm not testing the browser, I'm testing the software. The only exception to that idea I can think of is if you use some javascript logic or something browser-dependent to redirect you to a link, but please don't ever do that.

However, you run into a problem if you have to log in to access your file; you either have to implement auth in your Ruby code, which isn't technically part of your Cucumber specification, or you need the cookies. I use this code to copy the cookies to avoid logging in again, and grab the file:

def assert_file_link(uri, filename, content_type)
  f = open_uri_with_cookies uri

  attachment_filename = f.meta["content-disposition"].sub("Attachment;filename=", "") # "Attachment;filename=Simple Flow - Simple Form.rtf"
  content_length = Integer(f.meta["content-length"])

  assert(f.status == ["200", "OK"], "Response was not 200 OK")
  assert(f.content_type == content_type, "Expected content-type of '#{content_type}' but was '#{f.content_type}'")
  assert(attachment_filename == filename, "Expected filename of '#{filename}' but was '#{attachment_filename}'")
  assert(content_length > 0, "Expected content-length > 0 but was '#{content_length}'")
end

def open_uri_with_cookies(uri)
  # hack the cookies from the existing session so we don't need to log in!
  cookies = ""
  @driver.manage.all_cookies.each { |cookie| cookies.concat("#{cookie[:name]}=#{cookie[:value]}; ") }

  if block_given?
    open(uri, "Cookie" => cookies, :proxy => nil) do |f|
      yield f
    end
  else
    open(uri, "Cookie" => cookies, :proxy => nil)
  end
end

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top