Geb + Cucumber: Restart Browser Between Scenarios

坚强是说给别人听的谎言 提交于 2019-12-01 09:43:51

问题


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.

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.

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?

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

Here is my feature

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

And here is my Geb steps

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

When(~'^I enter valid credentials$') {
  page.add('user_10001@test.com', '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
}

回答1:


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

  • Just like I am already doing it, by logging out at the end of a scenario (or you could do it at the beginning
  • Make logging out its own scenario
  • In the env.groovy Before hook, add to LogoutPage
  • Logout using a Background



回答2:


Add the following line to the After hook in env.groovy:

bindingUpdater.browser.clearCookies()


来源:https://stackoverflow.com/questions/14096907/geb-cucumber-restart-browser-between-scenarios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!