ruby-on-rails - 如何在 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_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"
我得到了错误cannot load such file -- article_page (LoadError)
我也试过
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
我得到了错误cannot load such file -- article (LoadError)
和
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
我得到了错误uninitialized constant Article (NameError)
看起来好像使用 send(:load) 正在尝试加载文件而不是页面对象。当我将字符串转换为常量时,classify.constantize
它也不起作用,我想知道是否需要显式调用 UI::Pages::Article 或 UI::Pages::Comment 但我不知道该怎么做那是动态的。
有什么建议么?
解决方案
SitePrism#load
方法和参数化都经过了广泛的测试。
如果您认为存在问题,您能否减少您的用例并在此处提出 SSCCE:https ://github.com/natritmeyer/site_prism/issues - 尽量保持精简,最好以某种形式可克隆 git repo 以便于重现问题。
因为您正在对方法进行元编程,所以您的代码签名中可能存在错误,但这只是 5 秒的快速观察。我想你可能想要做send("#{page}.load", *args)
- 但我还不够深入
推荐阅读
- nginx - nginx:[emerg] 这里不允许使用“proxy_cache_path”指令,并且这里不允许使用“server”指令,并且
- android - 颤动运行错误和 gradle 失败,退出代码为 1
- php - 无法修改标头信息 - 从 /home/nyehfdcj/public_html/application/controllers/admin/Login.php:16 开始的输出已发送标头
- c++ - 引用另一个 const int 时删除 const int* const ptrs
- sql - 从单个列中的许多日期字符串中获取当前日期的最小值
- python - 如何通过 Python 中的装饰器检查参数和返回类型
- c# - 省略给定数字的第一个数字
- javascript - 使用 JS 发布时 php setcookie 不起作用
- visual-studio-code - FSharpLint,如何在 SuppressMessageAttribute 中使用规则“InterfaceNamesMustBeginWithI”?
- java - 如何修复 apache ignite 错误“ignoredFailureTypes=[SYSTEM_WORKER_BLOCKED, SYSTEM_CRITICAL_OPERATION_TIMEOUT]”?