黄瓜:如何仅在censcenn_outline中的所有示例之后运行一次After挂钩 [英] Cucumber: How to run the After hook only once after all examples in scenarion_outline

查看:54
本文介绍了黄瓜:如何仅在censcenn_outline中的所有示例之后运行一次After挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cenario_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.

运行Before挂钩很容易

Running the Before hook is easy

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

但是在所有方案完成后,我如何仅运行一次After挂钩?

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

After do
    #close web page
end

上面的示例只是在第一个示例之后关闭了网页,并导致其余的失败.不幸的是,我不能在这里应用我对 Before 钩子所做的

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

推荐答案

其余测试失败的原因是因为After方法未将全局值设置为false:

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

我不相信有AfterScenario挂钩.但是根据您的情况,如果您想为整个方案大纲打开浏览器,我会考虑使用常规的方案和黄瓜表.会在每个测试用例之间共享浏览器会话.

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.

仅供参考:还有一个at_exit挂钩.目的是在所有测试之后运行一些代码.

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

at_exit做#关闭网页结束

at_exit do #close web page end

https://github.com/cucumber/cucumber/wiki/Hooks#全局钩针

这篇关于黄瓜:如何仅在censcenn_outline中的所有示例之后运行一次After挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆