percy / percy-capybara

Visual testing for Capybara with Percy.
https://docs.percy.io/docs/capybara
MIT License
45 stars 23 forks source link

Using Percy snaps in view tests #54

Closed doutatsu closed 3 years ago

doutatsu commented 6 years ago

I am trying to figure out if its possible to use Percy snaps in view tests. As Capybara works to a certain degree in there, I'd imagine Percy would as well.

I use Capybara::Node::Simple.new(rendered) for page and pass it as well to Percy. But I get current_url method missing. Looking into Percy source code I can't find the instance where this is being called to understand the reason.

djones commented 6 years ago

Hi @doutatsu, the current_url call might be from here: https://github.com/percy/percy-capybara/blob/37109a3d627ee1a94d2d8e546bd734d6bf88f0ce/lib/percy/capybara/loaders/base_loader.rb#L76-L84

I'm curious to learn more about your use case. A bit more context around Capybara::Node::Simple.new and how the percy.snapshot call currently fits in would be really useful.

Cheers.

doutatsu commented 6 years ago

Hi @djones, thanks for a speedy reply. Not sure how I missed that code.

We use Percy snaps in Integration tests, where Capybara has full capacity. But we had a couple view tests, in one of which we used Capybara::Node::Simple.new to be able to use .find method on Capybara.

But there was a need to have a Percy snap in the view test itself, so I thought I'd reuse this object, but it seems its not enough for Percy 🤔

So as mentioned before, I wonder if it is possible to make snaps in view tests or I have to use an Integration test (Which inherits from ActionDispatch::IntegrationTest)

djones commented 6 years ago

My sense is this should be possible so long you're able to get the DOM and resources required to load that DOM. I don't have any experience with testing views in Capybara though.

I'd like to get a minimal view test example going so I can take a look at how we might modify percy-capybara to make it work out of the box for this use case.

Are you doing something similar to this? https://robots.thoughtbot.com/use-capybara-on-any-html-fragment-or-page

doutatsu commented 6 years ago

Yeah, I wouldn't be surprised older code was written based on that article. Here an example test trimmed to the usecase. We just used that Capybara to get access to .find

require "test_helper"
require "capybara"

class ViewTest < ActionView::TestCase
  test "sidebar has number 1" do
    render "sidebars/main_sidebar"

    assert page.find("#some_id").has_content? "1"
  end

  private

  def page
    Capybara::Node::Simple.new(rendered)
  end
end

Now an example of that I was trying to do:

require "test_helper"
require "capybara"

class AnotherViewTest < ActionView::TestCase
  test "some page to show some string" do
    render "pages/some_page"

    Percy::Capybara.snapshot(page, "Specific snap")

    assert_select "h3", "Some string"
  end
  private

  def page
    Capybara::Node::Simple.new(rendered)
  end
end
doutatsu commented 5 years ago

Hey @djones was wondering if you had any more thoughts on this? That work has been put on hold due to this, but I'd like to either get this working or confirm that its impossible