【问题标题】:What is does this error method mean: undefined method `method name' for nil:NilClass (NoMethodError)这个错误方法是什么意思:nil的未定义方法`方法名称':NilClass(NoMethodError)
【发布时间】:2018-04-12 22:12:10
【问题描述】:

我对自动化测试相当陌生,我正在使用 selenium-webdriver 在 Ruby 中编写 BDD 自动化测试场景,在运行我的测试时,它们在第一步就失败了。 (以tumblr为例)

此错误消息是什么意思,我该如何解决?任何帮助将非常感激!

在我的功能文件中:

  Feature: tumblr 
    @s1
  Scenario: Logging in to Tumblr
    Given I am on the Tumblr login page
    When I enter my login details
    Then I should be sent to the dashboard

在我的 login_page.rb 中:

def visit
  @browser.goto "#{EnvConfig.base_url}/login"
  await_on_page
end

在我的 login_step_defs.rb 中:

Given /^I am on the Odicci login page$/ do 
  @app.tumblr_login.visit
end

When /^I enter my login details$/ do 
  @app.tumblr_login.login
end

Then /^I should be sent to the dashboard$/ do 
  @app.tumblr_dashboard.go_to_dashboard
end 

最初,当我运行“cucumber features.feature”但无法找到步骤定义时,场景以“未定义”结束,因此运行“cucumber features.feature -r step_definitions”可以运行测试,但它们失败了因为这个错误信息:

Scenario: Logging in to Tumblr           # features.feature:4
Given I am on the Tumblr login page    # step_definitions/login_step_defs.rb:2
  undefined method `tumblr_login' for nil:NilClass (NoMethodError)
  ./step_definitions/login_step_defs.rb:3:in `/^I am on the Tumblr login page$/'
  features.feature:5:in `Given I am on the Tumblr login page'

【问题讨论】:

  • 您的 @app 变量是 nil(未定义) - 您在哪里定义它?
  • 这意味着nil 对象(在你的情况下是@app)不响应tumblr_login 方法。
  • 我的 @app 在我的 hooks.rb 文件中声明
  • @dianaosmani 无论如何都没有加载它。你能显示钩子文件吗?
  • 我试过了,但是因为我有很多实例变量,它正在以堆栈溢出用户的身份读取它们。如何在评论中发送代码? @maxpleaner

标签: ruby selenium-webdriver cucumber


【解决方案1】:

@maxpleaner

if ENV['HEADLESS']
  require 'headless'
  require 'selenium-webdriver'
  headless = Headless.new display: '100'
  headless.start
end

# Set up browser
# browser = Watir::Browser.new (ENV['BROWSER'] || 'chrome').to_sym
 driver = Selenium::WebDriver.for :chrome
 browser_type = ENV['BROWSER'] || 'chrome'
 $setup_done = false


Before do |scenario|

    @browser = browser
    @app = App.new @browser

    unless $setup_done
    $setup_done = true
    # This stuff will only run before the first scenario executed. Use it to set up data etc.
    end
end

After do |scenario|

end

at_exit do
    browser.quit
end

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多