rwebunit 0.9.4 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,99 +1,96 @@
1
- #***********************************************************
2
- #* Copyright (c) 2006, Zhimin Zhan.
3
- #* Distributed open-source, see full license in MIT-LICENSE
4
- #***********************************************************
5
- require File.join(File.dirname(__FILE__), 'assert')
6
- require File.join(File.dirname(__FILE__), 'driver')
7
- require 'fileutils'
8
-
9
- module RWebUnit
10
-
11
- # WebPage (children of AbstractWebPage) encapsulates a real web page.
12
- # For example,
13
- # beginAt("/home")
14
- # @browser.clickLinkWithText("/login")
15
- # @browser.setFormElement("username", "sa")
16
- # Can be rewritten to
17
- # begin_at("/home")
18
- # home_page = HomePage.new
19
- # login_page = home_page.clickLoginLink
20
- # login_page.enterUserName("sa")
21
- #
22
- # So you only need change in LoingPage class if UI changes, which happen quite often.
23
- class AbstractWebPage
24
-
25
- include RWebUnit::Assert
26
- include RWebUnit::Driver
27
-
28
- # browser: passed to do assertion within the page
29
- # page_text: text used to identify the page, title will be the first candidate
30
- attr_accessor :browser, :page_text
31
-
32
- def initialize(web_tester, page_text=nil)
33
- @web_tester = web_tester
34
- @page_text = page_text
35
- begin
36
- snapshot if ENV['ITEST_DUMP_PAGE'] == 'true'
37
- delay = ENV['ITEST_PAGE_DELAY'].to_i
38
- sleep(delay)
39
- rescue => e
40
- end
41
- assert_on_page
42
- end
43
-
44
- def browser
45
- @web_tester
46
- end
47
-
48
- def assert_on_page()
49
- assert_text_present(@page_text) if @page_text
50
- end
51
-
52
- def assert_not_on_page()
53
- assert_text_not_present(@page_text) if @page_text
54
- end
55
-
56
- def dump(stream = nil)
57
- @web_tester.dump_response(stream)
58
- end
59
-
60
- def source
61
- @web_tester.page_source
62
- end
63
-
64
- def title
65
- @web_tester.page_title
66
- end
67
-
68
- def expect_page(page_clazz)
69
- page_clazz.new(@web_tester)
70
- end
71
-
72
- # TO validate
73
- def contains?(ary)
74
- page_source = source
75
- found = false
76
- ary.each do |str|
77
- found ||= page_source.include?(str)
78
- end
79
- return found
80
- end
81
- alias include contains?
82
-
83
-
84
- def snapshot
85
- if ENV['ITEST_DUMP_DIR']
86
- spec_run_id = ENV['ITEST_RUNNING_SPEC_ID'] || "unknown"
87
- spec_run_dir_name = spec_run_id.to_s.rjust(4, "0") unless spec_run_id == "unknown"
88
- spec_run_dir = File.join(ENV['ITEST_DUMP_DIR'], spec_run_dir_name)
89
- Dir.mkdir(spec_run_dir) unless File.exists?(spec_run_dir)
90
- file_name = Time.now.strftime("%m%d%H%M%S") + "_" + self.class.name.gsub("", "") + ".html"
91
- file = File.join(ENV['ITEST_DUMP_DIR'], spec_run_dir_name, file_name)
92
- page_source = browser.page_source
93
- File.new(file, "w").puts source
94
- end
95
- end
96
-
97
- end
98
-
99
- end
1
+ #***********************************************************
2
+ #* Copyright (c) 2006, Zhimin Zhan.
3
+ #* Distributed open-source, see full license in MIT-LICENSE
4
+ #***********************************************************
5
+ require File.join(File.dirname(__FILE__), 'assert')
6
+ require File.join(File.dirname(__FILE__), 'driver')
7
+ require 'fileutils'
8
+
9
+ module RWebUnit
10
+
11
+ # WebPage (children of AbstractWebPage) encapsulates a real web page.
12
+ # For example,
13
+ # beginAt("/home")
14
+ # @web_browser.clickLinkWithText("/login")
15
+ # @web_browser.setFormElement("username", "sa")
16
+ # Can be rewritten to
17
+ # begin_at("/home")
18
+ # home_page = HomePage.new
19
+ # login_page = home_page.clickLoginLink
20
+ # login_page.enterUserName("sa")
21
+ #
22
+ # So you only need change in LoingPage class if UI changes, which happen quite often.
23
+ class AbstractWebPage
24
+
25
+ include RWebUnit::Assert
26
+ include RWebUnit::Driver
27
+
28
+ # browser: passed to do assertion within the page
29
+ # page_text: text used to identify the page, title will be the first candidate
30
+ attr_accessor :page_text
31
+
32
+ def initialize(the_browser, page_text=nil)
33
+ @web_browser = the_browser
34
+ @web_tester = the_browser
35
+ @page_text = page_text
36
+ begin
37
+ snapshot if $ITEST2_DUMP_PAGE
38
+ delay = $ITEST2_PAGE_DELAY
39
+ sleep(delay)
40
+ rescue => e
41
+ end
42
+ assert_on_page
43
+ end
44
+
45
+ def browser
46
+ @web_browser
47
+ end
48
+
49
+ def assert_on_page()
50
+ assert_text_present(@page_text) if @page_text
51
+ end
52
+
53
+ def assert_not_on_page()
54
+ assert_text_not_present(@page_text) if @page_text
55
+ end
56
+
57
+ def dump(stream = nil)
58
+ @web_browser.dump_response(stream)
59
+ end
60
+
61
+ def source
62
+ @web_browser.page_source
63
+ end
64
+
65
+ def title
66
+ @web_browser.page_title
67
+ end
68
+
69
+ # TO validate
70
+ def contains?(ary)
71
+ page_source = source
72
+ found = false
73
+ ary.each do |str|
74
+ found ||= page_source.include?(str)
75
+ end
76
+ return found
77
+ end
78
+ alias include contains?
79
+
80
+
81
+ def snapshot
82
+ if $ITEST2_DUMP_DIR
83
+ spec_run_id = $ITEST2_RUNNING_SPEC_ID || "unknown"
84
+ spec_run_dir_name = spec_run_id.to_s.rjust(4, "0") unless spec_run_id == "unknown"
85
+ spec_run_dir = File.join($ITEST2_DUMP_DIR, spec_run_dir_name)
86
+ Dir.mkdir(spec_run_dir) unless File.exists?(spec_run_dir)
87
+ file_name = Time.now.strftime("%m%d%H%M%S") + "_" + self.class.name.gsub("", "") + ".html"
88
+ file = File.join($ITEST2_DUMP_DIR, spec_run_dir_name, file_name)
89
+ page_source = browser.page_source
90
+ File.new(file, "w").puts source
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ end
@@ -1,36 +1,36 @@
1
- #***********************************************************
2
- #* Copyright (c) 2006, Zhimin Zhan.
3
- #* Distributed open-source, see full license in MIT-LICENSE
4
- #***********************************************************
5
-
6
- require 'test/unit'
7
- require File.join(File.dirname(__FILE__), 'assert')
8
- require File.join(File.dirname(__FILE__), 'driver')
9
-
10
- module RWebUnit
11
-
12
- class WebTestCase < Test::Unit::TestCase
13
- include RWebUnit::Driver
14
- include RWebUnit::Assert
15
- include RWebUnit::Utils
16
-
17
- attr_reader :web_tester
18
-
19
- def initialize(name=nil)
20
- super(name) if name
21
- @web_tester = WebTester.new
22
- end
23
-
24
- def default_test
25
- super unless (self.class == WebTestCase)
26
- end
27
-
28
- def open_browser(baseUrl, relativeUrl)
29
- context.base_url = baseUrl
30
- begin_at(relativeUrl)
31
- end
32
- alias open_ie open_browser
33
-
34
- end
35
-
36
- end
1
+ #***********************************************************
2
+ #* Copyright (c) 2006, Zhimin Zhan.
3
+ #* Distributed open-source, see full license in MIT-LICENSE
4
+ #***********************************************************
5
+
6
+ require 'test/unit'
7
+ require File.join(File.dirname(__FILE__), 'assert')
8
+ require File.join(File.dirname(__FILE__), 'driver')
9
+
10
+ module RWebUnit
11
+
12
+ class WebTestCase < Test::Unit::TestCase
13
+ include RWebUnit::Driver
14
+ include RWebUnit::Assert
15
+ include RWebUnit::Utils
16
+
17
+ attr_reader :web_tester
18
+
19
+ def initialize(name=nil)
20
+ super(name) if name
21
+ @web_browser = WebBrowser.new
22
+ end
23
+
24
+ def default_test
25
+ super unless (self.class == WebTestCase)
26
+ end
27
+
28
+ def open_browser(baseUrl, relativeUrl)
29
+ context.base_url = baseUrl
30
+ begin_at(relativeUrl)
31
+ end
32
+ alias open_ie open_browser
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,65 @@
1
+
2
+ module FireWatir
3
+ class Firefox
4
+
5
+ @@firefox_started = false
6
+
7
+ def initialize(options = {})
8
+ if(options.kind_of?(Integer))
9
+ options = {:waitTime => options}
10
+ end
11
+
12
+ if(options[:profile])
13
+ profile_opt = "-no-remote -P #{options[:profile]}"
14
+ else
15
+ profile_opt = ""
16
+ end
17
+
18
+ waitTime = options[:waitTime] || 2
19
+
20
+ case RUBY_PLATFORM
21
+ when /mswin/
22
+ # Get the path to Firefox.exe using Registry.
23
+ require 'win32/registry.rb'
24
+ path_to_bin = ""
25
+ Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Mozilla\Mozilla Firefox') do |reg|
26
+ keys = reg.keys
27
+ reg1 = Win32::Registry::HKEY_LOCAL_MACHINE.open("SOFTWARE\\Mozilla\\Mozilla Firefox\\#{keys[0]}\\Main")
28
+ reg1.each do |subkey, type, data|
29
+ if(subkey =~ /pathtoexe/i)
30
+ path_to_bin = data
31
+ end
32
+ end
33
+ end
34
+
35
+ when /linux/i
36
+ path_to_bin = `which firefox`.strip
37
+ when /darwin/i
38
+ path_to_bin = '/Applications/Firefox.app/Contents/MacOS/firefox'
39
+ when /java/
40
+ raise "Not implemented: Create a browser finder in JRuby"
41
+ end
42
+
43
+ @t = Thread.new { system("#{path_to_bin} -jssh #{profile_opt}")} unless @@firefox_started
44
+
45
+ sleep waitTime
46
+ begin
47
+ set_defaults()
48
+ rescue Watir::Exception::UnableToStartJSShException
49
+ if !@t # no new thread starting browser, try again
50
+ puts "Firefox with JSSH not detected after you indicated @@firefox_started"
51
+ @t = Thread.new { system("#{path_to_bin} -jssh #{profile_opt}")}
52
+ sleep waitTime
53
+ set_defaults
54
+ end
55
+ end
56
+ get_window_number()
57
+ set_browser_document()
58
+ end
59
+
60
+ def self.firefox_started=(value)
61
+ @@firefox_started = value
62
+ end
63
+
64
+ end
65
+ end
data/test/mock_page.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rwebunit'
2
+
3
+ class MockPage < RWebUnit::AbstractWebPage
4
+
5
+ def initialize(browser)
6
+ super(browser, "")
7
+ end
8
+ end
data/test/setup.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'firewatir'
2
+
3
+ $loaded = false
4
+ unless $loaded
5
+ sample_html = "file://" + File.expand_path(File.dirname(__FILE__) + "/test.html")
6
+ $firefox = FireWatir::Firefox.new
7
+ $firefox.goto(sample_html)
8
+ $web_browser = RWebUnit::WebBrowser.new("test", $firefox, :firefox => true)
9
+ $loaded = true
10
+ end
data/test/test.html ADDED
@@ -0,0 +1,129 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
2
+ <html>
3
+ <head>
4
+ <title>
5
+ For Testing Recorder
6
+ </title>
7
+ <style type="text/css">
8
+ body {
9
+ margin: 5px;
10
+ padding: 0;
11
+ font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Helvetica, Arial, sans-serif;
12
+ font-size: small;
13
+ background: #f4f4ed;
14
+ }
15
+
16
+ h1, h2, h3, h4, h5, h6 {
17
+ margin-top: 0;
18
+ color: black;
19
+ font-family: Georgia, "Times New Roman", Times, serif;
20
+ font-weight: normal
21
+ }
22
+
23
+ a:link {
24
+ color: #005f96
25
+ }
26
+
27
+ a:visited {
28
+ color: #484848
29
+ }
30
+
31
+ a:hover, a:active {
32
+ color: #c30
33
+ }
34
+
35
+ td {
36
+ font-size: small;
37
+ }
38
+ </style>
39
+ </head>
40
+ <body>
41
+ <form action="test">
42
+ <h1>
43
+ iTest2/Watir Recorder Test Page
44
+ </h1>
45
+ <h2>
46
+ Links
47
+ </h2>
48
+ <p>
49
+ <a href="http://www.itest2.com">iTest2</a> &nbsp; <a id="link_itest2" href="http://www.itest2.com" name="link_itest2">iTest2 with Id</a>
50
+ </p>
51
+ <h2>
52
+ Checkboxes
53
+ </h2>
54
+ <p>
55
+ <input name="checkbox1" type="checkbox"> No Value &nbsp; <input name="checkbox2" type="checkbox" value="true"> With value true &nbsp; <input id="checkbox3" type="checkbox"> Id but no name
56
+ </p>
57
+ <h2>
58
+ Buttons
59
+ </h2>
60
+ <p>
61
+ <input type="button" id="login-btn" value="Login"> &nbsp; <input type="button" value="Login"> &nbsp; <input type="submit" value="Submit without id">
62
+ </p>
63
+ <h2>
64
+ Radio buttons
65
+ </h2>
66
+ <p>
67
+ <input type="radio" checked="true" name="os" value="Apple"> Apple &nbsp; <input type="radio" name="os" value="Linux"> Linux
68
+ </p>
69
+ <h2>
70
+ Text fields
71
+ </h2>
72
+ <table>
73
+ <tr>
74
+ <td>
75
+ Text (single line):
76
+ </td>
77
+ <td>
78
+ <input type="text" name="text1" value="text already there"> <i>Right click to verify textfield</i>
79
+ </td>
80
+ </tr>
81
+ <tr>
82
+ <td>
83
+ Text (with name, just id):
84
+ </td>
85
+ <td>
86
+ <input type="text" id="text_without_id" value=""> <i>Right click to verify textfield</i>
87
+ </td>
88
+ </tr>
89
+ <tr>
90
+ <td>
91
+ Password:
92
+ </td>
93
+ <td>
94
+ <input type="password" name="password1">
95
+ </td>
96
+ </tr>
97
+ <tr>
98
+ <td valign="top">
99
+ TextArea:
100
+ </td>
101
+ <td>
102
+ <textarea name="textarea" cols="10" rows="2">
103
+ </textarea>
104
+ </td>
105
+ </tr>
106
+ </table>
107
+ <h2>
108
+ Select (combobox)
109
+ </h2>
110
+ <p>
111
+ By Name: <select name="testing_tool">
112
+ <option value="iTest2">
113
+ iTest2 display
114
+ </option>
115
+ <option value="Watir">
116
+ Watir
117
+ </option>
118
+ </select> &nbsp; &nbsp; By Id: <select id="methodology">
119
+ <option value="XP">
120
+ Agile way
121
+ </option>
122
+ <option value="Waterfall">
123
+ Water way
124
+ </option>
125
+ </select>
126
+ </p>
127
+ </form>
128
+ </body>
129
+ </html>