Search code examples
jqueryajaxselenium-webdrivercapybaragoogle-chrome-headless

Failed to download file when click the element that call ajax function on headless automation using capybara


i have a link element, when user/automation click the link, it will download a file, the link is like this:

<a target="_blank" href="#!" class="downloadFileAws" data-file="report2020-05-20_2020-05-20_jbkd2a1jYFFTyEEUrjiiQBLCQHsjWqj21375.xls" data-path="report">here</a>

when the automation click that link, it will call the ajax function as you can see below:

$('body').on('click', '.downloadFileAws', function(e){
  e.preventDefault();
  var $this = $(this);
  $this.addClass('disabled');
  var folderName =  $this.attr('data-path');
  var fileName =  $this.attr('data-file');
  $.ajax({
    type: 'POST',
    url : $('#getUrlFileAws').val(),
    data: {
      folderName  : folderName,
      fileName    : fileName
    },
    dataType:'JSON',
    success: function (data)
    {
       $this.removeClass('disabled')
       if(data.status === 'success')
       {
          window.open(data.url,'_blank');
       }
       else
       {
          Materialize.toast('File Not Found.<i class="fa fa-times ml25"></i>', 3000,'red accent-4');
       }
    }
  });
})

that ajax function is not attach directly on the html page, but located in a javascript file named "mainscript.js". and the file attach on the bottom of the html page, like this:

<script type="text/javascript" src="/assets/js/mainscript.js?v=100000301"></script>

however, when the headless automation tried to click the link element, the ajax function doesnt called. i have tried click with different way, for example:

find(:css, 'a.downloadFileAws').click

or

page.execute_script('$("a.downloadFileAws").click()')

but no one seems working, i tried to running automation on non-headless mode, and it work like a charm, the downloaded file appear on download directory inside my automation project directory.

here i will give you additional information about my config in env.rb:

browser_options = Selenium::WebDriver::Chrome::Options.new
browser_options.add_preference('download.default_directory', 
File.absolute_path('./features/data/files/downloaded'))
browser_options.add_preference(:download, default_directory: 
File.absolute_path('./features/data/files/downloaded'))
browser_options.add_preference(:browser, set_download_behavior: { 
behavior: 'allow' })

browser_options.add_preference('plugins.always_open_pdf_externally', true)
browser_options.add_preference(:plugins, always_open_pdf_externally: true)

Capybara.register_driver :chrome_headless do |app|
  browser_options.add_argument('--headless')
  browser_options.add_argument('--no-sandbox')
  browser_options.add_argument('--disable-gpu')
  browser_options.add_argument('--disable-dev-shm-usage')

  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: browser_options
  )
end

Capybara.register_driver :chrome do |app|
  profile = Selenium::WebDriver::Chrome::Profile.new

  browser_options.add_argument('--user-agent=selenium')
  browser_options.add_argument('--start-maximized')
  client = Selenium::WebDriver::Remote::Http::Default.new
  client.open_timeout = wait_time
  client.read_timeout = wait_time
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: browser_options,
    http_client: client,
    profile: profile
  )
end

any help will be so much appreciate as i already stuck in this problem for days, so thank you everyone.

EDIT: i already solved the problem by adding some code when registering chrome_headless driver:

Capybara.register_driver :chrome_headless do |app|
  browser_options.add_argument('--headless')
  browser_options.add_argument('--no-sandbox')
  browser_options.add_argument('--disable-gpu')
  browser_options.add_argument('--disable-dev-shm-usage')

  driver = Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: browser_options
  )

  driver.browser.download_path = File.expand_path('./features/data/files/downloaded')

  driver
end

Solution

  • Download behavior has to be set differently depending on Chrome version and mode (headless vs non-headless). Using the configuration Capybara uses for its own tests should work across any recent versions in both modes - see https://github.com/teamcapybara/capybara/blob/master/spec/selenium_spec_chrome.rb#L13

    You also shouldn't be using desired_capabilities anymore, and you should definitely stop disabling w3c mode unless you're on a really old version of Chrome in which case download in headless mode is likely not to work at all.