forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_spec.rb
164 lines (135 loc) · 5.85 KB
/
table_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Table" do
before :each do
browser.goto(WatirSpec.url_for("tables.html"))
end
# Exists
describe "#exists?" do
it "returns true if the table exists" do
expect(browser.table(id: 'axis_example')).to exist
expect(browser.table(id: /axis_example/)).to exist
expect(browser.table(index: 0)).to exist
expect(browser.table(xpath: "//table[@id='axis_example']")).to exist
end
it "returns the first table if given no args" do
expect(browser.table).to exist
end
it "returns false if the table does not exist" do
expect(browser.table(id: 'no_such_id')).to_not exist
expect(browser.table(id: /no_such_id/)).to_not exist
expect(browser.table(index: 1337)).to_not exist
expect(browser.table(xpath: "//table[@id='no_such_id']")).to_not exist
end
it "checks the tag name when locating by xpath" do
expect(browser.table(xpath: "//table//td")).to_not exist
expect(browser.table(xpath: "//table")).to exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.table(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.table(no_such_how: 'some_value').exists? }.to raise_error(Watir::Exception::MissingWayOfFindingObjectException)
end
end
# Other
describe "#strings" do
it "returns a two-dimensional array representation of the table" do
expect(browser.table(id: 'inner').strings).to eq [
["Table 2, Row 1, Cell 1",
"Table 2, Row 1, Cell 2"]
]
not_compliant_on :watir_classic do
expect(browser.table(id: 'outer').strings).to eq [
["Table 1, Row 1, Cell 1", "Table 1, Row 1, Cell 2"],
["Table 1, Row 2, Cell 1", "Table 1, Row 2, Cell 2\nTable 2, Row 1, Cell 1 Table 2, Row 1, Cell 2"],
["Table 1, Row 3, Cell 1", "Table 1, Row 3, Cell 2"]
]
end
deviates_on :watir_classic do
expect(browser.table(id: 'outer').strings).to eq [
["Table 1, Row 1, Cell 1", "Table 1, Row 1, Cell 2"],
["Table 1, Row 2, Cell 1", "Table 1, Row 2, Cell 2 Table 2, Row 1, Cell 1 Table 2, Row 1, Cell 2"],
["Table 1, Row 3, Cell 1", "Table 1, Row 3, Cell 2"]
]
end
end
end
describe "#hashes" do
it "returns an Array of Hashes for the common table usage" do
expect(browser.table(id: "axis_example").hashes).to eq [
{ "" => "March 2008", "Before income tax" => "", "Income tax" => "", "After income tax" => "" },
{ "" => "Gregory House", "Before income tax" => "5 934", "Income tax" => "1 347", "After income tax" => "4 587" },
{ "" => "Hugh Laurie", "Before income tax" => "6 300", "Income tax" => "1 479", "After income tax" => "4 821" },
{ "" => "April 2008", "Before income tax" => "", "Income tax" => "", "After income tax" => "" },
{ "" => "Gregory House", "Before income tax" => "5 863", "Income tax" => "1 331", "After income tax" => "4 532" },
{ "" => "Hugh Laurie", "Before income tax" => "6 252", "Income tax" => "1 420", "After income tax" => "4 832" },
{ "" => "Sum", "Before income tax" => "24 349", "Income tax" => "5 577", "After income tax" => "18 722"},
]
end
it "raises an error if the table could not be parsed" do
browser.goto(WatirSpec.url_for("uneven_table.html"))
expect {
browser.table.hashes
}.to raise_error("row at index 0 has 2 cells, expected 3")
end
end
describe "#click" do
it "fires the table's onclick event" do
browser.table(id: 'inner').click
expect(messages).to include('table')
end
end
describe "#[]" do
it "returns the nth child row" do
expect(browser.table(id: 'outer')[0].id).to eq "outer_first"
expect(browser.table(id: 'inner')[0].id).to eq "inner_first"
expect(browser.table(id: 'outer')[2].id).to eq "outer_last"
end
not_compliant_on :webdriver, :watir_classic do
it "raises UnknownRowException if the index is out of bounds" do
expect { browser.table(id: 'outer')[1337] }.to raise_error(Watir::Exception::UnknownRowException)
end
end
end
describe "#row" do
let(:table) { browser.table(id: 'outer') }
it "finds rows belonging to this table" do
expect(table.row(id: "outer_last")).to exist
expect(table.row(text: /Table 1, Row 1, Cell 1/)).to exist
end
it "does not find rows from a nested table" do
expect(table.row(id: "inner_first")).to_not exist
expect(table.row(text: /\ATable 2, Row 1, Cell 1 Table 2, Row 1, Cell 2\z/)).to_not exist
end
end
describe "#rows" do
it "finds the correct number of rows (excluding nested tables)" do
expect(browser.table(id: 'inner').rows.length).to eq 1
expect(browser.table(id: 'outer').rows.length).to eq 3
end
it "finds rows matching the selector" do
rows = browser.table(id: 'outer').rows(id: /first|last/)
expect(rows.first.id).to eq "outer_first"
expect(rows.last.id).to eq "outer_last"
end
it "does not find rows from a nested table" do
expect(browser.table(id: "outer").rows(id: "t2_r1_c1").size).to eq 0
end
end
describe "#tbody" do
it "returns the correct instance of TableSection" do
body = browser.table(index: 0).tbody(id: 'first')
expect(body).to be_instance_of(TableSection)
expect(body[0][0].text).to eq "March 2008"
end
end
describe "#tbodys" do
it "returns the correct instance of TableSection" do
bodies = browser.table(index: 0).tbodys
expect(bodies).to be_instance_of(TableSectionCollection)
expect(bodies[0].id).to eq "first"
expect(bodies[1].id).to eq "second"
end
end
end