在黄瓜中的场景大纲之前运行登录步骤 [英] Running a login step prior to scenario outline in cucumber

查看:16
本文介绍了在黄瓜中的场景大纲之前运行登录步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 webrat/mechanize 的 cucumber 来测试 PHP 站点,并且我正在尝试通过避免运行不必要的步骤来提高测试运行的速度.

I'm using cucumber with webrat/mechanize to test a PHP site and I'm trying to improve the speed the tests run by avoiding running unnecessary steps.

我想使用场景大纲来检查大量页面是否可以访问/受保护,具体取决于登录的用户:

I want to use a scenario outline to check a whole lot of pages are accessible/protected depending on the user who is logged in:

Scenario Outline: Check page access is secure
  Given I am logged in as "<user>"
    And I am on <page>
  Then I should see "<message>"
Examples:
  |user  |page      |message                |
  |admin |home page |Welcome to my site     |
  |admin |admin page|Site administration    |
  |editor|home page |Welcome to my site     |
  |editor|admin page|Access denied          |
  |guest |home page |Please login           |
  |guest |admin page|Access denied          |
  ...

这可行,但鉴于我有 10 个角色和数百个页面要检查,每次大纲运行时运行登录步骤都会产生大量开销.

This works, but given I have 10 roles and hundreds of pages to check, there is a lot of overhead in running the login step every time the outline runs.

我想知道是否有一种方法可以为每个角色运行一次登录步骤,然后依次访问每个页面,而无需每次都登录.即运行登录,访问 1,访问 2,访问 3"而不是登录,访问 1,登录,访问 2,登录,访问 3".

I'm wondering if there is a way to run the login step once for each role, then visit each page in turn without needing to login every time. i.e run "login, visit 1, visit 2, visit 3" instead of "login, visit 1, login, visit 2, login, visit 3".

我尝试过使用钩子和背景,但似乎找不到可行的方法.这可能吗?

I've tried using hooks, and Background, but can't seem to find an approach that works. Is this possible?

推荐答案

与其将所有可访问/受保护的信息放在功能中,不如考虑将它们放在步骤 defs 中(更好的是使用您的应用程序,但如果您的应用程序不在进程中,这并不容易)

Instead of putting all the information about what is accessible/protected in the feature, consider putting them in the step defs (even better would be to use the definitions in your application, but that isn't easy if your app is not in process)

如果你能接受一个抽象的特征

If you can live with a feature that is as abstract as

Given I am an admin
Then I should be able to access admin pages

然后你可以在步骤 defs 中更高效地完成所有工作

Then you can do all the work much more efficiently in step defs

以下只是一个代码草图,让您了解您可以做什么......

Following is just a code sketch to give some idea of what you can do ...

# step def
module AccessHelper
  AdminPages = {
    {page: ..., msg: ...
    ...
  }
  def login_as ... ; end
  def correct_message? msg ...; end
  def check_admin_access_for user
    @errors = []
    login_as @I
    AdminPages.each do |page|
      visit page[:path]
      errors << page unless correct_message?
    end
  end
end
World(AccessHelper)

Then "I should be able to access admin pages" do
  check_admin_access_for @I
  @errors.should be_empty
end

您当然可以使用 ruby​​ 的全部功能来扩展它以满足您的特定需求.基本思想是你总是可以采取几个黄瓜动作并将它们抽象成一个黄瓜动作.

You can of course expand this using the full power of ruby to meet you particular needs. The fundamental idea is that you can always take several cucumber actions and abstract them into one cucumber action.

希望有用

这篇关于在黄瓜中的场景大纲之前运行登录步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆