# author - Steven Gordon, starting from test_logger1 in the download.

#includes
require 'watir'
include Watir

#test::unit includes
require 'test/unit' 
require 'test/unit/ui/console/testrunner'

#logger includes
require 'example_logger1'

class TC_google_logging < Test::Unit::TestCase

   @@firstTime = true

   def start
     if @@firstTime == true
	   @@firstTime = false
	   #open the IE browser
	    $ie = IE.new
	    filePrefix = "My_test_logger2"
	   #create a logger 
	    $logger = LoggerFactory.start_xml_logger(filePrefix) 
	    $ie.set_logger($logger)
    end
  end

 def a_simplesearch( test_name, test_site, search_string, button_identifier_type, button_identifier, expected_string)
   start #fires up the IE browser and a logger object

   $logger.log("")
   $logger.log("## Beginning of test: " + test_name + ":") #logs only to corelogger file 
   $logger.log("Step 1: Go to the site: " + test_site)
   $ie.goto(test_site)
   $logger.log(" Action: entered " + test_site + " in the address bar.")

   $logger.log("Step 2: Enter '" + search_string + "' in the search text field")
   $ie.text_field(:name, "q").set(search_string)
   $logger.log("  Action: entered " + search_string + " in the search field")

   $logger.log("Step 3: Click the 'Search' button")
   if button_identifier_type == 'name'
	$ie.button(:name,  button_identifier).click
   else # if button_identifier_type == 'id'
	$ie.button(:id, button_identifier).click
   end
   $logger.log("  Action: clicked the Search button.")

   $logger.log("Expected Result: ")
   $logger.log(" - A Google page with results should be shown. '" + expected_string +"' should be high on the list.")

   begin
        assert($ie.contains_text(expected_string) )
        $logger.log("Passed. Found test string '" + expected_string +"' ")
        $logger.log_results(test_name, search_string, expected_string, "TEST PASSED.") #logs to both the XML file and corelogger
   rescue => e
        $logger.log("*FAILED*. Did not find test string '" + expected_string +"' ")
        $logger.log_results(test_name, search_string, expected_string, "TEST FAILED.")  #logs to both the XML file and corelogger    
   end

   $logger.log "## End of test: " + test_name + ".\n"
  
 end # end of a_simplesearch
 
 def test_a_simplesearch1
  
  a_simplesearch('simplesearch1', 'http://www.google.com', 'pickaxe', 'name', 'btnG', 'Programming Ruby')
  
  end # end of test_a_simplesearch1
 
 def test_a_simplesearch2

  a_simplesearch('simplesearch2', 'http://search.msn.com', 'pickaxe', 'id', 'srch_btn', 'Programming Ruby')
  
 end # end of test_a_simplesearch2
 
 def test_a_simplesearch3

  a_simplesearch('simplesearch3', 'http://www.google.com', 'pickax', 'name', 'btnG', 'Programming Ruby')
  
 end # end of test_a_simplesearch3
 
 def test_a_simplesearch4

  a_simplesearch('simplesearch4', 'http://search.msn.com', 'pickax', 'id', 'srch_btn', 'Programming Ruby')
  
 end # end of test_a_simplesearch4
 
 def test_b_googlenews

   #variables
   test_site = 'http://news.google.com'

   $logger.log("## Beginning of test: Google News")
  
   $logger.log("Step 1: go to the Google news site: news.google.com")
   $ie.goto(test_site)
   $logger.log("  Action: entered " + test_site + " in the address bar.")

   $logger.log("Step 2: Select Canada from the Top Stories drop-down list")
   $ie.select_list( :index , 1).select("Canada English")
   $logger.log("  Action: selected Canada from the drop-down list.")

   $logger.log("Step 3: click the 'Go' button")
   $ie.button(:caption, "Go").click
   $logger.log("  Action: clicked the Go button.")

   $logger.log("Expected Result: ")
   $logger.log(" - The Google News Canada site should be displayed")
  
    begin
       assert($ie.contains_text("Canada") )
       $logger.log("TEST PASSED. Found test string 'Canada' ")
        $logger.log_results("test_b_googlenews", "Canada English", "Canada", "TEST PASSED.") #logs to both the XML file and corelogger
   rescue => e
        $logger.log("TEST FAILED." + e.message + "\n" + e.backtrace.join("\n"))
        $logger.log_results("test_b_googlenews", "Canada English", "Canada", "TEST FAILED.")  #logs to both the XML file and corelogger    
   end
   
   $logger.log '## End of test: Google news selection'

   $logger.end_log  #close XML log file
  
 end # end of test_googlenews
 

  
end  #end of class TC_google_logging


