PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/firewatir/unittests/table_test.rb

https://github.com/chenpassion/watir
Ruby | 373 lines | 249 code | 69 blank | 55 comment | 10 complexity | 07ae598a82167b61e65190bfaf1a3f61 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # feature tests for Tables
  2. # revision: $Revision: 1.0 $
  3. $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') unless $SETUP_LOADED
  4. require 'unittests/setup'
  5. class TC_Tables < Test::Unit::TestCase
  6. def setup
  7. goto_page("table1.html")
  8. end
  9. def test_Table_Exists
  10. assert_false(browser.table(:id, 'missingTable').exists?)
  11. assert_false(browser.table(:index, 33).exists?)
  12. assert(browser.table(:id, 't1').exists?)
  13. assert(browser.table(:id, /t/).exists?)
  14. assert_false(browser.table(:id, /missing_table/).exists?)
  15. assert(browser.table(:index, 1).exists?)
  16. assert(browser.table(:index, 2).exists?)
  17. end
  18. tag_method :test_rows, :fails_on_ie
  19. def test_rows
  20. assert_raises(UnknownObjectException ){ browser.table(:id, 'missingTable').row_count }
  21. assert_raises(UnknownObjectException ){ browser.table(:index, 66).row_count }
  22. assert_raises(UnknownObjectException){ browser.table(:bad_attribute, 99).row_count }
  23. assert_equal(2, browser.table(:index, 1).row_count)
  24. assert_equal(2, browser.table(:index, 1).rows.length)
  25. assert_equal(5, browser.table(:id, 't1').row_count) # 4 rows and a header
  26. assert_equal(5, browser.table(:index, 2).row_count) # same table as above, just accessed by index
  27. assert_equal(5, browser.table(:id, 't1').rows.length)
  28. # test the each iterator on rows - ie, go through each cell
  29. row = browser.table(:index, 2)[2]
  30. count = 1
  31. row.each do |cell|
  32. if count == 1
  33. assert_equal('Row 1 Col1', cell.to_s.strip)
  34. elsif count==2
  35. assert_equal('Row 1 Col2', cell.to_s.strip)
  36. end
  37. count += 1
  38. end
  39. assert_equal(2, count -1)
  40. assert_equal(2, browser.table(:index, 2)[2].column_count)
  41. end
  42. def test_dynamic_tables
  43. t = browser.table(:id, 't1')
  44. assert_equal(5, t.row_count)
  45. browser.button(:value, 'add row').click
  46. assert_equal(6, t.row_count)
  47. end
  48. def test_columns
  49. assert_raises(UnknownObjectException ){ browser.table(:id, 'missingTable').column_count }
  50. assert_raises(UnknownObjectException ){ browser.table(:index, 77).column_count }
  51. assert_equal(2, browser.table(:index, 1).column_count)
  52. assert_equal(1, browser.table(:id, 't1').column_count) # row one has 1 cell with a colspan of 2
  53. end
  54. def test_to_a
  55. table1Expected = [ ["Row 1 Col1" , "Row 1 Col2"] ,[ "Row 2 Col1" , "Row 2 Col2"] ]
  56. assert_equal(table1Expected, browser.table(:index , 1).to_a )
  57. end
  58. def test_links_and_images_in_table
  59. table = browser.table(:id, 'pic_table')
  60. image = table[1][2].image(:index,1)
  61. assert_equal("106", image.width)
  62. link = table[2][2].link(:index,1)
  63. assert_equal("Google", link.innerText)
  64. end
  65. def test_cell_directly
  66. assert( browser.cell(:id, 'cell1').exists? )
  67. assert_false( browser.cell(:id, 'no_exist').exists? )
  68. assert_equal( "Row 1 Col1", browser.cell(:id, 'cell1').to_s.strip )
  69. # not really cell directly, but just to show another way of geting the cell
  70. assert_equal( "Row 1 Col1", browser.table(:index,1)[1][1].to_s.strip )
  71. assert_equal(2, browser.cell(:id, "cell_with_colspan").colspan)
  72. end
  73. def test_row_directly
  74. assert( browser.row(:id, 'row1').exists? )
  75. assert_false( browser.row(:id, 'no_exist').exists? )
  76. assert_equal('Row 2 Col1' , browser.row(:id, 'row1')[1].to_s.strip )
  77. end
  78. def test_row_iterator
  79. t = browser.table(:index, 1)
  80. count = 1
  81. t.each do |row|
  82. if count == 1
  83. assert("Row 1 Col1", row[1].text)
  84. assert("Row 1 Col2", row[2].text)
  85. elsif count == 2
  86. assert("Row 2 Col1", row[1].text)
  87. assert("Row 2 Col2", row[2].text)
  88. end
  89. count += 1
  90. end
  91. end
  92. def test_row_collection
  93. t = browser.table(:index,1)
  94. count = 1
  95. t.rows.each do |row|
  96. if count == 1
  97. assert("Row 1 Col1", row[1].text)
  98. assert("Row 1 Col2", row[2].text)
  99. elsif count == 2
  100. assert("Row 2 Col1", row[1].text)
  101. assert("Row 2 Col2", row[2].text)
  102. end
  103. count += 1
  104. end
  105. end
  106. #def test_table_body
  107. # assert_equal( 1, browser.table(:index,1).bodies.length )
  108. # assert_equal( 3, browser.table(:id, 'body_test' ).bodies.length )
  109. #
  110. # count = 1
  111. # browser.table(:id, 'body_test').bodies.each do |n|
  112. #
  113. # # do something better here!
  114. # n.flash # this line commented out to speed up the test
  115. # case count
  116. # when 1
  117. # compare_text = "This text is in the FRST TBODY."
  118. # when 2
  119. # compare_text = "This text is in the SECOND TBODY."
  120. # when 3
  121. # compare_text = "This text is in the THIRD TBODY."
  122. # end
  123. #
  124. # assert_equal(compare_text, n[1][1].to_s.strip ) # this is the 1st cell of the first row of this particular body
  125. #
  126. # count += 1
  127. # end
  128. # assert_equal( count - 1, browser.table(:id, 'body_test').bodies.length )
  129. #
  130. # assert_equal( "This text is in the THIRD TBODY." ,browser.table(:id, 'body_test' ).body(:index,3)[1][1].to_s.strip )
  131. #
  132. # # iterate through all the rows in a table body
  133. # count = 1
  134. # browser.table(:id, 'body_test').body(:index, 2).each do | row |
  135. # # row.flash # this line commented out, to speed up the tests
  136. # if count == 1
  137. # assert_equal('This text is in the SECOND TBODY.', row[1].text.strip )
  138. # elsif count == 1 # BUG: Huh?
  139. # assert_equal('This text is also in the SECOND TBODY.', row[1].text.strip )
  140. # end
  141. # count+=1
  142. # end
  143. #end
  144. def test_table_container
  145. assert_nothing_raised { browser.table(:id, 't1').html }
  146. end
  147. end
  148. class TC_Tables_Simple < Test::Unit::TestCase
  149. def setup
  150. goto_page("simple_table.html")
  151. end
  152. def test_simple_table_access
  153. table = browser.table(:index,1)
  154. assert_equal("Row 3 Col1",table[3][1].text.strip)
  155. assert_equal("Row 1 Col1",table[1][1].text.strip)
  156. assert_equal("Row 3 Col2",table[3][2].text.strip)
  157. assert_equal(2,table.column_count)
  158. end
  159. end
  160. class TC_Tables_Buttons < Test::Unit::TestCase
  161. def setup
  162. goto_page("simple_table_buttons.html")
  163. end
  164. def test_simple_table_buttons
  165. table = browser.table(:index,1)
  166. table[1][1].button(:index,1).click
  167. assert(browser.text_field(:name,"confirmtext").verify_contains(/CLICK1/i))
  168. table[2][1].button(:index,1).click
  169. assert(browser.text_field(:name,"confirmtext").verify_contains(/CLICK2/i))
  170. table[1][1].button(:id,'b1').click
  171. assert(browser.text_field(:name,"confirmtext").verify_contains(/CLICK1/i))
  172. assert_raises(UnknownObjectException ) { table[1][1].button(:id,'b_missing').click }
  173. table[3][1].button(:index,2).click
  174. assert(browser.text_field(:name,"confirmtext").verify_contains(/TOO/i))
  175. table[3][1].button(:value ,"Click too").click
  176. assert(browser.text_field(:name,"confirmtext").verify_contains(/TOO/i))
  177. browser.table(:index,1)[4][1].text_field(:index,1).set("123")
  178. assert(browser.text_field(:index,2).verify_contains("123"))
  179. # check when a cell contains 2 objects
  180. # if there were 2 different html objects in the same cell, some weird things happened ( button caption could change for example)
  181. assert_equal( 'Click ->' , browser.table(:index,1)[5][1].text_field(:index,1).value )
  182. browser.table(:index,1)[5][1].text_field(:index,1).click
  183. assert_equal( 'Click ->' , browser.table(:index,1)[5][1].text_field(:index,1).value )
  184. browser.table(:index,1)[5][1].button(:index,1).click
  185. assert_equal( '' , browser.table(:index,1)[5][1].text_field(:index,1).value )
  186. end
  187. def test_simple_table_gif
  188. table = browser.table(:index,2)
  189. assert_match( /1\.gif/ , table[1][1].image( :index,1).src )
  190. assert_match( /2\.gif/ , table[1][2].image( :index ,1).src )
  191. assert_match( /3\.gif/ , table[1][3].image( :index ,1).src )
  192. assert_match( /1\.gif/ , table[3][1].image( :index ,1).src )
  193. assert_match( /2\.gif/ , table[3][2].image( :index ,1).src )
  194. assert_match( /3\.gif/ , table[3][3].image( :index ,1).src )
  195. table = browser.table(:index,3)
  196. assert_match( /1\.gif/ , table[1][1].image( :index ,1).src )
  197. assert_match( /2\.gif/ , table[1][1].image( :index ,2).src )
  198. assert_match( /3\.gif/ , table[1][1].image( :index ,3).src )
  199. assert_match( /1\.gif/ , table[3][1].image( :index ,1).src )
  200. assert_match( /2\.gif/ , table[3][1].image( :index ,2).src )
  201. assert_match( /3\.gif/ , table[3][1].image( :index ,3).src )
  202. end
  203. def test_table_with_hidden_or_visible_rows
  204. t = browser.table(:id , 'show_hide')
  205. # expand the table
  206. t.each do |r|
  207. r[1].image(:src, /plus/).click if r[1].image(:src, /plus/).exists?
  208. end
  209. # shrink rows 1,2,3
  210. count=1
  211. t.each do |r|
  212. r[1].image(:src, /minus/).click if r[1].image(:src, /minus/).exists? and (1..3) === count
  213. count=2
  214. end
  215. end
  216. # def test_table_from_element
  217. # button = browser.button(:id, "b1")
  218. # table = Table.create_from_element(browser, button)
  219. # table[2][1].button(:index, 1).click
  220. # assert(browser.textField(:name, "confirmtext").verify_contains(/CLICK2/i))
  221. # end
  222. end
  223. class TC_Table_Columns < Test::Unit::TestCase
  224. def setup
  225. goto_page("simple_table_columns.html")
  226. end
  227. # def test_get_columnvalues_single_column
  228. # assert_equal(["R1C1", "R2C1", "R3C1"], browser.table(:index, 1).column_values(1))
  229. # end
  230. def test_colspan
  231. assert_equal(2, browser.table(:index, 3)[2][1].colspan)
  232. assert_equal(1, browser.table(:index, 3)[1][1].colspan)
  233. assert_equal(3, browser.table(:index, 3)[4][1].colspan)
  234. end
  235. def test_get_columnvalues_multiple_column
  236. assert_equal(["R1C1", "R2C1", "R3C1"], browser.table(:index, 2).column_values(1))
  237. assert_equal(["R1C3", "R2C3", "R3C3"], browser.table(:index, 2).column_values(3))
  238. end
  239. def test_get_columnvalues_with_colspan
  240. assert_equal(["R1C1", "R2C1", "R3C1", "R4C1", "R5C1", "R6C2"], browser.table(:index, 3).column_values(1))
  241. end
  242. def test_get_rowvalues_full_row
  243. assert_equal(["R1C1", "R1C2", "R1C3"], browser.table(:index, 3).row_values(1))
  244. end
  245. def test_get_rowvalues_with_colspan
  246. assert_equal(["R2C1", "R2C2"], browser.table(:index, 3).row_values(2))
  247. end
  248. def test_getrowvalues_with_rowspan
  249. assert_equal(["R5C1", "R5C2", "R5C3"], browser.table(:index, 3).row_values(5))
  250. assert_equal(["R6C2", "R6C3"], browser.table(:index, 3).row_values(6))
  251. end
  252. end
  253. class TC_Tables_Complex < Test::Unit::TestCase
  254. def setup
  255. goto_page("complex_table.html")
  256. end
  257. def test_complex_table_access
  258. table = browser.table(:index,1)
  259. assert_equal("subtable1 Row 1 Col1",table[1][1].table(:index,1)[1][1].text.strip)
  260. assert_equal("subtable1 Row 1 Col2",table[1][1].table(:index,1)[1][2].text.strip)
  261. assert_equal("subtable2 Row 1 Col2",table[2][1].table(:index,1)[1][2].text.strip)
  262. assert_equal("subtable2 Row 1 Col1",table[2][1].table(:index,1)[1][1].text.strip)
  263. end
  264. end
  265. class TC_Tables_Display < Test::Unit::TestCase
  266. include MockStdoutTestCase
  267. tag_method :test_showTables, :fails_on_ie
  268. def test_showTables
  269. goto_page("table1.html")
  270. $stdout = @mockout
  271. browser.showTables
  272. assert_equal(<<END_OF_MESSAGE, @mockout)
  273. There are 7 tables
  274. table: id: tblTest
  275. rows: 2
  276. columns: 2
  277. index: 1
  278. table: id: t1
  279. rows: 5
  280. columns: 1
  281. index: 2
  282. table: id: t2
  283. rows: 2
  284. columns: 2
  285. index: 3
  286. table: id:
  287. rows: 1
  288. columns: 2
  289. index: 4
  290. table: id: body_test
  291. rows: 5
  292. columns: 1
  293. index: 5
  294. table: id: pic_table
  295. rows: 2
  296. columns: 2
  297. index: 6
  298. table: id: tblTest1
  299. rows: 5
  300. columns: 1
  301. index: 7
  302. END_OF_MESSAGE
  303. end
  304. end