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

不问归期 提交于 2021-02-08 10:16:16

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/29874866/cucumber-how-to-run-the-after-hook-only-once-after-all-examples-in-scenarion-ou

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