I have an article page, a news page and a comment page which have some shared elements. At the moment I have different steps for loading and testing the shared elements as follows.
article_page.feature
Given I visit the Article page "Article title"
Then I should see the article title "Article title"
And I should see the summary "article summary"
article_steps.rb
Given('I visit the Article page {string}') do |title|
article_page.load(slug: title.parameterize)
end
Then('I should see the article title {string}') do |title|
expect(article_page).to have_content(title)
end
Then('I should see the summary {string}') do |summary|
expect(article_page.summary.text).to eq(summary)
end
comment_page.feature
Given I visit the Comment page "Comment title"
Then I should see the comment title "Comment title"
And I should see the summary "comment summary"
comment_steps.rb
Given('I visit the Comment page {string}') do |title|
comment_page.load(slug: title.parameterize)
end
Then('I should see the comment title {string}') do |title|
expect(comment_page).to have_content(title)
end
Then('I should see the summary {string}') do |summary|
expect(comment_page.summary.text).to eq(summary)
end
article.rb
module UI
module Pages
class Article < UI::Page
set_url '/en/articles/{/slug}'
element :summary, '.summary'
end
end
end
world/pages.rb
module World
module Pages
def current_page
UI::Page.new
end
pages = %w[article comment]
pages.each do |page|
define_method("#{page}_page") do
"UI::Pages::#{page.camelize}".constantize.new
end
end
end
end
World(World::Pages)
It works, but there will be several more pages and I'd like to share some of the steps. I've tried various combinations of sending the load method with the page parameters and initializing the Page object.
shared_page_steps.rb
Given('I visit the {string} page {string}') do |page_type, title|
page = "#{page_type}_page"
send(:load, page, slug: title.parameterize)
end
article_page.feature
Given I visit the "Article" page "Article title"
comment_page.feature
Given I visit the "Comment" page "Comment title"
and I get the error cannot load such file -- article_page (LoadError)
I also tried
shared_page_steps.rb
Given('I visit the {string} page {string}') do |page_type, title|
page = "#{page_type}"
send(:load, page, slug: title.parameterize)
end
and I get the error cannot load such file -- article (LoadError)
and
shared_page_steps.rb
Given('I visit the {string} page {string}') do |page_type, title|
page = "#{page_type}".classify.constantize
@page = page.new.load(slug: title.parameterize)
end
and I get the error uninitialized constant Article (NameError)
It looks as if using send(:load) is trying to load the file as opposed to the page object. When I convert the string to a constant with classify.constantize
that also doesn't work and I'm wondering if I need to explicitly call UI::Pages::Article or UI::Pages::Comment but I don't know how to do that dynamically.
Any suggestions?