本文介绍了Geb + Cucumber:在场景之间重启浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Grails 2.1.1 与 Cucumber 和 Geb 一起使用.我有一个包含 2 个场景的 Auth.feature.一个用于验证成功,另一个用于测试无效凭据.

I'm using Grails 2.1.1 with Cucumber and Geb. I have an Auth.feature that contains 2 scenarios. One is for authenticating successfully and the other is for testing invalid credentials.

我认为我必须解决这个问题的方法是让geb从第一个场景中注销用户,然后才能运行第二个场景.这是因为我的 Given 步骤会检查以确保我在登录页面.场景 1 执行后,我位于仪表板页面上.

The way I think I have to work this out is to have geb log out the user from the first scenario before it can run the second scenario. This is because my Given step checks to make sure I'm at the login page. After scenario 1 executes, I'm on a Dashboard page.

我想我的问题是我是 (a) 在完成场景之前使用 geb 注销有效用户还是 (b) 有没有办法让它在场景之间重新开始?

I guess my question is do I (a) use geb to sign out the valid user before completing the scenario or (b) is there a way to have it start over between scenarios?

现在,我已经实施了 (a) 并且效果很好.只是想知道这是否是最佳的.

Right now, I've implemented (a) and it works just fine. Just want to know if this is optimal.

这是我的功能

Feature: login to system
  As a user of the system
  I want to log in to the application
  so that I can use it

  Scenario: login
    Given I access the login page
    When I enter valid credentials
    Then I see the dashboard

  Scenario: auth fail
    Given I access the login page
    When I enter invalid credentials
    Then I see appropriate error messages

这是我的 Geb 步骤

And here is my Geb steps

Given(~'^I access the login page$') {->
  to LoginPage
  at LoginPage
}

When(~'^I enter valid credentials$') {
  page.add('[email protected]', '10001')
}

Then(~'^I see the dashboard$') {->
  at DashboardPage
}

Then(~'^I see an error message on the login page$') { ->
  at LoginPage
}

When(~'^I enter invalid credentials$') { ->
  page.add('baduser', 'paddpassword')
}

Then(~'^I see appropriate error messages$') { ->
  at LoginPage
  // check for error message
}

推荐答案

根据我所做的更多研究,看起来有几种方法可以解决这个问题:

Based on some more research I've done, it looks like there are a few ways to handle this:

  • 就像我已经在做的那样,在场景结束时退出(或者你可以在开始时这样做
  • 创建自己的退出场景
  • 在 env.groovy 钩子前,添加到LogoutPage
  • 使用背景退出

这篇关于Geb + Cucumber:在场景之间重启浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:08