Hi friends I want to test file download in capybara.
I have tried
page.response_headers['Content-Type']
But it raises Capybara::NotSupportedByDriverError exception.
I have also tried
page.driver.browser.switch_to.alert.text
It fails with the message 'No alert is present'
Any help appreciated.
Thank you.
Here is an example of testing with RSpec and Capybara for a 'login' page: DatabaseCleaner: As the name says, it is a tool used to clean the database after tests.
Capybara tests are often called “feature tests” because, unlike unit tests, they test complex program behaviors that rely on multiple systems within your code. Go ahead and create a spec/features directory to store these tests in later. Database_cleaner takes some more love to get working.
Capybara is a great tool for testing your Rails app. Recently I had cause to test uploading files through a particular UI. Here is how you go about in Capybara. What this tells Capybara is look for a file upload input with the label “Upload Your File” and then inserts the file specified.
I use the database_cleaner gem to scrub my test database before each test runs, ensuring a clean slate and stable baseline every time. By default, RSpec will actually do this for you, running every test with a database transaction and then rolling back that transaction after it finishes.
I tested that when I click on a link I should receive *.pdf file.
click_on "link_to_pdf"
page.response_headers['Content-Type'].should eq "application/pdf"
It doesn't work with selenium, so better to specify not javascript driver. I am sure that it works with RackTest.
feature "Download pdf", :js => false do
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With