require 'rake' require 'pry' require 'rexml/document' require 'gmail' require 'zip' include REXML # Usage Sample, # rake email["http://jenkins.io:8080/job/Automation/133","https://github.com/nareshnavinash/graber-ruby","master","SHA Key","commiter_email@testmail.com","nareshnavinash@gmail.com;naresh@testmail.com"] task :email, :jenkins_url, :git_url, :git_branch, :git_commit, :git_commiter_email, :to_email do |t, args| args.with_defaults(:jenkins_url => "http://jenkins.io:8080/job/Automation/", :git_branch => "master", :to_email => "nareshnavinash@gmail.com") args.with_defaults(:git_url => "https://github.com/nareshnavinash/graber-ruby", :git_commit => "SHA Key", :git_commiter_email => "commiter_email@gmail.com") puts args[:jenkins_url] puts args[:git_commit] puts args[:to_email] passed_count = 0 failed_count = 0 break_count = 0 failed_testcases = [] break_testcases = [] Dir["reports/allure/*.xml"].each do |f| xmlfile = File.new("#{f}") xmldoc = Document.new(xmlfile) XPath.each(xmldoc, "ns2:test-suite/test-cases/test-case").each do |e| if e.attributes["status"] == "passed" passed_count = passed_count + 1 elsif e.attributes["status"] == "failed" failed_count = failed_count + 1 failed_testcases << XPath.first(e,"name").text else break_count = break_count + 1 break_testcases << XPath.first(e,"name").text end end end puts "Failed - " puts failed_testcases puts "Break - \n" puts break_testcases puts passed_count, failed_count, break_count #Zipping the reporting folders File.delete("#{Pathname.pwd}/reports/html.zip") if File.exist?("#{Pathname.pwd}/reports/html.zip") File.delete("#{Pathname.pwd}/reports/pretty.zip") if File.exist?("#{Pathname.pwd}/reports/pretty.zip") directory = "#{Pathname.pwd}/reports/pretty/" zipfile_name = "#{Pathname.pwd}/reports/pretty.zip" Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| Dir[File.join(directory, '*')].each do |file| zipfile.add(file.sub(directory, ''), file) end end directory = "#{Pathname.pwd}/reports/html/" zipfile_name = "#{Pathname.pwd}/reports/html.zip" Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| Dir[File.join(directory, '*')].each do |file| zipfile.add(file.sub(directory, ''), file) end end #Email parameters email = "custom_email@gmail.com" #to send the email from this account password = "Test@123" to_email = args[:to_email] || "nareshnavinash@gmail.com" if failed_count == 0 and break_count == 0 email_subject = "Automation - Report #{Time.now}" else email_subject = "Automation - Report #{Time.now} - Alert failure(s) found !!!" end if failed_testcases.count != 0 final_string = "" failed_testcases.each do |f| final_string = final_string + "
  • #{f}
  • " end failed_testcases_string = " Failed Test Cases, " else failed_testcases_string = "" end if break_testcases.count != 0 final_string = "" break_testcases.each do |f| final_string = final_string + "
  • #{f}
  • " end break_testcases_string = " Broken Test Cases, " else break_testcases_string = "" end email_values = { "To" => "#{to_email}", "Subject" => "#{email_subject}", "Cc" => "", "Body" => "
     
     
     
     

    Automation Report

     
    Hi Team,
     
    GraphQL Automation Regression Status,
     
    Passed Test Case count : #{passed_count}
    Failed Test Case count : #{failed_count}
    Broken Test Case count : #{break_count}
    Run Completed at : #{Time.now}
     
    HTML Reports and Command line logs are attached for reference. Below are the few parameters that are responsible for this job trigger. If any failures or broken cases found in the result kindly trace back from the last commit ID with the last commited user.
     
    Jenkins URL: navigate_to_jenkins
    Git Branch: #{args[:git_branch]}
    Last Commit ID: #{args[:git_commit]}
    Last Commit by: #{args[:git_commiter_email]}
     
    Follow the following Link for detailed analysis in allure,
     
    Allure Report
     
    This email is auto-triggered from Jenkins Automation Job, for any queries please contact nareshnavinash@gmail.com or nareshsekar@zoho.com.
     
    #{failed_testcases_string}
     
    #{break_testcases_string}
     
     
     
    Thanks,
     
    SDET Team.
     
     
    Copyright © 2020 Naresh Sekar. All Rights Reserved. Confidential attachments!
     
    Beware Before Forwarding this e-mail!
     
    nareshnavinash@gmail.com
     
     
    ", "Body Type" => "html", "Attachments" => "reports/html/*.html" } puts "Send email to below values #{email_values.to_s}" @current_instance = Gmail.new(email,password) @current_instance.deliver do to email_values["To"] if email_values["To"] != nil cc email_values["Cc"] if email_values["Cc"] != nil bcc email_values["Bcc"] if email_values["Bcc"] != nil subject email_values["Subject"] if email_values["Subject"] != nil if email_values["Body Type"] == "text" && email_values["Body Type"] != nil text_part do body email_values["Body"] end elsif email_values["Body Type"] == "html" && email_values["Body Type"] != nil html_part do content_type 'text/html; charset=UTF-8' body email_values["Body"] end else body email_values["Body"] if email_values["Body"] != nil end if email_values["Attachments"] != nil attachments_values = Dir["reports/*.zip"] attachments_values.each do |attachment_value| file_path = "#{Pathname.pwd}/#{attachment_value}" add_file file_path end end end puts "Logout the gmail instance" @current_instance.logout end