Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduction of system tests in Capybara #357

Merged
merged 6 commits into from
Oct 5, 2023

Conversation

torrocus
Copy link
Member

@torrocus torrocus commented Oct 5, 2023

Pull Request Summary

I have set up system tests. We can use Capybara to describe our test cases and Cuprite as the Headless Chrome driver.
I added two sample tests. The first one that tests whether our seeds are loading correctly. And the second one, which checks whether the JavaScript libraries are loaded correctly.

Feedback

Our tests are a little slower now, but this is acceptable.

The approach of using Cuprite which using Ferrum under the hood is made possible by the popularization of CDP protocol.

UI Changes

N/A

> Defaults profile_examples to 10 examples [1]
> when @profile_examples is true.

This value determines the number of the slowest tests displayed.

[1]: https://rspec.info/documentation/3.1/rspec-core/RSpec/Core/Configuration.html#profile_examples-instance_method
We want to create high-level tests in Selenium [1] [2]
using Capybara. [3]

To make using Selenium easier,
it is worth installing webdrivers. [4]

> Run Selenium tests more easily with automatic installation
> and updates for all supported webdrivers.

However, recently there have been major changes,
which we can read about in the webdrivers documentation.

> With Google's new Chrome for Testing project, [5]
> and Selenium's new Selenium Manager feature,
> what is required of this gem has changed.
>
> If you can update to the latest version of Selenium (4.11+),
> please do so and stop requiring this gem.
> Provide feedback or raise issues to Selenium Project

My further research led me to an article published
by Evil Martians. [6]

This in turn led me to Cuprite [7] [8]
and related gems like Vessel [9] and Ferrum. [10]

> Cuprite is a pure Ruby driver
> (read as no Selenium/WebDriver/ChromeDriver dependency)
> for Capybara.

> Vessel - high-level web crawling framework
> It is Ruby high-level web crawling framework
> based on Ferrum for extracting the data you need from websites.

> Ferrum - high-level API to control Chrome in Ruby
> It is Ruby clean and high-level API to Chrome.
> Runs headless by default,
> but you can configure it to run in a headful mode.
> Ferrum connects to the browser by CDP protocol
> and there's no Selenium/WebDriver/ChromeDriver dependency.

[1]: https://www.selenium.dev/
[2]: https://github.com/SeleniumHQ/selenium
[3]: https://github.com/teamcapybara/capybara
[4]: https://github.com/titusfortner/webdrivers
[5]: https://developer.chrome.com/blog/chrome-for-testing/
[6]: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing
[7]: https://github.com/rubycdp/cuprite
[8]: https://cuprite.rubycdp.com/
[9]: https://github.com/rubycdp/vessel
[10]: https://github.com/rubycdp/ferrum
@torrocus torrocus added the ruby Pull requests that update Ruby code label Oct 5, 2023
@torrocus torrocus requested a review from ania-hm October 5, 2023 09:51
Based on the article I found earlier, [1]
I prepared a configuration for Capybara and Cuprite.

The `system_helper.rb` file contain general RSpec configuration
for system tests.

The `capybara_setup.rb` contains configuration for Capybara framework.
The `cuprite_setup.rb` is responsible for configuring Cuprite.

The `precompile_assets.rb` file is responsible for precompiling assets
before running system tests.

> Why precompile assets manually if Rails can do that automatically?
> The problem is that Rails precompiles assets lazily
> (i.e., the first time you request an asset),
> and this could make the first test example run much slower
> and even encounter random timeout exceptions.

Generally, I didn't want to go exactly in the direction presented
by Evil Martians. My goal was to simplify their setup.

[1]: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing
Recently, an error occurred that our seeds were not compatible
with the database structure.
We want to avoid this in the future,
so I created a simple integration test.
We want our test to monitor
what versions of the JavaScript libraries we are using.
After adding the tests in Capybara, our Rubocop informed us:

> The following RuboCop extension libraries are installed
> but not loaded in config:
>   * rubocop-capybara

So I added this extension to our Rubocop configuration.
@torrocus torrocus force-pushed the al-minor-improvements-in-tests branch from cd2c516 to f04ff58 Compare October 5, 2023 09:59
@torrocus torrocus changed the title Minor improvements in tests Introduce system tests in Capybara Oct 5, 2023
@torrocus torrocus changed the title Introduce system tests in Capybara Introduction of system tests in Capybara Oct 5, 2023
@torrocus torrocus merged commit c4fe4a0 into main Oct 5, 2023
8 checks passed
@torrocus torrocus deleted the al-minor-improvements-in-tests branch October 5, 2023 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants