简体   繁体   English

(Ruby)在黄瓜测试中不能使用硒命令吗?

[英](Ruby) Cannot use selenium commands in cucumber tests?

I am trying to save object definitions in a "home page" file and simply call those methods whenever I need to use that button/link/image/etc. 我试图将对象定义保存在“主页”文件中,并在需要使用该按钮/链接/图像/等时简单地调用这些方法。 But for some reason the selenium commands are bringing up a NoMethodError . 但是由于某些原因,selenium命令显示了NoMethodError When I simply run the cucumber command while on the features folder in the terminal, I get these errors: 当我在终端的功能文件夹中简单地运行cucumber命令时,出现以下错误:

When I click on Site Management           # features/step_definitions/steps.rb:17
  undefined method `find_element' for nil:NilClass (NoMethodError)
  ./features/lib/pages/home.rb:3:in `siteMgmt'
  ./features/step_definitions/steps.rb:18:in `/^I click on Site Management$/'
  features/test.feature:6:in `When I click on Site Management'

So in other words, it's trying to "click on site management," the code moves to the Home class, the SiteMgmt method (great!) and then fails when trying to run the selenium find_method method. 因此,换句话说,它试图“单击站点管理”,代码移至Home类的SiteMgmt方法(太好了!),然后在尝试运行selenium find_method方法时失败。 I thought I might have to add a require selenium-webdriver at the top of home.rb , but a) that's NOT the case in steps.rb and, even if I add it, it doesn't work. 我以为我可能必须在home.rb的顶部添加一个require selenium-webdriver ,但是a)在steps.rb不是这样,即使我添加了它,也无法正常工作。

Here is the folder structure: 这是文件夹结构:

features/
    --test.feature
    lib/
        pages/
            --home.rb
    step_definitions/
        --steps.rb
    support/
        --env.rb

env.rb env.rb

require 'selenium-webdriver'

Dir[File.dirname(__FILE__) + "/../lib/pages/*.rb"].each {|file| require file }

Before do |scenario|
  @driver = Selenium::WebDriver.for :chrome
  @url = "URL goes here"
end

After do |scenario|
  @driver.quit
end

test.feature 测试功能

Feature: Proof of Concept
  Stack overflow help!

  Scenario:
    Given I am logged into the site
    When I click on Site Management
    Then the Site Management page should load

steps.rb steps.rb

Given(/^I am logged into AMP$/) do
  @driver.get @amp_url
end

When(/^I click on Site Management$/) do
  link = Home.new.siteMgmt
  link.click
end

Then(/^the Site Management page should load$/) do
  # assert here
end

home.rb home.rb

class Home
    def siteMgmt
        elem = @driver.find_element(:xpath, '//*[@id="body"]/section[2]/ul/li[1]/h3/a')
        return elem
    end
end

Thanks for all your help! 感谢你的帮助!

The @driver instance variable that's created in the Before block isn't available to an instantiated Home object. Before块中创建的@driver实例变量不适用于实例化的Home对象。 You could add a parameter to the site_mgmt method and pass the @driver instance variable in. Here's a contrived example: 您可以在site_mgmt方法中添加一个参数,并在site_mgmt传递@driver实例变量。这是一个人为的示例:

class Home
  def site_mgmt(driver)
    elem = driver.find_element(:id, "logo")
  end
end

require 'selenium-webdriver'

@driver = Selenium::WebDriver.for :chrome
@driver.navigate.to "http://www.iana.org/domains/reserved"

link = Home.new.site_mgmt(@driver)
link.click

A couple of notes: 1) variables in ruby are snake_case'd (ie site_mgmt instead of siteMgmt ; and 2) return elem in site_mgmt isn't needed because ruby methods implicitly return. 一些注意事项:1)ruby中的变量是snake_case'd(即site_mgmt而不是siteMgmt ;以及2)不需要在site_mgmt return elem ,因为ruby方法隐式返回。

Well, it turns out all I had to do is turn @driver to $driver. 好吧,事实证明,我要做的就是将@driver变成$ driver。 I'm still learning Ruby and didn't realize the difference. 我仍在学习Ruby,但没有意识到两者之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM