Cucumber:如何在场景大纲中的所有示例之后只运行一次 After 钩子 [英] Cucumber: How to run the After hook only once after all examples in scenarion_outline

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

问题描述

我有一个测试网站登录屏幕的场景大纲.

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#全局钩子

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

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