MichaelR
MichaelR

Reputation: 999

Cucumber: How to run the After hook only once after all examples in scenarion_outline

I have a scenario_outline which tests login screen of a website.

Scenario_outline:
    Try to login
    Verify login

    Examples:
    | User  | Pass   |
    | user1 | pass1  |
    | user2 | pass2  |

I want to be able to start the web page at the beginning and close if after all examples are done.

Running the Before hook is easy

Before do
    $start ||= false
    if ! start
        #start web page
        $start = true
    end
end

But how do i run my After hook only once after all scenarios are done?

After do
    #close web page
end

The above example simply closes the web page after the first example and causes the rest to fail. I cannot apply here what i did with the Before hook unfortunately

Upvotes: 0

Views: 3174

Answers (1)

jmccure
jmccure

Reputation: 1249

The reason the rest of the tests are failing is because your After method is not setting your global back to false:

After do
    #close web page
    $start = false
end

I don't believe there is an AfterScenario hook. But in your situation, if you want the browser open for the entire Scenario Outline, I would consider using a regular Scenario and cucumber table. The will share the browser session across each test case.

FYI: There is also an at_exit hook. It's purpose is running some code after all tests.

at_exit do #close web page end

https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks

Upvotes: 3

Related Questions