Ruby Cucumber:我想在我的所有场景执行完毕后通过电子邮件发送报告,有没有像';毕竟';可以在hooks.rb中使用

Ruby Cucumber:我想在我的所有场景执行完毕后通过电子邮件发送报告,有没有像';毕竟';可以在hooks.rb中使用,ruby,cucumber,watir,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, d

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

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: 'xxxxxxxx@gmail.com' ,
          password: '*******' ,
          authentication: 'plain'
        }
      end

      Mail.deliver do
        from     'xxxxxxx.com'
        to       'xxxxx@test.com'
        subject  'Execution report'
        body     'PFA'
        add_file 'Automation_report.html'
      end
  end
end

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

这是我的钩子

# 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将在每个场景后执行,这就是为什么它会在每个场景执行后发送电子邮件的原因。另一方面,只有在执行所有场景后,才会在退出时执行
hook

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

World(GenericModules::Email)
假设您在GenericModules下有电子邮件模块

module GenericModules
  module Email
     def mailsender
         # implement your logic here
     end
  end
end
然后将电子邮件模块添加到
env.rb
中的
world
,如下所示

World(GenericModules::Email)

现在,即使在退出时,您也应该能够访问该方法。

现在我得到了回溯(最近一次调用):/Users/features/support/hooks.rb:22:in
block-in::未定义的局部变量或mailssender方法,用于main:Object(NameError)“``我在使用它时就像``在`出口do mailsender end``一样”`@Akshay用
world
实现更新了答案。让我们知道它是如何进行的。为什么你要使用selenium代码来截图,最大化窗口,而你实际使用的是WATIR代码?