这个错误方法的含义是什么:nil的未定义方法`method name':NilClass(NoMethodError)

时间:2018-04-11 14:23:48

标签: ruby selenium-webdriver cucumber

我对自动化测试相当陌生,我正在使用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'时,步骤定义无法定位,因此场景最终结束为'undefined',因此运行'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'

1 个答案:

答案 0 :(得分:0)

@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