gpt4 book ai didi

ruby - 公共(public)页面元素 - 它们可以被继承吗?

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:47 24 4
gpt4 key购买 nike

在 cucumber/Watir-webdriver/page-object 环境中,我需要验证通用页眉和页脚链接的存在和功能。虽然这些应该都是相同的并且有效,但我想将它们包含在所有相关页面上。

我正在尝试跟上 DRY 编码,并希望尽量减少我需要识别 Web 应用程序上共享的页眉/页脚元素的次数。

我想我可以通过在 support/文件夹中包含一些“common_links.rb”来做到这一点:

module CommonLinks
include PageObject
# Header links
link(:log_out_header_link, :text => "Log Out")
link(:logo, :id => "logo")

#Logged in nav links
div(:nav, :id => "globalnav_wrapper")
nav.link(:nav_activities, :href => "/activities/")

# Footer Links
link(:contact_us_footer, :text => "CONTACT US")

# Social Media Links
link(:social_twitter, :id => "soc_twitter")
link(:social_facebook, :id => "soc_fb")
link(:social_pinterest, :id => "soc_pin")
link(:social_google_plus, :id => "soc_gplus")
link(:social_youtube, :id => "soc_yt")

end

我正在尝试将以上内容包含在 support/pages/activities_page.rb 中。当我尝试加载调用它的 cucumber 功能时,出现以下错误:

/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/page-object-0.8.6.1/lib/page-object/accessors.rb:1110:in `block (2 levels) in <module:Accessors>'
/Users/user_name/svn/watir/cukes/client/features/support/common_links.rb:12:in `<module:CommonLinks>'
/Users/user_name/svn/watir/cukes/client/features/support/common_links.rb:1:in `<top (required)>'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_language.rb:129:in `load'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_language.rb:129:in `load_code_file'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:171:in `load_file'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:82:in `each'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/runtime.rb:175:in `load_step_definitions'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/runtime.rb:40:in `run!'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/cli/main.rb:43:in `execute!'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/lib/cucumber/cli/main.rb:20:in `execute'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/gems/cucumber-1.2.1/bin/cucumber:14:in `<top (required)>'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/bin/cucumber:23:in `load'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/bin/cucumber:23:in `<main>'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/Users/user_name/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'

我还尝试创建一个“CommonPage”类,并从中继承,但我遇到了一个单元化常量错误。 CommonPage 类与模块相同。

class CommonPage
include PageObject
#Header links
link(:log_out_header_link, :text => "Log Out")
link(:logo, :id => "logo")

#Logged in nav links
div(:nav, :id => "globalnav_wrapper")
nav.link(:nav_activities, :href => "/activities/")


# Footer Links
link(:contact_us_footer, :text => "CONTACT US")

# Social Media Links
link(:social_twitter, :id => "soc_twitter")
link(:social_facebook, :id => "soc_fb")
link(:social_pinterest, :id => "soc_pin")
link(:social_google_plus, :id => "soc_gplus")
link(:social_youtube, :id => "soc_yt")

end

尝试使用这两种方法中的任何一种都没有成功。

有没有人成功地在一个文件/模块/类中声明公共(public)链接(如页眉/页脚),然后在另一个类中使用它们?

任何方向将不胜感激。

最佳答案

可以从父类继承或包含模块。

例如,我在页面对象继承自父类的地方进行了以下测试。它按预期运行。

测试功能

Feature: Search

Scenario: Set google search field
When google search field set

支持\pages.rb

require 'watir-webdriver'
require 'page-object'

class CommonPage
include PageObject

text_field(:search, :name => 'q')
end

class PageA < CommonPage

end

步骤\步骤.rb

When /google search field set/ do
b = Watir::Browser.new
b.goto 'www.google.ca'
page = PageA.new(b)
page.search = 'test'
end

将 CommonPage 作为模块包含在 PageA 中时,效果类似。

可能的问题

异常与我的预期不符,但可能是由于以下行:

nav.link(:nav_activities, :href => "/activities/")

据我所知并尝试过,这是行不通的。 nav 将是未定义的。我想你想做的(注意我想你也想要一个正则表达式而不是一个字符串):

link(:nav_activities){ search.link(:href => /activities/) }

关于ruby - 公共(public)页面元素 - 它们可以被继承吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15624858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com