#!/usr/bin/ruby require "runit/testcase" require "runit/cui/testrunner" require "rubygems" require 'hpricot' require "youtube/searchresultscraper" class SearchResultScraperTest < RUNIT::TestCase def test_scrape # #正常系 # #検索結果がある open_and_scrape("http://www.youtube.com/results?search_query=", "doraemon", 2) #検索結果がない open_and_scrape("http://www.youtube.com/results?search_query=", "aeudyr jahafudfhadf ahf", 2) # #異常系 # #検索結果があるが、Not Foundがある場合 begin open_local_file_and_scrape("html/dataY_noMsgY.htm") assert_fail("おこるべきエラーが発生していない") rescue RuntimeError =>e #puts e end #検索結果がなく、Not Foundもない場合 begin open_local_file_and_scrape("html/dataN_noMsgN.htm") assert_fail("おこるべきエラーが発生していない") rescue RuntimeError #puts e end #一部の項目だけ取得できていない場合 begin open_local_file_and_scrape("html/scraping_error.html") assert_fail("おこるべきエラーが発生していない") rescue RuntimeError => e # puts e end end def test_scrape_video_count scraper = open_and_scrape("http://www.youtube.com/results?search_query=", "doraemon", 2) puts scraper.video_count assert( scraper.video_count > 0 ) scraper = open_and_scrape("http://www.youtube.com/results?search_query=", "doraemonifdadfa", 2) puts scraper.video_count assert( scraper.video_count == 0 ) end def open_and_scrape url, keyword=nil, page=nil scraper = MySearchResultScraper.new(url, keyword, page) scraper.open scraper.scrape scraper end def open_local_file_and_scrape url scraper = MySearchResultScraper.new(url) scraper.open_local_file scraper.scrape end end class MySearchResultScraper < Youtube::SearchResultScraper @@youtube_search_base_url = "http://www.youtube.com/results?search_query=" def initialize url, keyword=nil, page=nil @@youtube_search_base_url = url @keyword = keyword @page = page if not page == nil end def open_local_file @html = Kernel.open(@@youtube_search_base_url).read replace_document_write_javascript @search_result = Hpricot.parse(@html) end end