如何在Site Prism中动态加载页面

时间:2018-06-23 11:49:42

标签: ruby-on-rails cucumber site-prism

我有一个文章页面,一个新闻页面和一个评论页面,它们具有一些共享的元素。目前,我有如下不同的步骤来加载和测试共享元素。

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)

它可以工作,但是会有更多页面,我想分享一些步骤。我尝试了发送加载方法和page参数以及初始化Page对象的各种组合。

shared_pa​​ge_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"

我收到错误cannot load such file -- article_page (LoadError)

我也尝试过

shared_pa​​ge_steps.rb

Given('I visit the {string} page {string}') do |page_type, title|
  page = "#{page_type}"
  send(:load, page, slug: title.parameterize)
end

我收到错误cannot load such file -- article (LoadError)

shared_pa​​ge_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

我收到错误uninitialized constant Article (NameError)

似乎使用send(:load)尝试加载文件而不是页面对象。当我使用classify.constantize将字符串转换为常量也无法正常工作时,我想知道是否需要显式调用UI :: Pages :: Article或UI :: Pages :: Comment,但我不这样做。不知道如何动态地做到这一点。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

SitePrism #load方法和参数化都经过了广泛的测试。

如果您认为有问题,是否可以在此减少用例并在此处提出SSCCE:https://github.com/natritmeyer/site_prism/issues-尝试尽可能减少其使用,最好采用某种形式的可复制git repo来制作重现问题很容易。

因为您正在对方法进行元编程,所以代码签名中可能会出现错误,但这只是快速的5s观察。我认为您可能想做send("#{page}.load", *args)-但是我的潜能还不够