我已经创建了邮件功能来发送我的报告
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/