【问题标题】:Uninitialized Constant (NameError) using Cucumber & SitePrism使用 Cucumber 和 SitePrism 的未初始化常量 (NameError)
【发布时间】:2019-02-22 21:59:21
【问题描述】:

我正在使用 Cucumber 和 SitePrism 创建测试自动化。运行cucumber 命令启动场景时出现错误并出现此错误:

uninitialized constant HomePage::Navbar (NameError)
.../features/page_object/home_page.rb:4:in `<class:HomePage>'
.../features/page_object/home_page.rb:1:in `<top (required)>'

我有 3 个 Ruby 类位于单独的文件中


features/page_object/home_page.rb

class HomePage < SitePrism::Page
  set_url "/"

  section :navbar, Navbar, "header"
  element :flash_message, ".flash__text"
  element :username_link, ".c-nav-list__link.u-fg--yellow"
end

features/page_object/navbar/main.rb

class Navbar < SitePrism::Section
  section :login_section, LoginSection, "#new_user_session"
  element :login_button, :xpath, "//a[@id='login_link']"
end

features/page_object/navbar/login_section.rb

class LoginSection < SitePrism::Section
  element :username_field, :xpath, "//input[@id='user_session_username'"
  element :password_field, :xpath, "//input[@id='user_session_password'"
  element :remember_me_checkbox,
          :xpath, "//input[@id='user_session_remember_me']"
  element :forgot_password_link, :xpath, "//a[text()='Lupa Password?']"
  element :submit_button, :xpath, "//button[@type='submit']"
  element :facebook_login_button, :xpath, "//a[@id='fb_login_link']"
  element :gplus_login_button, :xpath, "//a[@id='gplus_login_link']"

  def login(username, password)
    self.username_field.set(username)
    self.password_field.set(password)
    self.submit_button.click
  end
end

需要帮助,以便自动化可以正常运行(没有错误)。有什么解决办法吗?

【问题讨论】:

  • 您在 HomePage 类中使用 Navbar 而不在该类inside 中定义它。

标签: ruby cucumber site-prism


【解决方案1】:

该部分需要在使用它的页面对象之前加载。您可以将这些部分放入与使用它们的页面对象相同的文件中:

features/page_object/home_page.rb

# loads before Navbar class uses it
class LoginSection < SitePrism::Section
  ...  
end

# loads before HomePage class uses it
class Navbar < SitePrism::Section
  section :login_section, LoginSection, "#new_user_session"
  element :login_button, :xpath, "//a[@id='login_link']"
end

class HomePage < SitePrism::Page
  section :navbar, Navbar, "header"
end

或者您可以将文件分开并添加require 语句以确保每个类在它所依赖的部分之后加载:

features/page_object/home_page.rb

require_rel 'navbar/main'

class HomePage < SitePrism::Page
  set_url "/"

  section :navbar, Navbar, "header"
  element :flash_message, ".flash__text"
  element :username_link, ".c-nav-list__link.u-fg--yellow"
end

features/page_object/navbar/main.rb

require_rel 'login_section'

class Navbar < SitePrism::Section
  section :login_section, LoginSection, "#new_user_session"
  element :login_button, :xpath, "//a[@id='login_link']"
end

features/page_object/navbar/login_section.rb

class LoginSection < SitePrism::Section
  element :username_field, :xpath, "//input[@id='user_session_username'"
  element :password_field, :xpath, "//input[@id='user_session_password'"
  element :remember_me_checkbox,
          :xpath, "//input[@id='user_session_remember_me']"
  element :forgot_password_link, :xpath, "//a[text()='Lupa Password?']"
  element :submit_button, :xpath, "//button[@type='submit']"
  element :facebook_login_button, :xpath, "//a[@id='fb_login_link']"
  element :gplus_login_button, :xpath, "//a[@id='gplus_login_link']"

  def login(username, password)
    self.username_field.set(username)
    self.password_field.set(password)
    self.submit_button.click
  end
end

【讨论】:

    【解决方案2】:

    在 Ruby(更具体地说是 cucumber)中,您需要确保从内到外加载 POM。 IE。章节在页面之前。

    为什么??

    因为当您使用 SitePrism DSL 时,我们正在运行脚本化方法。如果您调用页面的脚本化方法,它们将引用可能已加载或未加载的部分类(取决于您无法控制的 Cucumber 中的自动加载器)。

    如果您首先定义不调用任何其他部分的部分,那么就不会有任何固有问题。

    现在,如果您只使用部分。或者以某种方式你确信你永远不会遇到这个问题,那么你就不会遇到任何问题。但是我们建议(也许我们应该更好地记录这一点)是您应该首先加载最小的项目(部分),然后再加载页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2013-05-28
      • 2015-01-19
      • 2015-10-29
      相关资源
      最近更新 更多