首页 > 解决方案 > 如何在 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

文章.rb

module UI
  module Pages
    class Article < UI::Page
      set_url '/en/articles/{/slug}'

      element :summary, '.summary'
    end
  end
end

世界/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)

它有效,但还会有更多页面,我想分享一些步骤。我尝试了发送带有页面参数的加载方法和初始化页面对象的各种组合。

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 但我不知道该怎么做那是动态的。

有什么建议么?

标签: ruby-on-railscucumbersite-prism

解决方案


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

如果您认为存在问题,您能否减少您的用例并在此处提出 SSCCE:https ://github.com/natritmeyer/site_prism/issues - 尽量保持精简,最好以某种形式可克隆 git repo 以便于重现问题。

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


推荐阅读