0

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?

margo
  • 2,927
  • 1
  • 14
  • 31

1 Answers1

0

Both the SitePrism #load method and parameterizations are extensively tested.

If you think there is an issue, could you diet down your use case and raise an SSCCE here: https://github.com/natritmeyer/site_prism/issues - Try to keep it as trimmed down as possible, ideally in some form of clonable git repo to make it easy to reproduce the issue.

Because you're metaprogramming the methods, it could be likely there is an error in your code-signature, but this is just a quick 5s observation. I think you probably want to do send("#{page}.load", *args) - But I've not dived in enough

Luke Hill
  • 460
  • 2
  • 10