rwebunit 0.2.0 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,85 +2,77 @@
2
2
  #* Copyright (c) 2006, Zhimin Zhan.
3
3
  #* Distributed open-source, see full license in MIT-LICENSE
4
4
  #***********************************************************
5
+ require File.join(File.dirname(__FILE__), 'assert')
6
+ require File.join(File.dirname(__FILE__), 'driver')
5
7
 
6
8
  module RWebUnit
7
9
 
8
- # WebPage (children of AbstractWebPage) encapsulates a real web page.
9
- # For example,
10
- # beginAt("/home")
11
- # @browser.clickLinkWithText("/login")
12
- # @browser.setFormElement("username", "sa")
13
- # Can be rewritten to
14
- # beginAt("/home")
15
- # home_page = HomePage.new
16
- # login_page = home_page.clickLoginLink
17
- # login_page.enterUserName("sa")
18
- #
19
- # So you only need change in LoingPage class if UI changes, which happen quite often.
20
- class AbstractWebPage
10
+ # WebPage (children of AbstractWebPage) encapsulates a real web page.
11
+ # For example,
12
+ # beginAt("/home")
13
+ # @browser.clickLinkWithText("/login")
14
+ # @browser.setFormElement("username", "sa")
15
+ # Can be rewritten to
16
+ # begin_at("/home")
17
+ # home_page = HomePage.new
18
+ # login_page = home_page.clickLoginLink
19
+ # login_page.enterUserName("sa")
20
+ #
21
+ # So you only need change in LoingPage class if UI changes, which happen quite often.
22
+ class AbstractWebPage
21
23
 
22
- # browser: passed to do assertion within the page
23
- # page_text: text used to identify the page, title will be the first candidate
24
- attr_accessor :browser, :page_text
24
+ include RWebUnit::Assert
25
+ include RWebUnit::Driver
26
+
27
+ # browser: passed to do assertion within the page
28
+ # page_text: text used to identify the page, title will be the first candidate
29
+ attr_accessor :browser, :page_text
25
30
 
26
- def initialize(ie, page_text=nil)
27
- @browser = ie
28
- @page_text = page_text
29
- assertOnPage
30
- end
31
+ def initialize(web_tester, page_text=nil)
32
+ @web_tester = web_tester
33
+ @page_text = page_text
34
+ assert_on_page
35
+ end
31
36
 
32
- def assertTextPresent(text)
33
- @browser.assertTextPresent(text)
34
- end
37
+ def browser
38
+ @web_tester
39
+ end
35
40
 
36
- def assertTextInElement(elementID, text)
37
- @browser.assertTextInElement(elementID, text)
38
- end
41
+ def assert_on_page()
42
+ assert_text_present(@page_text) if @page_text
43
+ end
39
44
 
40
- def assertOnPage()
41
- assertTextPresent(@page_text) if @page_text
42
- end
45
+ def assert_not_on_page()
46
+ assert_text_not_present(@page_text) if @page_text
47
+ end
43
48
 
44
- def assertNotOnPage()
45
- assertTextNotPresent(@page_text) if @page_text
46
- end
49
+ def dump(stream = nil)
50
+ @web_tester.dump_response(stream)
51
+ end
47
52
 
48
- def submit
49
- @browser.submit
50
- end
53
+ def source
54
+ @web_tester.page_source
55
+ end
51
56
 
52
- def getElementValue(elementId)
53
- @browser.getElemenValue(elementId)
54
- end
57
+ def title
58
+ @web_tester.page_title
59
+ end
60
+
61
+ def expect_page(page_clazz)
62
+ page_clazz.new(@web_tester)
63
+ end
55
64
 
56
- def dumpResponse(stream = nil)
57
- @browser.dumpResponse(stream)
65
+ # TO validate
66
+ def contains?(ary)
67
+ page_source = source
68
+ found = false
69
+ ary.each do |str|
70
+ found ||= page_source.include?(str)
58
71
  end
72
+ return found
73
+ end
74
+ alias include contains?
59
75
 
60
- # Wait for specific seconds for an Ajax update finish.
61
- # Trick: In your Rails application,
62
- # :loading => "Element.show('search_indicator');
63
- # :complete => "Element.hide('search_indicator');
64
- #
65
- # <%= image_tag("indicator.gif", :id => 'search_indicator', :style => 'display:none') %>
66
- #
67
- # In your test case:
68
- # wait_ajax_update("search_indicator", "30)
69
- #
70
- # Warning: this method has not been fully tested, if you are not using Rails, change your parameter accordingly.
71
- #
72
- def wait_ajax_update(element_id, seconds)
73
- count = 0
74
- while count < (seconds / 2) do
75
- search_indicator = @browser.getElementById(element_id)
76
- search_indicator_outer_html = search_indicator.outerHtml if search_indicator
77
- return true if search_indicator_outer_html && search_indicator_outer_html.include?('style="DISPLAY: none"')
78
- sleep 2;
79
- count = count + 1
80
- end
81
- return false
82
- end
83
-
84
- end
76
+ end
85
77
 
86
78
  end
@@ -4,214 +4,33 @@
4
4
  #***********************************************************
5
5
 
6
6
  require 'test/unit'
7
+ require File.join(File.dirname(__FILE__), 'assert')
8
+ require File.join(File.dirname(__FILE__), 'driver')
7
9
 
8
10
  module RWebUnit
9
11
 
10
- class WebTestCase < Test::Unit::TestCase
11
- include RWebUnit::Utils
12
+ class WebTestCase < Test::Unit::TestCase
13
+ include RWebUnit::Driver
14
+ include RWebUnit::Assert
15
+ include RWebUnit::Utils
12
16
 
13
- attr_reader :web_tester
17
+ attr_reader :web_tester
14
18
 
15
- def initialize(name=nil)
16
- super(name) if name
17
- @web_tester = WebTester.new
18
- end
19
+ def initialize(name=nil)
20
+ super(name) if name
21
+ @web_tester = WebTester.new
22
+ end
19
23
 
20
- def default_test
21
- super unless(self.class == WebTestCase)
22
- end
24
+ def default_test
25
+ super unless (self.class == WebTestCase)
26
+ end
23
27
 
24
- def getTestContext
25
- @web_tester.test_context
26
- end
28
+ def open_browser(baseUrl, relativeUrl)
29
+ test_context.base_url = baseUrl
30
+ begin_at(relativeUrl)
31
+ end
32
+ alias open_ie open_browser
27
33
 
28
- def beginAt(url)
29
- @web_tester.beginAt(url)
30
- end
31
-
32
- def openBrowser(baseUrl, relativeUrl)
33
- getTestContext().base_url = baseUrl
34
- beginAt(relativeUrl)
35
- end
36
-
37
- def closeBrowser
38
- @web_tester.closeBrowser
39
- end
40
-
41
- def goBack()
42
- @web_tester.goBack
43
- end
44
-
45
- def goForward()
46
- @web_tester.goForward
47
- end
48
-
49
- def gotoPage(page)
50
- @web_tester.gotoPage(page)
51
- end
52
-
53
- # assertions
54
- def assertTitleEquals(title)
55
- @web_tester.assertTitleEquals(title)
56
- end
57
-
58
- def assertTextPresent(text)
59
- @web_tester.assertTextPresent(text)
60
- end
61
-
62
- def assertTextNotPresent(text)
63
- @web_tester.assertTextNotPresent(text)
64
- end
65
-
66
- def assertTextInTable(tableId, text)
67
- @web_tester.assertTextInTable(tableId, text)
68
- end
69
-
70
- def assertTextNotInTable(tableId, text)
71
- @web_tester.assertTextNotInTable(tableId, text)
72
- end
73
-
74
- # textfields
75
- def setFormElement(elementName, elementValue)
76
- @web_tester.setFormElement(elementName, elementValue)
77
- end
78
-
79
- def assertTextPresentInTextField(textfieldName, text, msg = nil)
80
- @web_tester.assertTextPresentInTextField(textfieldName, text, msg)
81
- end
82
-
83
- #links
84
- def clickLinkWithText(linkText)
85
- @web_tester.clickLinkWithText(linkText)
86
- end
87
-
88
- def assertLinkPresentWithText(linkText)
89
- @web_tester.assertLinkPresentWithText(linkText)
90
- end
91
-
92
- def assertLinkNotPresentWithText(linkText)
93
- @web_tester.assertLinkNotPresentWithText(linkText)
94
- end
95
-
96
- ##
97
- # buttons
98
-
99
- # submit the form using the first (index) submit button
100
- def submit()
101
- @web_tester.submit()
102
- end
103
-
104
- def submit(buttonId)
105
- @web_tester.submit(buttonId)
106
- end
107
-
108
- def clickButton(buttonId)
109
- @web_tester.clickButton(buttonId)
110
- end
111
-
112
- def clickButtonWithCaption(caption)
113
- @web_tester.clickButtonWithCaption(caption)
114
- end
115
-
116
- def clickButtonWithValue(value)
117
- @web_tester.clickButtonWithValue(value)
118
- end
119
-
120
- def assertButtonNotPresent(buttonID)
121
- @web_tester.assertButtonNotPresent(buttonID)
122
- end
123
-
124
- def assertButtonNotPresentWithText(text)
125
- @web_tester.assertButtonNotPresentWithText(text)
126
- end
127
-
128
- def assertButtonPresent(buttonID)
129
- @web_tester.assertButtonPresent(buttonID)
130
- end
131
-
132
- def assertButtonPresentWithText(buttonID)
133
- @web_tester.assertButtonPresentWithText(buttonID)
134
- end
135
-
136
- # checkbox
137
- def checkCheckbox(checkBoxName)
138
- @web_tester.checkCheckbox(checkBoxName)
139
- end
140
-
141
- def uncheckCheckbox(checkBoxName)
142
- @web_tester.uncheckCheckbox(checkBoxName)
143
- end
144
-
145
- def assertCheckboxNotSelected(checkBoxName)
146
- @web_tester.assertCheckboxNotSelected(checkBoxName)
147
- end
148
-
149
- def assertCheckboxSelected(checkBoxName)
150
- @web_tester.assertCheckboxSelected(checkBoxName)
151
- end
152
-
153
- # radio button
154
- def assertRadioOptionNotPresent(radioGroup, radioOption)
155
- @web_tester.assertRadioOptionNotPresent(radioGroup, radioOption)
156
- end
157
-
158
- def assertRadioOptionPresent(radioGroup, radioOption)
159
- @web_tester.assertRadioOptionPresent(radioGroup, radioOption)
160
- end
161
-
162
- def assertRadioOptionSelected(radioGroup, radioOption)
163
- @web_tester.assertRadioOptionSelected(radioGroup, radioOption)
164
- end
165
-
166
- def assertRadioOptionNotSelected(radioGroup, radioOption)
167
- @web_tester.assertRadioOptionNotSelected(radioGroup, radioOption)
168
- end
169
-
170
- # combo box
171
- def selectOption(selectName, option)
172
- @web_tester.selectOption(selectName, option)
173
- end
174
-
175
- # dhtml related
176
- def assertElementPresent(elementID)
177
- @web_tester.assertElementPresent(elementID)
178
- end
179
-
180
- def assertElementNotPresent(elementID)
181
- @web_tester.assertElementNotPresent(elementID)
182
- end
183
-
184
- # Wait for specific seconds for an Ajax update finish.
185
- # Trick: In your Rails application,
186
- # :loading => "Element.show('search_indicator');
187
- # :complete => "Element.hide('search_indicator');
188
- #
189
- # <%= image_tag("indicator.gif", :id => 'search_indicator', :style => 'display:none') %>
190
- #
191
- # In your test case:
192
- # wait_ajax_update("search_indicator", "30)
193
- #
194
- # Warning: this method has not been fully tested, if you are not using Rails, change your parameter accordingly.
195
- #
196
- def wait_ajax_update(element_id, seconds)
197
- count = 0
198
- while count < (seconds / 2) do
199
- search_indicator = @web_tester.getElementById(element_id)
200
- search_indicator_outer_html = search_indicator.outerHtml if search_indicator
201
- return true if search_indicator_outer_html && search_indicator_outer_html.include?('style="DISPLAY: none"')
202
- sleep 2;
203
- count = count + 1
204
- end
205
- return false
206
- end
207
-
208
- # ---
209
- # For deubgging
210
- # ---
211
- def dumpResponse(stream = nil)
212
- @web_tester.dumpResponse(stream)
213
- end
214
-
215
- end
34
+ end
216
35
 
217
36
  end