Skip to content

Instantly share code, notes, and snippets.

@jfroom
Last active March 16, 2017 17:47
Show Gist options
  • Save jfroom/7161ba9d2d72e772475e0a7ae6046f65 to your computer and use it in GitHub Desktop.
Save jfroom/7161ba9d2d72e772475e0a7ae6046f65 to your computer and use it in GitHub Desktop.
capybara selenium ':nth-of-type' selector
# Notice use of ':nth-of-type(idx)' in combination with has_css?(css, text: '...').
# Similar to a jquery equivalent of $('td:eq(3)').
# Notice it's a 1 based index for :nth-of-type (not 0).
# In the past I think we were comparing element.text == '...' when iterating/targeting a css index like a specific TD.
# The advantage here is a leveraging capybara's built in 'wait'.
# https://github.com/teamcapybara/capybara/issues/1109
# Gems
# - selenium-webdriver (3.0.7)
# - capybara (2.12.0)
# - nokogiri (>= 1.3.3)
require "acceptance/pages/base_page"
class Admin::UsersPage < Pages::Base
ATTRIBUTES = ['username', 'first_name', 'last_name', 'admin', 'login_locked', 'login_attempts'].freeze
def has_user_table_header?
# Verify number of TH vs. attributes
css = 'table.users thead th'
unless @session.has_css?(css, count: ATTRIBUTES.size + 1) # include last empty 'action' header col
raise "Expected #{ATTRIBUTES.size} columns, but found #{@session.all(css).size}"
end
# Verify attributes vs. TH values
ATTRIBUTES.each_with_index do |attr, idx|
unless @session.has_css?("#{css}:nth-of-type(#{idx + 1})", text: attr)
text = @session.all(css)[idx].text
raise "Expected col[#{idx}] = '#{attr}', but got '#{text}'."
end
end
true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment