简体   繁体   English

在step_definitions和PageObject方法中共享@variable可能吗?

[英]Share @variable in step_definitions and PageObject methods possible?

Is there an easy way to reference @current_page in both the step_definition.rb files and the PageObject.rb files? 在step_definition.rb文件和PageObject.rb文件中都有引用@current_page的简便方法吗?

Examples using the @current_page in the Calabash docs force you to always do something like: 在Calabash文档中使用@current_page的示例迫使您始终执行以下操作:

@current_page = @current_page.login(user)

After login you are on a new page and you must pass back a reference to this for the @current_page . login您将进入新页面,并且必须将@current_page的引用传递回去。 It would be nice to just set @current_page in the login method and not have to do that @current_page = @current_page.login(user) assignment every time you call a method that brings you to a new page. @current_pagelogin方法中设置@current_page ,而不必在每次调用将您带到新页面的方法时都进行@current_page = @current_page.login(user)分配。

Is there a good way to do this? 有什么好方法吗?

Is there a good way to do this? 有什么好方法吗?

It is not recommended that you keep a reference to the current page. 不建议您保留对当前页面的引用。

I could demonstrate a number of ways to keep a reference, but I don't want to because it is not a good pattern. 我可以演示多种保留引用的方法,但是我不想这样做,因为它不是一个好的模式。

At risk of getting my response marked as a non-answer, I will try to explain why. 冒着将我的回答标记为不回答的风险,我将尝试解释原因。

Examples using the @current_page in the Calabash docs force you to always do something like: 在Calabash文档中使用@current_page的示例迫使您始终执行以下操作:

In my opinion, the docs are not good. 我认为文档并不好。 I have been trying to get them changed. 我一直在努力改变他们。 There is general agreement on this topic among the Calabash developers, but none of us has had the time to change them. 葫芦开发人员对此主题达成了普遍共识,但是我们当中没有人有时间进行更改。

It is not a best practice to use @current_page to track the current page between steps. 使用@current_page跟踪步骤之间的当前页面不是最佳实践。 The reason is that the reader can never know what the value of the @current_page is just by looking at it: it could have been set to anything by subsequent step. 原因是读者无法仅通过查看就知道@current_page的值是什么:可以在后续步骤中将其设置为任何值。

The best practice is to create a temporary page object whenever you need one. 最佳做法是在需要时创建一个临时页面对象。

# Bad
@current_page = @current_page.login(user)

# Good
login_page = page(LoginPage).await
some_other_page = login_page.login(user)
some_other_page.await

force you to always do something like: 强制您始终执行以下操作:

The @current_page is a Cucumber World variable; @current_page是黄瓜世界变量; there is nothing special about it. 没有什么特别的。 It could have been called: @page or @screen or @foobar . 它可能被称为: @page@screen@foobar When I say there is nothing special I mean that Calabash does not use @current_page anywhere internally. 当我说没有什么特别的意思时,我的意思是Calabash在内部任何地方都不使用@current_page

It would be nice to just set @current_page in the login method and not have to do that @current_page = @current_page.login(user) assignment every time you call a method that brings you to a new page. 最好在登录方法中设置@current_page,而不必在每次调用将您带到新页面的方法时都进行@current_page = @ current_page.login(user)分配。

Is there a good way to do this? 有什么好方法吗?

In general, it is not a good idea save state in your cucumber tests or page model. 通常,将状态保存在黄瓜测试或页面模型中不是一个好主意。 If you need a piece of information in a Step or method, you should ask for it by querying the app. 如果您需要步骤或方法中的一条信息,则应通过查询应用程序来询问。

There are, of course, exceptions. 当然也有例外。 Imagine an app with a Dashboard page that has a Reminders icon with badge count that represents the number of unread reminders. 想象一下一个应用程序,其“ 仪表板”页面上有一个“ 提醒”图标,其徽章计数代表未读提醒的数量。

 Scenario:  Verify the reminders badge count
   Given I am looking at the Dashboard
   And I make a note of the reminders badge count
   When I go to the reminders page
   Then the reminders badge count should match the unread reminders

It is reasonable to store the badge count in a Cucumber World variable so you can use it in a subsequent Step. 将徽章计数存储在Cucumber World变量中是合理的,因此您可以在后续步骤中使用它。

 And(/^I make a note of the reminders badge count$/) do
   dashboard = page(Dashboard).await
   @reminder_badge_count = dashboard.reminder_badge_count
 end

 When(/^I go to the reminders page$/) do
   dashboard = page(Dashboard).await
   dashboard.navigate_to(:reminders)
 end

 Then(/^the reminders badge count should match the unread reminders$/) do
   reminders_page = page(Reminders).await
   unread_actual = reminders_page.unread_reminders
   unless unread_actual == @reminder_badge_count
     screenshot_and_raise("Expected badge count '#{@reminder_badge_count}' to match unread count '#{unread_actual}") 
   end
 end

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

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