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

Replace PhantomJS with Chromium #691

Merged
merged 5 commits into from
May 5, 2024

Conversation

mmx900
Copy link
Contributor

@mmx900 mmx900 commented Apr 20, 2024

Context

Digging the test failure of develop branch, I found that the project is still using old PhantomJS(with Poltergeist) which is outedated and doesn't support ES6 features. When I try to debug using page.driver.debug, I can see console errors including Unexpected token 'const'. As I remember there are many weird bugs in PhantomJS. So I just replaced that with Chromium/Cuprite/Ferrum. Now all tests are passed in my local environment with docker compose run -e "RAILS_ENV=test" web rake db:test:prepare spec. But I'm not sure why it was OK before 2c70ab9.

Summary of Changes

  • Remove outdated PhantomJS/Poltergeist dependency.
  • Add Chromium to Dockerfile. (I considered Chrome, but the package was not available on Arm environment like linux in docker on apple silicon)
  • Add Cuprite dependency with no-sandbox option for docker.
  • Rewrite functions mocking location to resolve Refuge is not defined error.

Checklist

  • Tested Mobile Responsiveness
  • Added Unit Tests
  • CI Passes
  • Deploys to Heroku on test Correctly (Maintainers will handle)
  • Added Documentation (Service and Code when required)

@DeeDeeG
Copy link
Contributor

DeeDeeG commented Apr 20, 2024

Thank you for this pull request. I'll take a look at it.

@DeeDeeG
Copy link
Contributor

DeeDeeG commented Apr 20, 2024

I'm not sure why it was OK before 2c70ab9.

Fair question. I would guess something changed in one of our dependencies, perhaps the Google Maps library.

I ran a passing CI run of 2c70ab9 back on the 10th of May 2023. (Passing run: https://app.travis-ci.com/github/RefugeRestrooms/refugerestrooms/builds/262958108). And then did a CI run with no code changes, to see if things were still in good shape, on the 4th of October 2023, which failed. (Failing run with no code changes 5 months later: https://app.travis-ci.com/github/RefugeRestrooms/refugerestrooms/builds/266379502). (This revealed another, separate issue with the old Travis-CI Xenial CI environment, which was fixed by updating Travis-CI to use a newer base OS. Fixed by: #683. But we still have the one failing spec, in our out of CI, even after fixing the Ubuntu Xenial CI issue.)

So, I suspect one or more of our many dependencies changed "out from under us," so to speak. We can't pin to a specific Google Maps library version anymore, only define the pace of updates as "weekly" or "quarterly", if I recall correctly. Since most of our other dependencies are locked in our yarn.lock or Gemfile.lock lockfiles, I suppose it would have to be something we can't lock down, such as the Google Maps library.

For reference, you can see our total CI build history here: https://app.travis-ci.com/github/RefugeRestrooms/refugerestrooms/builds.

@DeeDeeG
Copy link
Contributor

DeeDeeG commented Apr 28, 2024

Sorry for the delay, I've been working on getting CI running again for this repo, plus attending to some IRL things.

There is a Ferrum:TimeoutError when I try to run this PR through CI:

     # --- Caused by: ---
     # Ferrum::TimeoutError:
     #   Timed out waiting for response. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the :timeout option to a higher value might help.
     #   /usr/local/bundle/gems/ferrum-0.14/lib/ferrum/browser/client.rb:46:in `command'

A fuller snippet of the log output:

Failures:
  1) restrooms preview can preview a restroom before submitting
     Failure/Error: visit "/"
     Ferrum::PendingConnectionsError:
       Request to http://127.0.0.1:41901/ reached server, but there are still pending connections: http://127.0.0.1:41901/
     # /usr/local/bundle/gems/ferrum-0.14/lib/ferrum/page.rb:124:in `rescue in go_to'
     # /usr/local/bundle/gems/ferrum-0.14/lib/ferrum/page.rb:113:in `go_to'
Rails 7.1 has deprecated the singular fixture_path in favour of an array
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
     # /usr/local/bundle/gems/cuprite-0.15/lib/capybara/cuprite/browser.rb:62:in `visit'
     # /usr/local/bundle/gems/cuprite-0.15/lib/capybara/cuprite/driver.rb:52:in `visit'
     # /usr/local/bundle/gems/capybara-3.39.2/lib/capybara/session.rb:280:in `visit'
     # /usr/local/bundle/gems/capybara-3.39.2/lib/capybara/dsl.rb:52:in `call'
     # /usr/local/bundle/gems/capybara-3.39.2/lib/capybara/dsl.rb:52:in `visit'
     # ./spec/features/restrooms_spec.rb:94:in `block (3 levels) in <top (required)>'
     # /usr/local/bundle/gems/webmock-3.12.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Ferrum::TimeoutError:
     #   Timed out waiting for response. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the :timeout option to a higher value might help.
     #   /usr/local/bundle/gems/ferrum-0.14/lib/ferrum/browser/client.rb:46:in `command'
1 deprecation warning total
Finished in 27.83 seconds (files took 2.09 seconds to load)
64 examples, 1 failure
Failed examples:
rspec ./spec/features/restrooms_spec.rb:93 # restrooms preview can preview a restroom before submitting
Randomized with seed 58963

Those are the logs I gathered from getting this PR running in GitHub Actions: https://github.com/DeeDeeG/refugerestrooms/actions/runs/8868660490/job/24348476133

Similar error in Travis CI: https://app.travis-ci.com/github/DeeDeeG/refugerestrooms/builds/270094473#L2761

@mmx900
Copy link
Contributor Author

mmx900 commented Apr 30, 2024

Thanks for the detailed explanation. I've incresed the :timeout for Cuprite from the default of 5 seconds to 30 seconds to prevent the error. Now it seems to be fixed on both Github Action and Travis CI. However, there are still some Rubocop errors since aa22bf7. I think I can fix them, but I'm not sure if this PR is right place to do it.

Copy link
Contributor

@DeeDeeG DeeDeeG left a comment

Choose a reason for hiding this comment

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

Thank you for this!

I can see the specs are indeed passing now. Willing to ignore the Rubocop things as they are generally left over from before this PR.

Looks good to me!

Comment on lines -14 to +18
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

Refuge.Library.Geocoder = (function() {
function Geocoder() {
this.getAddress = __bind(this.getAddress, this);
this.geocodeSearchString = __bind(this.geocodeSearchString, this);
}

Geocoder.prototype.getCurrentLocation = function() {
return jQuery.when(
{
latitude: #{location[:latitude]},
longitude: #{location[:longitude]}
});
};

return Geocoder;

})();
navigator.geolocation.getCurrentPosition = function(success, failure) {
success({ coords: {
latitude: #{location[:latitude]},
longitude: #{location[:longitude]}
}, timestamp: Date.now() });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this is mostly just moving to modern JS promises instead of JQuery stuff. Which is good, I suppose, perhaps not using standard promises was why this spec broke? Hmm.

Anyhow, it's working, which is the most important thing!

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm actually a little bit unclear why we were defining this as Refuge.Library.Geocoder.getCurrentLocation() before, and it's being defined as navigator.geolocation.getCurrentPosition() now.

But, unfortunately, I'm just not very versed in the underlying code of this web app, I've been focused on bugfixes and dependency updates. So, if this works, I'm inclined to believe it is the right thing to do.

Would like to understand better why it worked as Refuge.Library.Geocoder.getCurrentLocation() before, stopped working, and works as navigator.geolocation.getCurrentPosition() now. But I'm not sure if it's worth it to hold up the PR until this can be explained better, and my not understanding it may be down to my own unfamiliarity with the code to begin with.

I can mainly say the new code looks cleaner and more readable, that's about it. To be quite transparent.

Thank you anyway! Regardless of if you wish to try to explain this or not, the fix is appreciated either way!

Copy link
Contributor Author

@mmx900 mmx900 May 6, 2024

Choose a reason for hiding this comment

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

At the time that the code was written(69d417), Geocoder was Refuge.Library.Geocoder(69d417). Then It was rewritten to Geocoder, applying ES6/Webpacker. However, this change did not cause any test failure for two reasons :

  1. Poltergeist was set to ignore javascript errors with js_errors: false. So you could replace the code with anything like asdf.qwer.blah() without causing any test failure.
  2. The tests using mock_location in restroom_spec.rb seems never to have been written completely since cd52015 except for one test that didn't require an exact location. Some expectation codes are still commented out, and some specs are just wrapped with #rubocop:disable RSpec/NoExpectationExample. So I think that the return values of getCurrentLocation() would never be a problem. I'll try to complete them later.

Thanks for review this PR.

@DeeDeeG DeeDeeG merged commit 747d92f into RefugeRestrooms:develop May 5, 2024
1 check failed
@DeeDeeG DeeDeeG mentioned this pull request May 5, 2024
5 tasks
mmx900 added a commit to mmx900/refugerestrooms that referenced this pull request May 6, 2024
* Replacing abandoned PhantomJS with Chromium

* Fixing "Refuge is not defined" error

* Removing unnecessary workaround

* Increasing timeout to prevent PendingConnectionError

* Remove comments that are no longer needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants