cheat sheet · QA · Ruby · Ruby 1.9 · ruby excercise · selenium · selenium-webdriver · Selenium-webdriver · watir · watir-webdriver

Custom HTML report for Ruby(Watir/Selenium) Base Automation framework

Requirement:

Sample HTML report for any ruby based Automation Testing Framework

Installation/Changes :

1. Create code.rb file and paste the below code.

2. Create folder named as Results in the same location.

Code:

def get_time_name
$time=Time.now
$time_name="#{$time.hour.to_s}-#{$time.min.to_s}-#{$time.sec.to_s}-#{$time.day.to_s}-#{$time.mon.to_s}-#{$time.year.to_s}"
$result_date = "#{$time.day.to_s}-#{$time.month.to_s}-#{$time.year.to_s}"
end
def create_report
get_time_name
@result_file_name="Report"+"-"+$time_name
@full_file_name="Results/#{@result_file_name}.html"
$report=File.open(@full_file_name,'w')
end
def insert_head_title(title)
$report.puts "<html><head>
<title> #{title} </title>
</head>"
end
def start_table
$report=File.open(@full_file_name,'a')
$report.puts "<table border=1>
<tr>
<th>Test Case Name</th>
<th>Test Case Description</th>
<th>Browser Name</th>
<th>Result</th>
<th>Remarks</th>
</tr>"
$report.close
end
def insert_reportname_date(name,date)
$report.puts "<body bgcolor='#5CB3FF'>
<p align='left' size=2>
<b><img src='https://encrypted-tbn1.google.com/images?q=tbn:ANd9GcQB0l0xnGOHuRPFMMMi-OVg39nfAU1Ogvxr7Okk7DD8ZpqlMF9r'></img> </b>
</p>
<p size=12>
<center> <b><u>#{name} </u></b></center>
</p>
<p align='right' size=12>
<b>Date: #{date} </b>
</p>"
$report.close
end
def report_row(*details)
$report=File.open(@full_file_name,'a')
name=details[0]
desc=details[1]
browser=details[2]
result=details[3]
reason=details[4]
if result.downcase == "pass"
$report.puts "<tr>
<td>#{name}</td>
<td> #{desc}</td>
<td> #{browser} </td>
<td bgcolor='green'>#{result}</td>
<td>#{reason}</td>
</tr>"
else
$report.puts "<tr>
<td>#{name}</td>
<td> #{desc}</td>
<td> #{browser} </td>
<td bgcolor='red'>#{result}</td>
<td>#{reason}</td>
</tr>"
end
$report.close
end
def close_table
$report=File.open(@full_file_name,'a')
$report.puts "</table></br>"
$report.close
end
def summary_report(overall,passed,failed)
$report_overall=overall
$report_pass=passed
$report_fail=failed
$report=File.open(@full_file_name,'a')
$report.puts "<p> <b>Total Test cases : #{$report_overall} </b></p>
<p> <b>Passed : #{$report_pass} </b></p>
<p> <b>Failed : #{$report_fail} </b></p>
</body>
</html>"
$report.close
end

create_report
insert_head_title("Raveendran -- Sample HTML Report")
insert_reportname_date("My Test Report",$result_date )
start_table
report_row("Check Google Home Page","Check The Title in Google Home Page","IE","PASS","Title Present")
report_row("Check Google Home Page","Check The Title in Google Home Page","FF","FAIL","Title Not Present")
close_table
summary_report(2,1,1)

 

Output HTML File:

Advertisements

16 thoughts on “Custom HTML report for Ruby(Watir/Selenium) Base Automation framework

  1. Hi there,
    Really appreciable your effort. But can you please tell me how this code.rb will access my test case ? my test cases are in functional/ and i have placed Results/ at the same level. but how do i tell my test.rb to send results to code.rb ??
    Thank you in advance for your help.
    Qamar

    1. 1. Place code.rb file in the same dir where test.rb is available

      2. In Your Test.rb File Add this

      require 'code.rb'

      create_report
      insert_head_title("Raveendran -- Sample HTML Report")
      insert_reportname_date("My Test Report",$result_date )
      start_table
      report_row("Check Google Home Page","Check The Title in Google Home Page","IE","PASS","Title Present")
      report_row("Check Google Home Page","Check The Title in Google Home Page","FF","FAIL","Title Not Present")
      close_table
      summary_report(2,1,1)

        1. Hi Raveendran,
          I have fixed that load error but still not getting the reports,I am not able to send the dat to the code.rb file.
          Its coming only what ever we have hadr coded as follows:

          create_report
          insert_head_title(“Raveendran — Sample HTML Report”)
          insert_reportname_date(“My Test Report”,$result_date )
          start_table
          report_row(“Check Google Home Page”,”Check The Title in Google Home Page”,”IE”,”PASS”,”Title Present”)
          report_row(“Check Google Home Page”,”Check The Title in Google Home Page”,”FF”,”FAIL”,”Title Not Present”)
          close_table
          summary_report(2,1,1)

          Can you please help me out for this,

          Thanks

          1. Hi MANISH,

            You need to update the content

            create_report
            insert_head_title(“Raveendran — Sample HTML Report”)
            insert_reportname_date(“My Test Report”,$result_date )
            start_table
            report_row(“Check Google Home Page”,”Check The Title in Google Home Page”,”IE”,”PASS”,”Title Present”)
            report_row(“Check Google Home Page”,”Check The Title in Google Home Page”,”FF”,”FAIL”,”Title Not Present”)
            close_table
            summary_report(2,1,1)

            These are all sample report data.

            can you please send your code and call me at 91 9941414834

            1. Hi,
              Thanks for your reply,
              I have written my code inside lib/tasks/Selenium_Test/login.rb

              require ‘rubygems’
              require ‘selenium-webdriver’
              require ‘test/unit’

              require ‘./code’

              class LoginTest 10
              @driver.manage.window.maximize
              # navigate to the login page
              @driver.get ‘http://tsop3.bitlasoft.com’

              # sending login details through csv file
              sleep 4
              i = 0
              File.foreach(File.dirname(__FILE__)+”/login.csv”, {:headers => :first_row, :return_headers => true}) do |row|
              row = row.split(‘,’)
              sleep 2
              if i > 0
              element = @driver.find_element(:id => ‘login’)
              element.clear
              element.send_keys(row[0])
              sleep 2
              element = @driver.find_element(:id => ‘password’)
              element.clear
              element.send_keys(row[1])
              sleep 2
              @driver.find_element(:name => ‘commit’).click
              sleep 2
              end
              i = i + 1
              end

              end

              # closing the browse
              def teardown
              p “Testing Logout”
              @driver.find_element(:xpath, “//*[@id=’header_logobar’]/div/div/div[3]/div/div/a[1]/button”).click
              p “Closing browser”
              @driver.quit
              end

              # test dashboard from left side bar
              def test_a_dashboard
              p “Testing Dasbboard Module”
              @driver.find_element(:xpath, “html/body/section/div/div[1]/div/ul/li[1]/a/div/i”).click
              sleep 4

              p “Bussiness Summary”
              p “Testing Today from select box”
              @driver.find_element(:id, “pie_id”).click
              sleep 4
              p “Testing Yesterday from select box”
              select = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath,”//*[@id=’pie_chart_date_range’]”))
              select.select_by(:text,”Yesterday”)
              @driver.find_element(:id, “pie_id”).click
              sleep 4
              p “Testing Cutom date from Select box”
              select.select_by(:text,”Custom Date”)
              @driver.find_element(:xpath, “//*[@id=’dashboard-filter-custom-date’]/td/table/tbody/tr/td[1]/span/p/img”).click
              select_start_date = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath,”//*[@id=’ui-datepicker-div’]/div[1]/div/div/select”))
              select_start_date.select_by(:text, “Jan”)
              @driver.find_element(:xpath, “//*[@id=’ui-datepicker-div’]/div[4]/button[2]”).click

              @driver.find_element(:xpath, “//*[@id=’dashboard-filter-custom-date’]/td/table/tbody/tr/td[3]/span/p/img”).click
              @driver.find_element(:xpath, “//*[@id=’ui-datepicker-div’]/div[4]/button[1]”).click
              @driver.find_element(:xpath, “//*[@id=’ui-datepicker-div’]/div[4]/button[2]”).click
              @driver.find_element(:id, “pie_id”).click

              @driver.find_element(:id, “booking_type_2”).click
              @driver.find_element(:id, “pie_id”).click

              p “Comparision Chart”
              p “Testing For Today for source Branch”
              @driver.find_element(:id, “bar_id”).click

              p “Testing for Yesterday”
              select_drop_down = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath,”//*[@id=’bar_chart_date_range’]”))
              select_drop_down.select_by(:text, “Yesterday”)
              @driver.find_element(:id, “bar_id”).click

              p “Testing for Custom date”
              select_drop_down.select_by(:text, “Custom Date”)
              @driver.find_element(:xpath, “//*[@id=’dashboard-filter-custom-date1′]/table/tbody/tr/td[1]/span/p/img”).click
              select_from_date = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath,”//*[@id=’ui-datepicker-div’]/div[1]/div/div/select”))
              select_from_date.select_by(:text, “Jan”)
              @driver.find_element(:xpath, “//*[@id=’ui-datepicker-div’]/div[4]/button[2]”).click
              @driver.find_element(:id, “bar_id”).click

              select_source = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath, “//*[@id=’bar_chart_select’]”))
              select_source.select_by(:text, “Agent”)
              @driver.find_element(:id, “bar_id”).click
              sleep 5

              select_source.select_by(:text, “API”)
              @driver.find_element(:id, “bar_id”).click

              p “Testing for check box issue date”
              @driver.find_element(:id, “booking_bar_type_2”).click
              @driver.find_element(:id, “bar_id”).click

              p “Testing for check box Low Ten Performers”
              @driver.find_element(:id, “booking_bar_select_4”).click
              @driver.find_element(:id, “bar_id”).click

              end

              # test booking from left side bar
              def test_b_booking
              p “Testing Booking Module”
              @driver.find_element(:xpath, “html/body/section/div/div[1]/div/ul/li[2]/a/div/i”).click
              sleep 5
              @driver.find_element(:xpath, “//*[@id=’search-form’]/table/tbody/tr[2]/td/input”).click
              sleep 4
              @driver.find_element(:xpath, “//*[@id=’search-results-header’]/span[3]/a[2]/span”).click
              sleep 2
              @driver.find_element(:xpath, “//*[@id=’search-results-header’]/span[1]/a[1]/span”).click
              sleep 6
              @driver.find_element(:xpath, “//*[@id=’194625′]/td[1]/b/a”).click
              sleep 6
              @driver.find_element(:id, “span_1_4”).click
              @driver.find_element(:id, “none_agent”).click

              select_boarding_at = Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, “searchbus_boarding_at”))
              select_boarding_at.select_by(:value, “8847”)

              select_drop_at = Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, “searchbus_drop_off”))
              select_drop_at.select_by(:value, “19802”)

              # sending passenger details through csv file
              i = 0
              File.foreach(File.dirname(__FILE__)+”/passenger_details.csv”, {:headers => :first_row, :return_headers => true}) do |row|
              row = row.split(‘,’)
              sleep 2
              if i > 0
              element = @driver.find_element(:id => ‘ticket_main_passenger_name’)
              element.clear
              element.send_keys(row[0])
              sleep 2

              element = @driver.find_element(:id => ‘ticket_main_passenger_age’)
              element.clear
              element.send_keys(row[1])
              sleep 2

              element = @driver.find_element(:id => ‘ticket_phone_number’)
              element.clear
              element.send_keys(row[2])
              sleep 2

              element = @driver.find_element(:id => ‘ticket_email’)
              element.clear
              element.send_keys(row[3])
              sleep 2

              element = @driver.find_element(:id => ‘ticket_other_number’)
              element.clear
              element.send_keys(row[4])
              sleep 2

              element = @driver.find_element(:id => ‘remarks’)
              element.clear
              element.send_keys(row[5])
              sleep 2

              @driver.find_element(:id => ‘search_submit_btn’).click
              sleep 2
              end
              i = i + 1
              end

              @driver.switch_to.alert.accept
              sleep 4
              p “———————Click on show history——————–”
              # @driver.find_element(:link_text, “Show History”).click
              @driver.switch_to_window(driver.window_handles[1])
              @driver.close()
              @driver.switch_to_window(driver.window_handles[0])
              p “————–closing of show history popup—–”
              end

              end

              Can you please tell me how to send the data to code.rb page to display in report

              Thanks

  2. Hi Raveendran,

    I am facing an issue with the execution of the script on Chrome. Even after including the ChromeDriver in the path of the system, on firing the below code it is not starting the chrome browser.

    require ‘rubygems’
    require ‘watir-webdriver’
    browser = Watir::Browser.new :chrome
    browser.goto(“http://google.com”)

    1. Hi Shartul,

      Please download the latest Chrome Driver and upgrade your selenium-webdriver library.

      1. >gem update selenium-webdriver
      2.>gem update watir-webdriver
      3. Downlaod lates Chrome Driver — it will solve ur problem

  3. Hey I know this is off topic but I was wondering if you knew of any widgets I
    could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was
    hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward
    to your new updates.

  4. Hi raveendran , thanks a lot for this useful reporter ,, I’m implementing my ruby project for testing .. it has a lots of ruby files ,, how can a start this reporter at the beginning of the test and end it at the end ,, and user the reporter_row as an assertion for every steo .. please advise

    thanks again for this work

    1. Dear Duaa,

      Beginning of the script pls include the below code to create report

      create_report
      insert_head_title(“Raveendran — Sample HTML Report”)
      insert_reportname_date(“My Test Report”,$result_date )
      start_table

      IN CODE –> use the below code based on your test case

      report_row(“Check Google Home Page”,”Check The Title in Google Home Page”,”IE”,”PASS”,”Title Present”)
      report_row(“Check Google Home Page”,”Check The Title in Google Home Page”,”FF”,”FAIL”,”Title Not Present”)

      After the CODE –> Use this to complete the report structure
      close_table
      summary_report(2,1,1)

  5. Thanks Ravi for sharing such a fabulous reporting code which makes my work easier. I have modified your code to suit for my framework .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s