The ActionDispatch module of Rails gives you the helper method flash to access the flash messages in a response.
We prefer to run our end-to-end tests with headless Chrome. While it's a very stable solution overall, we sometimes see the headless Chrome process freeze (or the...
...Capybara driver losing connection, we're not sure). The effect is that your test suite suddenly stops progressing without an error. You will eventually see an error after a long...
We currently test most of our gems on Travis CI, but want to migrate those tests to Github Actions. This is a step-by-step guide on how to do...
...Gemfiles needed which database, but is not currently very smart about it. If your test.yml does not set up the database correctly, please adjust it by comparing it with...
When your cucumber features grow massively over time, the test execution can take a lot of time. One easy way to speed up your test execution is to use the...
...parallel_tests gem. It comes along with some useful rake tasks that let you setup your local test environment shortly to run your features, specs or unit-tests in parallel...
...and will cause unexpected errors with Selenium. You will see errors in your integration tests like below if you are affected by the issue: The field's content "" did not...
After removing the line above in the configuration it could happen that the tests are no longer running in headless mode. Updating the selenium-webdriver gem to >= 3.13.0 fixed...
Jasmine has spyOnProperty(), but it only works if the property is implemented using getter and setter functions. This is a...
...the print stylesheet. This card describes how to write a simple Cucumber feature that tests some aspects of a print stylesheets. This way, the requirement of having a print stylesheet...
...is manifested in your tests and cannot be inadvertedly removed from the code. Note that you can always use your DevTools to preview the print styles in your browser.
...Here is a snippet that shows the current settings and lets you send a test mail directly from the console: mailer = ActionMailer::Base.new # check settings: mailer.delivery_method # -> :smtp mailer.smtp_settings...
...authentication: nil, enable_starttls_auto: true } # send mail: mailer.mail(from: 'sender@example.com', to: 'recipient@example.com', subject: 'test', body: "Hello, you've got mail!").deliver You could use this to test the spam...
To run a single test file: rake test:units TEST=test/unit/post_test.rb rake test:functionals TEST=test/functional/posts_controller_test.rb rake test:integration TEST=test/integration/admin_news_posts_test.rb You may even run a single test method:
...I test test/unit/post_test.rb -n "name of the test" ruby -I test test/functional/posts_controller_test.rb -n test_name_of_the_test # underscored, prefixed with 'test_' Or all tests matching a regular expression:
...wait until one of the two requests is finished. Concurrent requests in development and tests Normally having multiple threads for development is fine. Puma can handle blocking threads e.g. when...
...count = Integer(ENV['RAILS_MAX_THREADS'] || 5) Note: Having multiple workers in development and tests might cause unexpected issues (database transactions and other shared states), you should not enable it...
...write the password hash to a file: echo firstname.lastname:$2y$05$4JXxd2GM/J2...9c3KJmFS > htpass_test Check, if it is correct: htpasswd -v htpass_test firstname.lastname You probably should not use...
Geordi uses parallel_tests if available for running the test suite. To debug an application it is very unhandy to have multiple processes as your terminal I/O will not work...
...as expected once a breakpoint is hit. Even parallel_tests support an option to enable a single process run, it is not possible to pass this option through geordi. But...
end Note that you need to tag the scenario with @allow-rescue to test that an error is shown like this @allow-rescue Scenario: Accessing the admin area requires...
Capybara drivers will usually delete all cookies after each scenario. If you need to lose cookie data in the middle...
...to config/initializers.rb. In the same initializer, type a line: Undebug.trace_message('foobar') Now run tests or whatever you need to do to to trigger that message. The console output should...
...a problem when using Selenium with Firefox. We recommend using ChromeDriver for your Selenium tests. Firefox will not trigger focus/blur events when its window is not focused. While this makes...
...sense in standard usage, it breaks in parallel test execution. Please do not rely on focus events in your tests. The linked card has an example of how to build...
Instead of running all missing migrations on your test database with rake db:migrate RAILS_ENV=test you can also use a handful of rake tasks to prepare the database...
...structure directly. They can produce different results, though. In a nutshell, to ensure your test database gains the correct structure: Don't use rake db:test:prepare carelessly
...by "regular" browsers, search engine crawlers, or other web client software. Cucumber In Rack::Test, you can set your user agent like this on Capybara: Given /^my user agent is...
...User-Agent', agent) # Or, for older Capybaras: # page.driver.header('User-Agent', agent) end For Selenium tests with Firefox, it seems you can set the general.useragent.override profile setting to your preferred value...
...it has not yet recorded. Some signs an error is caused by randomness The test is green on a single run but red when run in parallel
...always green on recording but red when the cassette is used The test is green on one day and red the following day Finding the randomness Taking a close look...
...when two focus-sensitive Selenium scenarios run at the same time (probably with parallel_tests). This can help you to detect and fix flickering integration tests. The attached feature works...
...you'll see). Default is 5 minutes. It should be longer than your flickering tests take to run. Make sure that your root_path contains a form element (select, input...
...external URL (like http://somehost.com/some/path) you will find that this is hard to test with Cucumber and Capybara: A non-Javascript Rack::Test scenario will just ignore the host...
...and try to open /some/path in your local application A Selenium test will actually follow the redirect, which you probably don't want either There are two workarounds for this...
TLDR: In tests you need to clean out the database before each example. Use :transaction where possible. Use :deletion for Selenium features or when you have a lot of MyISAM...
Understanding database cleaning You want to clean out your test database after each test, so the next test can start from a blank database. To do so you have...
...a script that helps you create an unchanging version of Firefox for your Selenium tests. In particular, this new copy of Firefox will have the following properties:
Once the setup process has completed, you have two Firefoxes: One for Selenium tests, one for regular browsing. You can now update your regular Firefox without fear of Selenium...
Unfortunately, Capybara does not offer a switch to disable cookies in your test browser. However, you can work around that by using a tiny Rack middleware -- it works for both...
...Selenium and non-Selenium tests. Wouldn't it be nice to say something like this? Given cookies are disabled When I try to sign in Then I should see "Can...