简体   繁体   English

让黄瓜步骤在另一步骤中验证页面对象设置的变量

[英]Have Cucumber Step Verify Variable Set By A Page Object In Another Step

I am using Cheezy's PageObject to setup some cucumber tests. 我正在使用Cheezy的PageObject设置一些黄瓜测试。 I have everything pretty much setup like Jeff Morgan's book "Cucumber & Cheese". 我已经完成了几乎所有设置,例如Jeff Morgan的书“ Cucumber&Cheese”。

Right now I have a page object "PublishPage" setup that has a method that sets a variable @tag. 现在,我有一个页面对象“ PublishPage”设置,该对象具有设置变量@tag的方法。 For example I have in the file publish_page.rb 例如,我在文件publish_page.rb中

Class PublishPage
  def tag
  #some steps left out
  @tag = "123"
  end

  def verify_tag
  #some steps left out
  @tag.should include "2"
  end
end

In the Cucumber steps, for one of the steps i have on_page(PublishPage).tag , and then in another step i have on_page(PublishPage).verify_tag . 在Cucumber步骤中,对于其中一个步骤,我具有on_page(PublishPage).tag ,然后在另一步骤中,我具有on_page(PublishPage).verify_tag In my env.rb file I have require 'rspec-expectations' . 在我的env.rb文件中,我require 'rspec-expectations'

The problem is that when I run this code I get an error that says undefined method 'include' for #<PublishPage:xxxxxx> . 问题是,当我运行此代码时,出现一条错误,提示undefined method 'include' for #<PublishPage:xxxxxx> But if I move the code inside the verify_tag method into the steps everything works except it does not have access to @tag... 但是,如果我将verify_tag方法中的代码移到步骤中,则所有内容都可以正常工作,除了它无法访问@tag ...

This should be as simple as adding 这应该像添加一样简单

include RSpec::Matchers

to your page object. 到页面对象。

The other alternative would be to expose @tag through some method and then in your Cucumber step say something like 另一种选择是通过某种方法公开@tag ,然后在您的Cucumber步骤中说类似

on_page(PublishPage).the_displayed_tag.should include("2")

Every time you invoke on_page(PublishPage), it will instantiate a new page object. 每次调用on_page(PublishPage)时,它将实例化一个新的页面对象。 You are most likely getting the "cannot convert nil to string" error because you are referencing an instance variable from a new object, hence it's value being nil. 您很可能会收到“无法将nil转换为字符串”错误,因为您引用的是新对象中的实例变量,因此其值为nil。 You should no longer get that error if you instantiate your page object only once, between calling page.tag and page.verify_tag. 如果仅在调用page.tag和page.verify_tag之间实例化页面对象一次,则不应再出现该错误。

Doing things this way will use one instance of your page object, allowing you to persist between step definitions. 通过这种方式执行操作将使用页面对象的一个​​实例,从而使您能够在步骤定义之间进行持久化。

When /I publish a tag/ do
   #on_page(PublishPage).tag
   @publish_page = PublishPage.new @browser
   @publish_page.tag
end

Then /I should have my tag/ do
   @publish_page.verify_tag 
end

Hope this helps! 希望这可以帮助!

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

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