ruby - Cucumber : I want to send report on email after my all the scenario get executed, 有没有像 'AfterAll' 这样的方法可以在 hooks.rb 中使用

标签 ruby cucumber watir

我已经创建了邮件功能来发送我的报告

class Email
  include PageObject
  require 'mail'

  def mailsender
      Mail.defaults do
        delivery_method :smtp,{ 
          address: "smtp.gmail.com",
          openssl_verify_mode: "none",
          port: 587,
          domain: 'gmail.com',
          user_name: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10686868686868686850777d71797c3e737f7d" rel="noreferrer noopener nofollow">[email protected]</a>' ,
          password: '*******' ,
          authentication: 'plain'
        }
      end

      Mail.deliver do
        from     'xxxxxxx.com'
        to       '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8af2f2f2f2f2cafeeff9fea4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>'
        subject  'Execution report'
        body     'PFA'
        add_file 'Automation_report.html'
      end
  end
end

我希望这个函数将在所有场景执行后执行。

这是我的 Hook 文件

# frozen_string_literal: true

require watir

Before do |scenario|
  DataMagic.load_for_scenario(scenario)
  @browser = Watir::Browser.new :chrome
  @browser.driver.manage.window.maximize
end

After do |scenario|
  if scenario.failed?
    screenshot = "./screenshot.png"
    @browser.driver.save_screenshot(screenshot)
    embed(screenshot, "image/png",)
  end
  @browser.close
end

如果我在 After do 中使用此函数,那么每次执行每个场景后它都会发送电子邮件

最佳答案

您可以在 hooks.rb 文件中使用 at_exit

at_exit do
   # your email logic goes here
end

附加说明:After Hook 将在每个场景之后执行,这就是它在每个场景执行后发送电子邮件的原因。另一方面,at_exit 钩子(Hook)仅在所有场景执行完毕后才会执行。

您可以直接在 at_exit Hook 中实现电子邮件逻辑。如果您想调用 mailsender 方法但无法在 at_exit Hook 中访问它,那么您可以创建电子邮件类/模块,如下所示。

考虑您在 GenericModules 下有电子邮件模块

module GenericModules
  module Email
     def mailsender
         # implement your logic here
     end
  end
end

然后将Email模块添加到env.rb中的world中,如下所示。

World(GenericModules::Email)

现在,即使在 at_exit Hook 中,您也应该能够访问该方法。

关于ruby - Cucumber : I want to send report on email after my all the scenario get executed, 有没有像 'AfterAll' 这样的方法可以在 hooks.rb 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60076256/

相关文章:

ruby-on-rails - 类似查找查询的 DRY 实例变量

kotlin - cucumber 与Kotlin:重复步骤定义存在问题

java - 是否可以使用通配符逻辑 '*' 通过 Apache Maven 目标和选项列出特定目录?

watir browser.attach 覆盖以前的浏览器对象

ip - 创建特定于区域的 IP 地址以进行测试

ruby-on-rails - Rails 保存返回 true 但不保存任何内容

ruby-on-rails - sidekiq队列的优先级

mysql - 使用 Ruby 将 mysql 结果导出到文件

ruby-on-rails - Spork 不会重新加载 lib 中的文件

ruby-on-rails - 如何让 cucumber 显示完整的(rails)错误信息?