Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

page.must_have_content not working #10

Closed
alec-c4 opened this issue Jun 23, 2014 · 22 comments
Closed

page.must_have_content not working #10

alec-c4 opened this issue Jun 23, 2014 · 22 comments
Assignees

Comments

@alec-c4
Copy link

alec-c4 commented Jun 23, 2014

Hi,
on attempt to run following test

Then(/^I should see "(.*?)"$/) do |string|
  page.must_have_content(string)
end

i see error

    Then I should see "Test" # features/step_definitions/pages_steps.rb:9
      undefined method `assert_page_has_content' for nil:NilClass (NoMethodError)
      (eval):4:in `must_have_content'
      ./features/step_definitions/pages_steps.rb:10:in `/^I should see "(.*?)"$/'
      features/can_access_home.feature:7:in `Then I should see "Test"'

how to fix it?

@ordinaryzelig
Copy link
Owner

Can you give me more information on what you are using? Testing gems, minitest version, capybara version, etc.?

@alec-c4
Copy link
Author

alec-c4 commented Jun 24, 2014

of course :)

minitest 5.3.5
cucumber 1.3.15
capybara 2.3.0
capybara_minitest_spec 1.0.2
rails 4.1.1
cucumber-rails 1.4.1

Feature: Home page
  In order to test availability of the home page
  a user wants to successfully land on the home page

  Scenario: Access home page
    Given I am on the home page
    Then I should see "Lorgnette"
Given(/^I am on the home page$/) do
  visit "/"
end

Given(/^I am on the page with url "([^"]*)"$/) do |url|
  visit "#{url}"
end

Then(/^I should see "(.*?)"$/) do |string|
  page.must_have_content(string)
end

@ordinaryzelig
Copy link
Owner

I'm afraid I've never used cucumber. Just before page.must_have_content(string), what does puts inspect output?

Do capybara and cucumber normally work in this way without my gem?

@alec-c4
Copy link
Author

alec-c4 commented Jun 25, 2014

i've got

      #<Cucumber::Rails::World+EmailSpec::Matchers+EmailSpec::Helpers+EmailSpec::Deliveries+EmailSpec::BackgroundProcesses::Compatibility+EmailSpec::MailerDeliveries+Cucumber::Rails::Capybara::SelectDatesAndTimes+Capybara::RSpecMatchers+Capybara::DSL+MultiTest::MinitestWorld+Cucumber::RbSupport::RbWorld+Rack::Test::Methods+ActionDispatch::Routing::UrlFor+ActionDispatch::Routing::PolymorphicRoutes+ActionController::ModelNaming+ActionController::TemplateAssertions+ActionDispatch::Integration::Runner+ActionDispatch::Assertions+ActionDispatch::Assertions::TagAssertions+ActionDispatch::Assertions::SelectorAssertions+ActionDispatch::Assertions::RoutingAssertions+ActionDispatch::Assertions::ResponseAssertions+ActionDispatch::Assertions::DomAssertions+ActiveRecord::TestFixtures+ActiveSupport::Testing::TimeHelpers+ActiveSupport::Testing::Deprecation+ActiveSupport::Testing::Assertions+ActiveSupport::Callbacks+ActiveSupport::Testing::SetupAndTeardown+ActiveSupport::Testing::TaggedLogging+Minitest::Guard+Minitest::Test::LifecycleHooks+Minitest::Assertions+Minitest::Expectations+PP::ObjectMixin+ActiveSupport::Dependencies::Loadable+V8::Conversion::Object+JSON::Ext::Generator::GeneratorMethods::Object+Kernel:0x3fecd1a96200>

without your gem i see

      undefined method `must_have_content' for #<Capybara::Session> (NoMethodError)
      ./features/step_definitions/pages_steps.rb:11:in `/^I should see "(.*?)"$/'
      features/can_access_home.feature:7:in `Then I should see "Lorgnette"'

@ordinaryzelig
Copy link
Owner

Sorry for the late reply.

Try: page.has_content?(string) to try basic capybara behavior.

@alec-c4
Copy link
Author

alec-c4 commented Jul 5, 2014

I've fixed problem with following

Then(/^I should see "(.*?)"$/) do |string|
  assert page.has_content?(string)
end

Thanks a lot!

@alec-c4 alec-c4 closed this as completed Jul 5, 2014
@toby-1-kenobi
Copy link

That's not a fix. It's a workaround. This gem is supposed to provide minitest::spec style assertions for Capybara, but it doesn't work in Cucumber. Can we reopen this bug? It applies to me too, and I'm not content with the workaround.

@ordinaryzelig ordinaryzelig reopened this Sep 18, 2015
@toby-1-kenobi
Copy link

I can confirm that refute_selector(".my_css_class") works in a cucumber step, but page.wont_have_selector(".my_css_class") gives an error complaining that refute_selector is undefined for nil:NilClass

@ordinaryzelig
Copy link
Owner

Any chance you could create a repo for me with your environment and a failing test? Would help me a ton to debug.

@toby-1-kenobi
Copy link

Here's the repo of my current project
https://github.com/toby-1-kenobi/sag_reporter
to get the error:
cucumber features/user_may_have_multiple_geo_states.feature

is this ok? or do you want a smaller repo, just enough to reproduce the problem?

@ordinaryzelig
Copy link
Owner

is this ok? or do you want a smaller repo, just enough to reproduce the problem?

Please, if it's not too much trouble. Would help to isolate the problem.

@toby-1-kenobi
Copy link

OK, when I get some time to do that.

@toby-1-kenobi
Copy link

Here's the repo for a bare rails project with just enough code to produce the bug:
https://github.com/toby-1-kenobi/bug-demo

run cucumber to produce the bug
run rake test TEST=test/features/no_bug_here.rb to have the same test run outside cucumber and see that no bug is there.

@ordinaryzelig
Copy link
Owner

@toby-1-kenobi I was able to get it to work, but it's not a good solution. Not sure how long it will last as a "feature". I'm open to suggestions. See toby-1-kenobi/bug-demo#1.

@ordinaryzelig ordinaryzelig self-assigned this Oct 23, 2015
@ordinaryzelig
Copy link
Owner

There are also failing tests. Will look into those when I can. But do let me know if you think the solution is good/bad.

@toby-1-kenobi
Copy link

Why do you think the solution is not good?

@toby-1-kenobi
Copy link

It looks like the the key is having the code

Before do
  Thread.current[:current_spec] = self
end

which I put into features/support/env.rb and it works. (See commit "Workaround" in the bug-demo repo). Thanks @ordinaryzelig.

putting this code into rnv.rb is a workaround with which I am satisfied, and I prefer it to cloning capybara_minitest_spec and putting the code in there.

@ordinaryzelig
Copy link
Owner

I don't particularly like it because it's not intuitive. It's really just a bandaid. I just figured out what was missing from Minitest::Spec's initializer and copied it in the Before statement. A better solution would be to somehow make Cucumber::Rails::World a subclass of Minitest::Spec. Then it would properly get everything it's supposed to get. It would work with future versions.

Since you find the solution good enough, I'll add it to the README, but I don't feel like it's good enough to support in the gem itself, even unofficially.

@f3ndot
Copy link

f3ndot commented Apr 12, 2016

@ordinaryzelig we apply a workaround by doing the following in env.rb:

require 'minitest/spec'

# ...

# Make minitest assertions available in Cucumber
class MinitestWorld
  extend Minitest::Assertions
  attr_accessor :assertions

  def initialize
    self.assertions = 0
  end
end

World do
  MinitestWorld.new
end

Is this more palatable?

@ordinaryzelig
Copy link
Owner

@f3ndot I gave it a shot (still not familiar with cucumber at all). I copied your code into the bug demo above in env.rb and got:

You can only pass a proc to #World once, but it's happening
in 2 places:

cucumber-rails-1.4.2/lib/cucumber/rails/world.rb:24:in `World'
features/support/env.rb:73:in `World'

Use Ruby modules instead to extend your worlds. See the Cucumber::RbSupport::RbDsl#World RDoc
or http://wiki.github.com/cucumber/cucumber/a-whole-new-world.

 (Cucumber::RbSupport::MultipleWorld)
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/rb_support/rb_language.rb:87:in `build_rb_world_factory'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/rb_support/rb_dsl.rb:15:in `build_rb_world_factory'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/rb_support/rb_dsl.rb:50:in `World'
/Users/ningja/dev/projects/bug-demo/features/support/env.rb:73:in `<top (required)>'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/rb_support/rb_language.rb:95:in `load'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/rb_support/rb_language.rb:95:in `load_code_file'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/runtime/support_code.rb:180:in `load_file'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/runtime/support_code.rb:82:in `each'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/runtime.rb:184:in `load_step_definitions'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/runtime.rb:42:in `run!'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/lib/cucumber/cli/main.rb:47:in `execute!'
/Users/ningja/.gem/ruby/2.3.0/gems/cucumber-1.3.20/bin/cucumber:13:in `<top (required)>'
/Users/ningja/.gem/ruby/2.3.0/bin/cucumber:23:in `load'
/Users/ningja/.gem/ruby/2.3.0/bin/cucumber:23:in `<main>'

I'm assuming you're using plain cucumber, and not cucumber-rails?

Either way, I think my main goal is to have the world class somehow run everything that happens in Minitest::Spec's constructor. Currently, the only thing it needs is the Thread snippet, which I've already added to capybara_minitest_spec/cucumber_rails.rb but don't particularly like as a solution (also why I haven't released it). And the workaround is so easy that I figure it's not worth adding to this gem.

@f3ndot
Copy link

f3ndot commented Apr 16, 2016

Yes, we're using plain cucumber. And fair enough. Hopefully it help Google-searchers in the future 😄

@ordinaryzelig
Copy link
Owner

Also, there's a note in the readme pointing here. 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants