forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_spec.rb
151 lines (128 loc) · 7.69 KB
/
option_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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Option" do
before :each do
browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
end
describe "#exists?" do
it "returns true if the element exists (page context)" do
expect(browser.option(:id, "nor")).to exist
expect(browser.option(:id, /nor/)).to exist
expect(browser.option(:value, "2")).to exist
expect(browser.option(:value, /2/)).to exist
expect(browser.option(:text, "Norway")).to exist
expect(browser.option(:text, /Norway/)).to exist
expect(browser.option(:class, "scandinavia")).to exist
expect(browser.option(:index, 1)).to exist
expect(browser.option(:xpath, "//option[@id='nor']")).to exist
end
it "returns the first option if given no args" do
expect(browser.option).to exist
end
it "returns true if the element exists (select_list context)" do
expect(browser.select_list(:name, "new_user_country").option(:id, "nor")).to exist
expect(browser.select_list(:name, "new_user_country").option(:id, /nor/)).to exist
expect(browser.select_list(:name, "new_user_country").option(:value, "2")).to exist
expect(browser.select_list(:name, "new_user_country").option(:value, /2/)).to exist
expect(browser.select_list(:name, "new_user_country").option(:text, "Norway")).to exist
expect(browser.select_list(:name, "new_user_country").option(:text, /Norway/)).to exist
expect(browser.select_list(:name, "new_user_country").option(:class, "scandinavia")).to exist
expect(browser.select_list(:name, "new_user_country").option(:index, 1)).to exist
expect(browser.select_list(:name, "new_user_country").option(:xpath, "//option[@id='nor']")).to exist
expect(browser.select_list(:name, "new_user_country").option(:label, "Germany")).to exist
end
it "returns false if the element does not exist (page context)" do
expect(browser.option(:id, "no_such_id")).to_not exist
expect(browser.option(:id, /no_such_id/)).to_not exist
expect(browser.option(:value, "no_such_value")).to_not exist
expect(browser.option(:value, /no_such_value/)).to_not exist
expect(browser.option(:text, "no_such_text")).to_not exist
expect(browser.option(:text, /no_such_text/)).to_not exist
expect(browser.option(:class, "no_such_class")).to_not exist
expect(browser.option(:index, 1337)).to_not exist
expect(browser.option(:xpath, "//option[@id='no_such_id']")).to_not exist
end
it "returns false if the element does not exist (select_list context)" do
expect(browser.select_list(:name, "new_user_country").option(:id, "no_such_id")).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:id, /no_such_id/)).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:value, "no_such_value")).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:value, /no_such_value/)).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:text, "no_such_text")).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:text, /no_such_text/)).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:class, "no_such_class")).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:index, 1337)).to_not exist
expect(browser.select_list(:name, "new_user_country").option(:xpath, "//option[@id='no_such_id']")).to_not exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.option(:id, 3.14).exists? }.to raise_error(TypeError)
expect { browser.select_list(:name, "new_user_country").option(:id, 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.option(:no_such_how, 'some_value').exists? }.to raise_error(MissingWayOfFindingObjectException)
expect { browser.select_list(:name, "new_user_country").option(:no_such_how, 'some_value').exists? }.to raise_error(MissingWayOfFindingObjectException)
end
end
describe "#select" do
it "selects the chosen option (page context)" do
browser.option(:text, "Denmark").select
expect(browser.select_list(:name, "new_user_country").selected_options.map(&:text)).to eq ["Denmark"]
end
it "selects the chosen option (select_list context)" do
browser.select_list(:name, "new_user_country").option(:text, "Denmark").select
expect(browser.select_list(:name, "new_user_country").selected_options.map(&:text)).to eq ["Denmark"]
end
it "selects the option when found by text (page context)" do
browser.option(:text, 'Sweden').select
expect(browser.option(:text, 'Sweden')).to be_selected
end
it "selects the option when found by text (select_list context)" do
browser.select_list(:name, 'new_user_country').option(:text, 'Sweden').select
expect(browser.select_list(:name, 'new_user_country').option(:text, 'Sweden')).to be_selected
end
# there's no onclick event for Option in IE / WebKit
# http://msdn.microsoft.com/en-us/library/ms535877(VS.85).aspx
compliant_on :firefox do
it "fires the onclick event (page context)" do
browser.option(:text, "Username 3").select
expect(browser.text_field(:id, 'delete_user_comment').value).to eq 'Don\'t do it!'
end
end
# there's no onclick event for Option in IE / WebKit
# http://msdn.microsoft.com/en-us/library/ms535877(VS.85).aspx
compliant_on :firefox do
it "fires onclick event (select_list context)" do
browser.select_list(:id, 'delete_user_username').option(:text, "Username 3").select
expect(browser.text_field(:id, 'delete_user_comment').value).to eq 'Don\'t do it!'
end
end
it "raises UnknownObjectException if the option does not exist (page context)" do
expect { browser.option(:text, "no_such_text").select }.to raise_error(UnknownObjectException)
expect { browser.option(:text, /missing/).select }.to raise_error(UnknownObjectException)
end
it "raises UnknownObjectException if the option does not exist (select_list context)" do
expect { browser.select_list(:name, "new_user_country").option(:text, "no_such_text").select }.to raise_error(UnknownObjectException)
expect { browser.select_list(:name, "new_user_country").option(:text, /missing/).select }.to raise_error(UnknownObjectException)
end
it "raises MissingWayOfFindingObjectException when given a bad 'how' (page context)" do
expect { browser.option(:missing, "Denmark").select }.to raise_error(MissingWayOfFindingObjectException)
end
it "raises MissingWayOfFindingObjectException when given a bad 'how' (select_list context)" do
expect { browser.select_list(:name, "new_user_country").option(:missing, "Denmark").select }.to raise_error(MissingWayOfFindingObjectException)
end
end
describe "#class_name" do
it "is able to get attributes (page context)" do
expect(browser.option(:text, 'Sweden').class_name).to eq "scandinavia"
end
it "is able to get attributes (select_list context)" do
expect(browser.select_list(:name, "new_user_country").option(:text, 'Sweden').class_name).to eq "scandinavia"
end
end
describe "#respond_to?" do
it "returns true for all attribute methods" do
expect(browser.select_list(:name, "new_user_country").option(:text, 'Sweden')).to respond_to(:class_name)
expect(browser.select_list(:name, "new_user_country").option(:text, 'Sweden')).to respond_to(:id)
expect(browser.select_list(:name, "new_user_country").option(:text, 'Sweden')).to respond_to(:text)
end
end
end