Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, August 15, 2016

Is it possible to test the order of elements via RSpec/Capybara?

Is it possible to test the order of elements via RSpec/Capybara?


I'm using RSpec/Capybara as my test suite. I have some javascript that dynamically appends

  • to the end of a
      . I want to write a request spec to ensure that this is happening.

      I tried using the has_css Capybara method and advanced CSS selectors to test for the ordering of the

    • elements, but Capybara doesn't support the + CSS selector.

      Example:

      page.should have_css('li:contains("ITEM #1")')  pseuo_add_new_li  page.should have_css('li:contains("ITEM #1")+li:contains("ITEM #2")')  

      Does anyone know of another way to test for ordering?

      Answer by Marnen Laibow-Koser for Is it possible to test the order of elements via RSpec/Capybara?


      Capybara should support the + selector (I believe it uses Nokogiri, which certainly supports this). Are you perhaps using an old version? Or are you using a Capybara driver that doesn't support JavaScript, so that the extra element isn't getting rendered at all?

      Also, don't use RSpec for testing your HTML. Cucumber is far better at this. For interacting with JavaScript, you'll want to use Selenium or Capybara-Webkit. Or if you have a lot of JavaScript, consider testing it with Jasmine.

      Answer by John for Is it possible to test the order of elements via RSpec/Capybara?


      I resolved this issue by testing for a regex match against the body content of the page. A bit kludgy, but it works.

      page.body.should =~ /ITEM1.*ITEM2.*ITEM3/  

      Answer by dgilperez for Is it possible to test the order of elements via RSpec/Capybara?


      I found a more canonical way of testing this behaviour with CSS. You could user :first-child, :last-child and :nth-child(n) selectors in whichever assert you like.

      In your example I'd try these assertions:

      page.should have_tag("ul:last-child", :text => "ITEM #1")  pseuo_add_new_li  page.should have_tag("ul:nth-last-child(2)", :text => "ITEM #1")  page.should have_tag("ul:last-child", :text => "ITEM #2")  

      I hope this helps someone. Read more about this.

      Answer by charlesdeb for Is it possible to test the order of elements via RSpec/Capybara?


      This page has got a very clever way of testing the order of string elements on a page - be they in a list or not:

      https://makandracards.com/makandra/789-match-strings-in-a-given-order-with-cucumber-and-capybara

      It is in the form of a cucumber step, but you should be able to extract what you need for an Rspec step. It is similar to Johns 'kludgy' solution - but a bit fancier from what I can make out. All of their other cunning capybara testing steps can be found here:

      https://github.com/makandra/spreewald

      Answer by Finn MacCool for Is it possible to test the order of elements via RSpec/Capybara?


      this article lists several ways to test sort order in RSpec, the best of which seems to be this matcher:

      RSpec::Matchers.define :appear_before do |later_content|    match do |earlier_content|      page.body.index(earlier_content) < page.body.index(later_content)    end  end  

      Answer by TomTaila for Is it possible to test the order of elements via RSpec/Capybara?


      I have had the same issue recently and found this neat & ideal solution: http://launchware.com/articles/acceptance-testing-asserting-sort-order

      It's even packaged as a tiny gem.

      Answer by steel for Is it possible to test the order of elements via RSpec/Capybara?


      Using capybara-ui you could use the #widgets method to get all of the elements in top-down order.

      # First define the widget,  # or reusable dom element reference.  # In this case in a role  class UserRole < Capybara::UI::Role    widget :list_item, '.list-item'  end    # Then test the expected order using #widgets  role = UserRole.new  expected_order = ['buy milk', 'get gas', 'call dad']  actual_order = role.widgets(:list_item).map(&:text)    expect(actual_order).to eq(expected_order)  

      Answer by Jan Klimo for Is it possible to test the order of elements via RSpec/Capybara?


      Use the orderly gem, written by the author of the article mentioned previously.

      It's as simple as:

      expect(this).to appear_before(that)  

      Answer by j4y for Is it possible to test the order of elements via RSpec/Capybara?


      You can use the all finder method to select multiple elements and then use collect to pull out the text into an array:

      assert_equal page.all('#navigation ul li').collect(&:text), ['Item 1', 'Item 2', 'Item 3']  

      If your list isn't visible on the page such as a popup navigation menu, you need to pass visible: false into the all method.


      Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
  • 0 comments:

    Post a Comment

    Popular Posts

    Powered by Blogger.