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

Use a driver supporting JavaScript in system tests #4454

Merged
merged 52 commits into from
Apr 7, 2021
Merged

Conversation

javierm
Copy link
Member

@javierm javierm commented Mar 30, 2021

References

Objectives

  • Test the way the application behaves in browsers supporting JavaScript (about 98% of existing browsers)
  • Use selenium/chromedrive as the default driver, so we don't have to remember to add a :js tag on every test we write
  • Make usability issues more obvious in system tests

Notes

In the past, using a headless browser in tests made the test suite much slower because we had to use database cleaner to truncate the database after each test. With system tests, however, we use database transactions to make sure data is removed between tests, and so the speed difference isn't that big.

@javierm javierm added the Specs label Mar 30, 2021
@javierm javierm added this to Reviewing in Consul Democracy via automation Mar 30, 2021
@javierm javierm self-assigned this Mar 30, 2021
@javierm javierm moved this from Reviewing to Doing in Consul Democracy Mar 30, 2021
spec/rails_helper.rb Outdated Show resolved Hide resolved
@@ -257,7 +257,7 @@
expect(page).to have_content comment.body

expect(page).to have_link "Cleaner city",
href: admin_budget_budget_investment_url(investment.budget, investment, anchor: "comments")
href: admin_budget_budget_investment_url(investment.budget, investment, anchor: "comments", host: app_host)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/LineLength: Line is too long. [115/110] (https://rubystyle.guide#max-line-length)

@javierm javierm force-pushed the browser_tests branch 9 times, most recently from e275494 to 10495aa Compare April 1, 2021 16:55
It was strange to specify the unfeasibility reason without marking the
investment as unfeasible.
Before clicking the "Edit phase" link, there's already a "Name" field
present in the page (the name of the budget).

With the rack driver, there's no problem since the `fill_in` action
waits until the page is loaded.

However, the test will be a flaky spec if we use a driver supporting
JavaScript, since clicking the "Edit phase" link will cause an AJAX
request and the `fill_in` action might be executed before the AJAX
request is finished.
This way we reproduce the user experience in the tests, and we can make
sure modal dialogs open when we expect it.
We were filling in textareas, so we were only testing how the
application behaves for about 1%-2% of our users.
The markdown editor only works with JavaScript, so in order to test it
we need a JavaScript test.
IMHO opening new windows is a usability issue which has been known for
twenty years since it takes control away from the user and breaks the
"back button", but for now we're keeping the same behavior as we already
had, while slightly increasing the complexity of the tests (which is a
good indicator of a usability issue).
So now we feel in the tests the same usability problems users feel when
they want to click a link and the flash message is in their way.
Having all the text show when hovering over a cell is an
accessibility/usability issue that we now experience in the tests as
well.
We're implicitly checking the link is listed on the menu by clicking it
in other tests.
This menu requires JavaScript to open/close subnavigation menus, so
we're now testing the way users with a browser supporting JavaScript
(98%-99% of the users) deal with the menu.
This way we click on the "menu" link first before clicking on any
sections, just like real users do.
So what we write in the tests is close to what users experience.
Content like lowercase letters with `text-transform: uppercase` or
spaces after elements with `display: block` or "You're on page:" are not
seen that way by users with a browser supporting CSS.

So we're testing what most users actually experience.
The test is failing on my machine sometimes. I thought it was because
`open_last_email` didn't wait fot the request generated by clicking the
"Registrarse" button to finish. Even if that might be the case, adding
an extra expectation showed the test was really failing because
invisible captcha reported the form was filled in too fast.

I wonder whether invisible captcha is working properly or there are
times where it doesn't start its timer when it should.

Since this might be a bug in the application, I'm not changing the test
just to make it pass.
JavaScript is used by about 98% of web users, so by testing without it
enabled, we're only testing that the application works for a very
reduced number of users.

We proceeded this way in the past because CONSUL started using Rails 4.2
and truncating the database between JavaScript tests with database
cleaner, which made these tests terribly slow.

When we upgraded to Rails 5.1 and introduced system tests, we started
using database transactions in JavaScript tests, making these tests much
faster. So now we can use JavaScript tests everywhere without critically
slowing down our test suite.
This offense was introduced in commit 28caabe.
We've got quite a messy hack to sign in managers: they need to visit a
specific URL (management root path).

That means tests signing in managers start the browser to sign them in,
which might cause issues if we setup the database after that.
GitHub Actions is failing to finish sometimes. Usually that happens due
to concurrency issues when the process running the test accesses the
database after the process running the browser has started.

Since these files were the ones being tested the times we had this
issue, these are the ones we are fixing right now, although there are
probably other places where we might have this issue in the future.
Sometimes a test gets stuck and and we have to wait until it times out
in order to check which files were being tested at the time.

The default timeout is six hours. I'm reducing it to one hour which
should still be plenty of time even on repositories with no knapsack
token.
That way we make sure the request is finished before the test finishes.
We were getting a failure in GitHub Actions because an unrelated test
executed after this one had its locale set to Spanish (just for one
test, though), and one possible explanation could be that a previous
request which changed I18n.locale was still being executed.
@javierm javierm merged commit 5f642f9 into master Apr 7, 2021
Consul Democracy automation moved this from Testing to Release 1.3.0 Apr 7, 2021
@javierm javierm deleted the browser_tests branch April 7, 2021 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants