在 Cucumber 中的场景大纲之前运行登录步骤

发布于 2024-09-16 23:51:42 字数 842 浏览 3 评论 0原文

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

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

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 个角色和数百个页面要检查,因此会产生大量开销每次大纲运行时都运行登录步骤。

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

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

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          |
  ...

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇 2024-09-23 23:51:42

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

如果您可以接受像这样抽象的功能

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

那么您可以在步骤 defs 中更有效地完成所有工作

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

# 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 的全部功能可以满足您的特殊需求。基本思想是,您始终可以采取多个 Cucumber 操作并将它们抽象为一个 Cucumber 操作。

希望有用

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

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

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.

Hope thats useful

转身泪倾城 2024-09-23 23:51:42

您可以实现 Given 步骤,使每个角色仅登录一次:

# lazily log in each role as needed, and keep the login in a hash table
$logins = Hash.new do |_logins, role|
  _logins[role] = do_expensive_login(role)
end
Given /^I am logged in as "([^"]+)"$/ |role|
  @login = $logins[role]
end

当然,如果未来的步骤可以更改登录状态,或者更改世界,使登录不再有效,这可能会让你陷入困境,所以要小心行事。

You could implement the Given step to only log in once for each role:

# lazily log in each role as needed, and keep the login in a hash table
$logins = Hash.new do |_logins, role|
  _logins[role] = do_expensive_login(role)
end
Given /^I am logged in as "([^"]+)"$/ |role|
  @login = $logins[role]
end

Of course, if the future steps can change the state of the login, or change the world such that the login is no longer valid, this might hose you down the line, so tread carefully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文