我有一个文章页面、一个新闻页面和一个评论页面,其中包含一些共享元素。目前,我有不同的步骤来加载和测试共享元素,如下所示。
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 但我不知道该怎么做那是动态的。
有什么建议么?