instiki/test/watir/e2e.rb

306 lines
9.9 KiB
Ruby
Raw Normal View History

2005-05-06 04:40:52 +02:00
require 'fileutils'
require 'test/unit'
INSTIKI_ROOT = File.expand_path(File.dirname(__FILE__) + "/../..")
require(File.expand_path(File.dirname(__FILE__) + "/../../config/environment"))
2005-05-18 00:27:26 +02:00
# Use instiki/../watir, if such a directory exists; This can be a CVS HEAD.
# Otherwise Watir has to be installed in ruby/lib.
$:.unshift INSTIKI_ROOT + '/../watir' if File.exists?(INSTIKI_ROOT + '/../watir/watir.rb')
require 'watir'
2005-05-06 04:40:52 +02:00
INSTIKI_PORT = 2501
HOME = "http://localhost:#{INSTIKI_PORT}"
class E2EInstikiTest < Test::Unit::TestCase
def startup
@@instiki = InstikiController.start
2005-05-06 04:40:52 +02:00
sleep 5
@@ie = Watir::IE.start(HOME)
2005-05-06 04:40:52 +02:00
setup_web
setup_home_page
2005-05-18 05:11:33 +02:00
2005-05-06 04:40:52 +02:00
@@ie
end
2005-05-18 05:11:33 +02:00
2005-05-06 04:40:52 +02:00
def self.shutdown
@@ie.close if defined? @@ie
@@instiki.stop
2005-05-06 04:40:52 +02:00
end
def ie
if defined? @@ie
@@ie
else
startup
end
end
def setup
2005-05-18 00:27:26 +02:00
ie.goto HOME
2005-05-06 04:40:52 +02:00
ie
end
2005-05-18 00:27:26 +02:00
# Numbers like _00010_ determine the sequence in which the test cases are executed by Test::Unit
# Unfortunately, this sequence is important.
def test_00010_home_page_contents
check_main_menu
check_bottom_menu
check_footnote
end
def test_00020_add_a_page
# Add reference to a non-existant wiki page
enter_markup('HomePage', '[[Another Wiki Page]]')
2005-05-18 00:27:26 +02:00
assert_equal '?', ie.link(:url, url(:show, 'Another+Wiki+Page')).text
# Edit the first revision of a page
enter_markup('Another+Wiki+Page', 'First revision of Another Wiki Page, linked from HomePage')
2005-05-18 00:27:26 +02:00
# Check contents of the new page
assert_equal url(:show, 'Another+Wiki+Page'), ie.url
assert_match /First revision of Another Wiki Page, linked from Home Page/, ie.text
assert_match /Linked from: Home Page/, ie.text
2005-05-18 00:27:26 +02:00
# There must be three links to HomePage - main menu, contents of the page and navigation bar
links_to_homepage = ie.links.to_a.select { |link| link.text == 'Home Page' }
assert_equal 3, links_to_homepage.size
links_to_homepage.each { |link| assert_equal url(:show, 'HomePage'), link.href }
2005-05-18 02:39:26 +02:00
# Check also the "created on ... by ..." footnote
assert_match Regexp.new('Created on ' + date_pattern + ' by Anonymous Coward\?'), ie.text
2005-05-06 04:40:52 +02:00
end
def test_00030_edit_page
enter_markup('TestEditPage', 'Test Edit Page, revision 1')
assert_match /Test Edit Page, revision 1/, ie.text
# subsequent revision by the anonymous author
2005-05-18 05:11:33 +02:00
enter_markup('TestEditPage', 'Test Edit Page, revision 1, altered')
assert_match /Test Edit Page, revision 1, altered/, ie.text
assert_match Regexp.new('Created on ' + date_pattern + ' by Anonymous Coward\?'), ie.text
# revision by a named author
2005-05-18 05:11:33 +02:00
enter_markup('TestEditPage', 'Test Edit Page, revision 2', 'Author')
assert_match /Test Edit Page, revision 2/, ie.text
assert_match Regexp.new('Revised on ' + date_pattern + ' by Author\?'), ie.text
link_to_previous_revision = ie.link(:name, 'to_previous_revision')
2005-05-18 05:11:33 +02:00
assert_equal url(:revision, 'TestEditPage', 0), link_to_previous_revision.href
assert_equal 'Back in time', link_to_previous_revision.text
assert_match /Edit \| Back in time \(1 revisions\) \| See changes/, ie.text
2005-05-18 05:11:33 +02:00
# another anonymous revision
enter_markup('TestEditPage', 'Test Edit Page, revision 3')
assert_match /Test Edit Page, revision 3/, ie.text
assert_match /Edit \| Back in time \(2 revisions\) \| See changes \| Hide changes /, ie.text
end
def test_00040_traversing_revisions
ie.goto url(:revision, 'TestEditPage', 1)
assert_match /Test Edit Page, revision 2/, ie.text
assert_match(Regexp.new(
'Forward in time \(1 more\) \| Back in time \(1 more\) \| ' +
'See current \| See changes \| Hide changes \| Rollback'),
ie.text)
ie.link(:name, 'to_previous_revision').click
assert_match /Test Edit Page, revision 1, altered/, ie.text
assert_match /Forward in time \(2 more\) \| See current \| Rollback/, ie.text
ie.link(:name, 'to_next_revision').click
assert_match /Test Edit Page, revision 2/, ie.text
ie.link(:name, 'to_next_revision').click
assert_match /Test Edit Page, revision 3/, ie.text
end
def test_00050_rollback
ie.goto url(:revision, 'TestEditPage', 1)
assert_match /Test Edit Page, revision 2/, ie.text
ie.link(:name, 'rollback').click
assert_equal url(:rollback, 'TestEditPage', 1), ie.url
assert_equal 'Test Edit Page, revision 2', ie.text_field(:name, 'content').value
ie.text_field(:name, 'content').set('Test Edit Page, revision 2, rolled back')
ie.button(:value, 'Update').click
assert_equal url(:show, 'TestEditPage'), ie.url
assert_match /Test Edit Page, revision 2, rolled back/, ie.text
end
2005-05-06 04:40:52 +02:00
2005-05-29 00:54:09 +02:00
def test_0060_see_changes
ie.goto url(:show, 'TestEditPage')
assert ie.html.include?('<P>Test Edit Page, revision <DEL class=diffmod>2</DEL><INS class=diffmod>2, rolled back</INS></P>')
assert_match /Test Edit Page, revision 2, rolled back/, ie.text
ie.link(:text, 'See changes').click
assert_match /Showing changes from revision #1 to #2: Added \| Removed/, ie.text
assert_match /Test Edit Page, revision 22, rolled back/, ie.text
ie.link(:text, 'Hide changes').click
assert_match /Test Edit Page, revision 2, rolled back/, ie.text
end
def test_0070_all_pages
ie.link(:text, 'All Pages').click
page_links = ie.links.map { |l| l.text }
expected_page_links = ['Another Wiki Page', 'Home Page', 'Test Edit Page', 'Test Edit Page']
assert_equal expected_page_links, page_links[-4..-1]
# and before that, we have the tail of the main menu
assert_equal 'Export', page_links[-5]
end
def test_0070_all_pages
ie.link(:text, 'All Pages').click
page_links = ie.links.map { |l| l.text }
expected_page_links = ['Another Wiki Page', 'Home Page', 'Test Edit Page', 'Test Edit Page']
assert_equal expected_page_links, page_links[-4..-1]
assert_equal 'Export', page_links[-5]
2005-05-29 00:54:09 +02:00
end
2005-05-06 04:40:52 +02:00
private
2005-05-18 05:11:33 +02:00
2005-05-18 00:27:26 +02:00
def bp
require 'breakpoint'
breakpoint
end
2005-05-18 05:11:33 +02:00
2005-05-18 00:27:26 +02:00
def check_main_menu
assert_equal HOME + '/wiki/list', ie.link(:text, 'All Pages').href
assert_equal HOME + '/wiki/recently_revised', ie.link(:text, 'Recently Revised').href
assert_equal HOME + '/wiki/authors', ie.link(:text, 'Authors').href
assert_equal HOME + '/wiki/feeds', ie.link(:text, 'Feeds').href
assert_equal HOME + '/wiki/export', ie.link(:text, 'Export').href
end
2005-05-18 05:11:33 +02:00
2005-05-18 00:27:26 +02:00
def check_bottom_menu
assert_equal url(:edit, 'HomePage'), ie.link(:text, 'Edit Page').href
assert_equal HOME + '/wiki/edit_web', ie.link(:text, 'Edit Web').href
assert_equal url(:print, 'HomePage'), ie.link(:text, 'Print').href
2005-05-18 00:27:26 +02:00
end
2005-05-18 05:11:33 +02:00
2005-05-18 00:27:26 +02:00
def check_footnote
2005-05-18 02:39:26 +02:00
assert_match /This site is running on Instiki/, ie.text
2005-05-18 00:27:26 +02:00
assert_equal 'http://instiki.org/', ie.link(:text, 'Instiki').href
2005-05-18 02:39:26 +02:00
assert_match /Powered by Ruby on Rails/, ie.text
2005-05-18 00:27:26 +02:00
assert_equal 'http://rubyonrails.com/', ie.link(:text, 'Ruby on Rails').href
end
def date_pattern
'(January|February|March|April|May|June|July|August|September|October|November|December) ' +
'\d\d?, \d\d\d\d \d\d:\d\d'
end
2005-05-18 05:11:33 +02:00
def enter_markup(page, content, author = nil)
ie.goto url(:show, page)
if ie.url == url(:show, page)
ie.link(:name, 'edit').click
assert_equal url(:edit, page), ie.url
else
assert_equal url(:new, page), ie.url
end
ie.text_field(:name, 'content').set(content)
ie.text_field(:name, 'author').set(author || '')
ie.button(:value, 'Submit').click
assert_equal url(:show, page), ie.url
end
2005-05-18 05:11:33 +02:00
2005-05-06 04:40:52 +02:00
def setup_web
assert_equal 'Wiki', ie.textField(:name, 'web_name').value
assert_equal 'wiki', ie.textField(:name, 'web_address').value
assert_equal '', ie.textField(:name, 'password').value
assert_equal '', ie.textField(:name, 'password_check').value
ie.textField(:name, 'password').set('123')
ie.textField(:name, 'password_check').set('123')
ie.button(:value, 'Setup').click
2005-05-18 00:27:26 +02:00
assert_equal url(:new, 'HomePage'), ie.url
2005-05-06 04:40:52 +02:00
end
def setup_home_page
ie.textField(:name, 'content').set('Homepage of a test wiki')
ie.button(:value, 'Submit').click
2005-05-18 00:27:26 +02:00
assert_equal url(:show, 'HomePage'), ie.url
2005-05-06 04:40:52 +02:00
end
2005-05-18 05:11:33 +02:00
def url(operation, page_name = nil, revision = nil)
2005-05-18 00:27:26 +02:00
case operation
2005-05-18 05:11:33 +02:00
when :edit, :new, :show, :print, :revision, :rollback
"#{HOME}/wiki/#{operation}/#{page_name}" + (revision ? "?rev=#{revision}" : '')
2005-05-18 00:27:26 +02:00
else
raise "Unsupported operation: '#{operation}"
end
2005-05-06 04:40:52 +02:00
end
end
class InstikiController
attr_reader :process_id
def self.start
startup_info = [68].pack('lx64')
process_info = [0, 0, 0, 0].pack('llll')
2005-05-18 05:11:33 +02:00
startup_command =
"ruby #{RAILS_ROOT}/instiki.rb --storage #{prepare_storage} " +
" --port #{INSTIKI_PORT} --environment development"
2005-05-18 05:11:33 +02:00
result = Win32API.new('kernel32.dll', 'CreateProcess', 'pplllllppp', 'L').call(
nil,
startup_command,
0, 0, 1, 0, 0, '.', startup_info, process_info)
2005-05-18 05:11:33 +02:00
# TODO print the error code, or better yet a text message
raise "Failed to start Instiki." if result == 0
process_id = process_info.unpack('llll')[2]
return self.new(process_id)
end
2005-05-18 05:11:33 +02:00
def self.prepare_storage
storage_path = INSTIKI_ROOT + '/storage/e2e'
FileUtils.rm_rf(storage_path) if File.exists? storage_path
FileUtils.mkdir_p(storage_path)
storage_path
end
2005-05-18 05:11:33 +02:00
def initialize(pid)
@process_id = pid
end
def stop
right_to_terminate_process = 1
handle = Win32API.new('kernel32.dll', 'OpenProcess', 'lil', 'l').call(
right_to_terminate_process, 0, @process_id)
Win32API.new('kernel32.dll', 'TerminateProcess', 'll', 'L').call(handle, 0)
end
2005-05-18 05:11:33 +02:00
end
2005-05-06 04:40:52 +02:00
begin
require 'test/unit/ui/console/testrunner'
Test::Unit::UI::Console::TestRunner.new(E2EInstikiTest.suite).start
rescue => e
$stderr.puts 'Unhandled error during test execution'
$stderr.puts e.message
$stderr.puts e.backtrace
ensure
begin
E2EInstikiTest::shutdown
rescue => e
$stderr.puts 'Error during shutdown'
$stderr.puts e.message
$stderr.puts e.backtrace
end
end