Remove file after Capybara test in Rails 7 - ruby-on-rails

In my Rails 7 app I've got Capybara test which checks if downloading a PDF file works. The issue is that after a successful check, Capybara saves this file in the main path of the project. How do I delete this file right after the test?
it 'download invoice' do
payment = build :isor_payment, :with_pdf
stub_payment(payment)
login_as user
visit payment_path(payment.platform_payment_id)
click_on 'Download'
expect(page).to have_content I18n.t('payments.main_data_component.invoice_pdf')
end
After that test it will save me a pdf named payment-43523452.pdf.

I'm assuming you're using Rspec as your test runner. You can configure an after(:each) callback to remove the file:
RSpec.configure do |config|
# all the rspec configuration options...
config.after(:each) do
FileUtils.rm('payment-43523452.pdf')
end
end
But this is a little brittle, you have to change the configuration whenever your download file name changes. I prefer to create a download directory under tmp/, like tmp/test_downloads/, then use the above callback method to remove all files from the tmp/test_downloads/ directory.
Configure the download file directory in the Capybara driver configuration, usually in spec_helper.rb. My driver configuration looks like this:
Capybara.register_driver :headless_chrome do |app|
chrome_options = Selenium::WebDriver::Chrome::Options.new
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1400,800')
chrome_options.add_preference(:download,
directory_upgrade: true,
prompt_for_download: false,
default_directory: 'tmp/test_downloads/'
)
chrome_options.add_preference(:plugins, always_open_pdf_externally: true)
driver = Capybara::Selenium::Driver.new(app, :browser => :chrome, :capabilities => [chrome_options])
driver
end
Then your RSpec after(:each) callback will be:
FileUtils.rm_rf(Dir.glob("tmp/test_downloads/*"))

Related

What can cause Capybara::Poltergeist::TimeoutError: in group testing that doesn't individually?

A Rails app that uses remote testing to test a CakePHP app running on a Vagrant Ubuntu VM.
My OS is macOS High Sierra.
'rspec/rails'
'capybara/rails'
'capybara/mechanize'
'capybara/poltergeist'
'phantomjs'
If I run rspec ./spec/features/my_tests_folder/,
the first 2 tests in the folder always pass and the rest always end up with Capybara::Poltergeist::TimeoutError:.
If I run any of the tests in that folder individually, they ALL pass ALWAYS.
There are 7 test files total. They each have 1 feature with 1 scenario. All are js: true.
I have tried increasing :timeout in Capybara.register_driver and increasing default_max_wait_time in Capybara.configure. Neither changed the outcome.
I've also played around with Capybara.reset! after and before each test. It didn't seem to matter, either.
When I ran this with config.order = :random, sometimes 5 out of 7 had the erros, sometimes only 2 out of 7. But, there were always some errors and some passing. Also, every test was in the errors group at least once.
I'm running out of ideas. What could cause something like this?
UPDATE (to include Capybara and Poltergeist configs and example of a failing test):
Configs in rails_helper.rb:
Capybara.register_driver :poltergeist do |app|
options = {
:timeout => 90, # default is 30
:js_errors => false,
:phantomjs => Phantomjs.path,
# :debug => true
}
Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.register_driver :mechanize do |app|
driver = Capybara::Mechanize::Driver.new(app)
driver.configure do |agent|
agent.user_agent_alias = 'Mac Safari'
end
driver
end
Capybara.configure do |config|
config.run_server = false
config.app_host = "http://my_vm_domain.com"
config.javascript_driver = :poltergeist
config.default_driver = :mechanize
config.default_max_wait_time = 10 # default is 2
end
Example of failing test (not failing, but getting Capybara::Poltergeist::TimeoutError:):
require 'rails_helper'
feature 'agente visualiza estoques de um passeio', js: true do
scenario 'com sucesso' do
agente_log_in
visit '/roteiro/show/723'
find(:partial_href, 'new_passeio/723').click
select 'Passeio Teste', from: 'passeio_produto'
fill_in 'passeio_data', with: '11/11/2017'
within('button#estoque_controls_0') do
within('div#estoque_0_hora') do
expect(page).to have_content('07:30')
end
within('span#estoque_0_vagas') do
expect(page).to have_content('3')
end
end
within('button#estoque_controls_1') do
within('div#estoque_1_hora') do
expect(page).to have_content('10:00')
end
within('span#estoque_1_vagas') do
expect(page).to have_content('5')
end
end
end
end
Code from agente_log_in.rb in support folder:
def agente_log_in
Capybara.app_host = "http://my_vm_domain.com"
visit '/usuario/logout'
visit '/'
fill_in 'data[AdmUsuario][usuario]', with: 'agente'
fill_in 'data[AdmUsuario][senha]', with: 'pa$$w0rd'
click_on 'Entrar'
end
Code for that :partial_href find:
module Selectors
Capybara.add_selector(:partial_href) do
xpath {|href| XPath.descendant[XPath.attr(:href).contains(href)] }
end
end
Everything is fine with the other tests that are in the app's other folders. They're also fine if I run the tests in this folder individually. The problem only seems to happen when I run THIS specific folder as a whole.
After including the extra information requested by Thomas Walpole, I continued searching and studying possibilities.
I eventually came across poltergeist's issue #781 on GitHub, which describes a situation very similar to mine, and eventually presents a wait_for_ajax solution.
Before implementing it in my project, I read more about waiting for Ajax and found this post very helpful, too.
In the end, I chose jasonfb's code from GitHub because it seemed more thorough and informative.
It worked like a charm! My tests now pass consistently. I was even able to remove customization for :timeout and default_max_wait_time.
The CakePHP app in question is very js heavy and the specific part that this folder tests is particularly full of Ajax requests.

Issues with system test setup using Capybara and Selenium on Rails 5.1 app (upgraded from Rails 4)

I'm attempting to set up system tests with Capybara and Selenium on an existing Rails 5.1 (Upgraded from Rails 4) app that already had capybara based feature tests. Here's what I've done so far.
In the gem file under group :development, :test:
gem 'chromedriver-helper'
gem 'selenium-webdriver'
gem 'rack_session_access'
In the environments/development.rb and environments/test.rb:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
In the spec\rails_helper.rb:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.configure do |config|
config.default_max_wait_time = 10 # seconds
config.default_driver = :selenium
config.app_host = 'http://localhost:3000'
config.server_host = 'localhost'
end
The issues I'm having are both with new systems tests and old feature tests.
With the system tests it appears that Capybara isn't creating a page object as I get undefined local variable or method 'page' Additionally when I duplicate the same test under the feature test directory I don't have this issue.
With the old Capybara feature tests, working with the rackTest driver, a Chrome window opens but I get No route matches [GET] "/rack_session/edit"
config.middleware.use RackSessionAccess::Middleware is already present in the environments/test.rb
Example system test:
require 'rails_helper'
describe User do
let(:user) { create :user }
let(:membership) { create :membership, admin: true}
let(:admin) { create :user, memberships: [membership] }
context 'viewing the index' do
it 'directs you to the appropriate page' do
set_current_user(admin)
visit root_url
click_button 'Manage Users'
expect(page.current_url).to end_with users_path
expect(page).to have_selector 'h1', text: 'Users'
end
end
end
Example feature test:
require 'rails_helper'
describe 'edit an assignment' do
let(:roster) { create :roster }
let(:user) { create :user, rosters: [roster] }
before :each do
Timecop.freeze Date.new(2018, 1, 10)
set_current_user(user)
end
after :each do
Timecop.return
end
context 'returns the user to the appropriate index page' do
let(:date_today) { Date.new(2017, 4, 4) }
let(:start_date) { Date.new(2017, 3, 31) }
let(:month_date) { date_today.beginning_of_month }
it 'redirects to the correct URL' do
visit roster_assignments_url(roster, date: date_today)
visit new_roster_assignment_url(roster, date: start_date)
click_button 'Create'
expect(current_url)
.to eq roster_assignments_url(roster,
date: month_date)
end
In the spec_helper:
def set_current_user(user)
page.set_rack_session user_id: user.id
end
You need to have the gem puma installed. New rails 5 projects have it installed by default, but your app was made in Rails 4, which is why it didn't.
Why is this? Well, if you were to do a bundle update (and I'll admit I can't explain why) you'd get this error when trying to run the specs, which is a lot more explanatory:
System test integration requires Rails >= 5.1 and has a hard dependency on a webserver and `capybara`, please add capybara to your Gemfile and configure a webserver (e.g. `Capybara.server = :webrick`) before attempting to use system tests.
Googling this error led me to this page, which explains Capybara needs a server.
After adding puma, I'm able to run system tests on your application.
A number of issues here.
Why are you setting - config.app_host = 'http://localhost:3000' ??? That would run the tests against your dev instance rather than the test instance Capybara starts. app_host really should only ever need to be set if you are doing subdomain based testing. This could be the reason for the no route error (normally rack_session_access would only be included in the test environment), or that could be caused by having not actually included the middleware as specified in the rack_session_access gem readme.
NEVER do expectations against current_path/current_url directly, instead use the provided matchers or you'll have flaky tests
expect(page).to have_current_path(users_path)
page is just an alias for Capybara.current_session and is a member of the module Capybara::DSL. If it's not available in the scope of your tests that it most likely means Capybara::DSL isn't included. That would normally be done by rspec-rails - https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/vendor/capybara.rb#L21 - so it's possible you haven't actually set the test type to 'system'. If it's that it's not available in your spec_helper methods, just using Capybara.current_session instead is usually easier.

How to use selenium and poltergeist together in RSpec?

I have a rich frontend in my application. Some of my tests not works well with poltergeist, because of animations and AJAX requests, but works fine with selenium.
How can i use them together in one project and in one test session?
If you're using the standard RSpec configuration with Capybara (require 'capybara/rspec') then you can override the normal driver that would be used for a given test with :driver metadata
it "should do something", driver: :selenium do
# will use the selenium driver for this test
end
it "should do something else", driver: :poltergeist do
# will use the poltergeist driver for this test
end
that could also be specified on the enclosing feature if you want the whole feature to use a specific driver
feature "blah balh", driver: :selenium do
# all scenarios here would use the selenium driver unless overridden with their own :driver metadata
I found solution.
Created macros in spec/support/selenium_macros.rb:
module SeleniumMacros
def use_selenium_webdriver
before(:all) do
Capybara.javascript_driver = :selenium
Capybara.current_driver = :selenium
end
after(:all) do
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
end
end
spec/rails_helper.rb
RSpec.configure do |config|
config.extend SeleniumMacros, type: :feature # add macros for acceptance tests
using example
spec/features/example_feature_spec.rb
feature 'Add files to question' do
use_selenium_webdriver
this feature will be work with selenium, after it will be executed it activates poltergeist webdriver.
P.S. Sorry for my english.

Disabling JavaScript when using Poltergeist and Capybara's default driver

My setup is using poltergeist as the Capybara driver for all my tests, both JS and non-JS.
# spec/rails_helper.rb
require "capybara/poltergeist"
# ...
# ...
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: true)
end
Capybara.configure do |config|
config.ignore_hidden_elements = true
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
I have some tests where I confirm that certain features on my app are still working even with javascript disabled. For those tests, I of course disable javascript with js: false.
describe "accessibility" do
describe "JavaScript disabled", js: false do
before(:each) { visit root_path }
it "user can still log in" do
# ...
end
end
end
However, I'm noticing that these js:false tests still use JavaScript. I can confirm this by printing debug statements to the console log in JavaScript.
Is there a way to disable JavaScript when using poltergeist? Or is it always enabled? Is it even valid to use poltergeist as a non-JS driver?
Thanks!
No, there doesn't seem to be a way to use poltergeist without Javascript (unless you modify poltergeist yourself). According to this Github issue it would require support in phantomjs, which is available in a patch but not in master.

Downloading file to specific folder using Capybara and Poltergeist driver

I am writing my acceptance tests using Capybara and Poltergeist driver.I need to validate the content of the CSV file downloaded.
I tried various ways of rendering the content on the page itself instead of downloading it.
Also tried changing the mime types, but it is not working.
Finally I want to settle down with the option of downloading the file in a specific folder and then read the CSV file using core ruby libraries.
In order to achieve this,when poltergeist driver clicks on download link then I want it to handle the pop-up and download the file directly in the given folder.
In Selenium's chrome and firefox drivers, I have option of configuring profiles to handle pop ups and configure download directory.
Is there any such option using poltergeist? Any information will be helpful.
It is not possible with Poltergeist, you can just check the headers.
step 'I should get zipped file' do
page.response_headers['Content-Disposition'].should include("filename=\"file.zip\"")
end
But is is possible with Chrome driver and also with recent versions of Firefox and Selenium Webdriver. Unfortunately it runs via Selenium - i.e. not headless... See this article: http://collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver/
My approach - slightly different as I'm working with Spinach and Rubyzip:
Add the following to your Gemfile
group :test do
gem 'chromedriver-helper' # for Chrome <= 28
gem 'chromedriver2-helper' # for Chrome >= 29
gem 'selenium-webdriver'
end
features/support/capybara.rb - I'm using Poltergeist for scenarios with #javascript tag and Chrome for scenarios with #download tag.
require 'spinach/capybara'
require 'capybara/poltergeist'
require 'selenium/webdriver'
# ChromeDriver 1.x, for Chrome <= 28
Capybara.register_driver :chrome do |app|
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.default_directory'] = DownloadHelper::PATH.to_s
args = ["--window-size=1024,768"]
Capybara::Selenium::Driver.new(app, browser: :chrome, profile: profile, args: args)
end
# ChromeDriver 2.x, for Chrome >= 29
Capybara.register_driver :chrome do |app|
prefs = {
download: {
prompt_for_download: false,
default_directory: DownloadHelper::PATH.to_s
}
}
args = ['--window-size=1024,768']
Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs, args: args)
end
# Tested with Firefox 27 and Selenium Webdriver 2.39
Capybara.register_driver :firefox do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = DownloadHelper::PATH.to_s
profile['browser.download.folderList'] = 2 # 2 - save to user defined location
profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/zip'
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end
Capybara.javascript_driver = :poltergeist # :webkit :selenium :poltergeist :chrome
Spinach.hooks.on_tag("javascript") do
Capybara.current_driver = Capybara.javascript_driver
Capybara.default_wait_time = 5
end
Spinach.hooks.on_tag("download") do
Capybara.current_driver = :chrome # or :firefox
Capybara.default_wait_time = 50
end
features/support/downloads.rb
module DownloadHelper
TIMEOUT = 10
PATH = Rails.root.join("tmp/downloads")
extend self
def downloads
Dir[PATH.join("*")]
end
def download_path
wait_for_download
downloads.first
end
def download_content
wait_for_download
File.read(download_path)
end
def wait_for_download
Timeout.timeout(TIMEOUT) do
sleep 0.1 until downloaded?
end
end
def downloaded?
downloads.any? && !downloading?
end
def downloading?
downloads.grep(/\.crdownload$/).any?
end
def clear_downloads
FileUtils.rm_f(downloads)
end
end
Spinach.hooks.before_scenario do |scenario|
DownloadHelper.clear_downloads
end
Spinach.hooks.after_scenario do
DownloadHelper.clear_downloads
end
features/file_download.feature
Feature: File download
As a user
I want to be able to download my files
Background:
Given I am logged in as a user
And I have uploaded files in the system
#download
Scenario: Successfull download
When I click on the download button
Then I should get zipped files
features/steps/file_download.rb - Note that you can't use page.response_headers as it is not supported by the Selenium/ChromeDriver. But you can check the filename of the downloaded file using the File.basename().
class Spinach::Features::FileDownload < Spinach::FeatureSteps
include SharedAuthentication
step 'I click on the download button' do
click_link "Download"
end
step 'I should get zipped files' do
File.basename(DownloadHelper.download_path).should == 'file.zip'
Zip::ZipFile.open(DownloadHelper.download_path) do |zipfile|
zipfile.find_entry('myfile.txt').should_not be_nil
zipfile.find_entry('myphoto.jpg').should_not be_nil
end
end
end
I've had to do similar things in my rails app. My solution is using Javascript to make a XMLHttpRequest to the URL, downloading the file, returning the contents of the file back to Capybara, and using ruby to save the file somewhere on disk. Then in another step, I check the contents to the downloaded CSV file.
Here's the step definition for downloading the file:
Then /^I download the csv file$/ do
page.execute_script("window.downloadCSVXHR = function(){ var url = window.location.protocol + '//' + window.location.host + '/file.csv'; return getFile(url); }")
page.execute_script("window.getFile = function(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, false); xhr.send(null); return xhr.responseText; }")
data = page.evaluate_script("downloadCSVXHR()")
File.open(File.join(Rails.root, "tmp", "csv.data"), "w") { |f| f.write(data) }
end
Change the URL in the Javascript code to your CSV's location.
And finally, here's my step definition for validating the CSV file's contents:
And /^the contents of the downloaded csv should be:$/ do |contents|
file = File.open(File.join(Rails.root, "tmp", "csv.data"), "r")
file_contents = file.read
file_contents.chop!
file_contents.should == contents
end
Good luck. Hope this helps.
This is not currently possible with Poltergeist.
I think you'd be better off writing a test for this CSV which doesn't use Capybara. (E.g. by using the built-in Rails integration testing stuff and parsing the response as a CSV.)
There is an ticket to support downloading files in PhantomJS/Poltergeist and there are one or two forks which claims that they made it to work somehow. See https://github.com/ariya/phantomjs/issues/10052

Resources