forked from cobrateam/splinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_elements.py
More file actions
227 lines (186 loc) · 10.4 KB
/
form_elements.py
File metadata and controls
227 lines (186 loc) · 10.4 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import time
import re
class FormElementsTest(object):
def test_fill(self):
self.browser.fill('query', 'new query')
value = self.browser.find_by_name('query').value
self.assertEqual('new query', value)
def test_fill_element(self):
self.browser.find_by_name('q').fill('new query')
time.sleep(1)
value = self.browser.find_by_name('q').value
self.assertEqual('new query', value)
def test_clicking_submit_input_doesnt_post_input_value_if_name_not_present(self):
self.browser.find_by_css('input.submit-input-no-name').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text.strip(), '')
def test_clicking_submit_input_posts_empty_value_if_value_not_present(self):
self.browser.find_by_css('input[name="submit-input-no-value"]').click()
body_text = self.browser.find_by_xpath('/descendant-or-self::*').text.strip()
self.assertTrue(
re.match(r'^submit-input-no-value:(?:| Submit)$', body_text),
repr(body_text))
def test_clicking_submit_input_doesnt_post_input_value_if_empty(self):
self.browser.find_by_css('input.submit-input-empty').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text.strip(), '')
def test_clicking_submit_input_posts_input_value_if_value_present(self):
self.browser.find_by_css('input[name="submit-input"]').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text,
'submit-input: submit-input-value')
def test_clicking_submit_button_doesnt_post_button_value_if_name_not_present(self):
self.browser.find_by_css('button.submit-button-no-name').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text, '')
def test_clicking_submit_button_posts_empty_value_if_value_not_present(self):
self.browser.find_by_css('button[name="submit-button-no-value"]').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text.strip(), 'submit-button-no-value:')
def test_clicking_submit_button_doesnt_post_button_value_if_empty(self):
self.browser.find_by_css('button.submit-button-empty').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text.strip(),'')
def test_clicking_submit_button_posts_button_value_if_value_present(self):
self.browser.find_by_css('button[name="submit-button"]').click()
self.assertEqual(
self.browser.find_by_xpath('/descendant-or-self::*').text,
'submit-button: submit-button-value')
def test_submiting_a_form_and_verifying_page_content(self):
self.browser.fill('query', 'my name')
self.browser.find_by_name('send').click()
self.assertIn('My name is: Master Splinter', self.browser.html)
def test_can_choose_a_radio_button(self):
"should provide a way to choose a radio button"
self.assertFalse(self.browser.find_by_id("gender-m").checked)
self.browser.choose("gender", "M")
self.assertTrue(self.browser.find_by_id("gender-m").checked)
def test_can_find_textarea_by_tag(self):
"should provide a way to find a textarea by tag_name"
tag = self.browser.find_by_tag("textarea").first
self.assertEqual('', tag.value)
def test_can_find_input_without_type(self):
"should recognize an input element that doesn't have a `type` attribute"
tag = self.browser.find_by_css('[name="typeless"]').first
self.assertEqual('default value', tag.value)
def test_can_find_button(self):
"should recognize a button"
tag = self.browser.find_by_css('.just-a-button').first
self.assertTrue(hasattr(tag, 'click'))
def test_can_find_option_by_value(self):
"should provide a way to find select option by value"
self.assertEqual("Rio de Janeiro", self.browser.find_option_by_value("rj").text)
def test_can_get_value_attribute_for_a_option(self):
"should option have a value attribute"
self.assertEqual("rj", self.browser.find_option_by_value("rj")["value"])
def test_can_find_option_by_text(self):
"should provide a way to find select option by text"
self.assertEqual("rj", self.browser.find_option_by_text("Rio de Janeiro").value)
def test_can_select_a_option(self):
"should provide a way to select a option"
self.assertFalse(self.browser.find_option_by_value("rj").selected)
self.browser.select("uf", "rj")
self.assertTrue(self.browser.find_option_by_value("rj").selected)
def test_can_select_an_option_in_an_optgroup(self):
"should provide a way to select an option that is in an optgroup"
self.assertEqual(self.browser.find_by_name("food").value, "apples")
self.browser.select("food", "grapes")
self.assertEqual(self.browser.find_by_name("food").value, "grapes")
def test_can_select_a_option_via_element(self):
"should provide a way to select a option via element"
self.assertFalse(self.browser.find_option_by_value("rj").selected)
self.browser.find_by_name("uf").select("rj")
self.assertTrue(self.browser.find_option_by_value("rj").selected)
def test_can_check_a_checkbox(self):
"should provide a way to check a radio checkbox"
self.assertFalse(self.browser.find_by_name("some-check").checked)
self.browser.check("some-check")
self.assertTrue(self.browser.find_by_name("some-check").checked)
def test_check_keeps_checked_if_called_multiple_times(self):
"should keep a checkbox checked if check() is called multiple times"
self.assertFalse(self.browser.find_by_name("some-check").checked)
self.browser.check("some-check")
self.browser.check("some-check")
self.assertTrue(self.browser.find_by_name("some-check").checked)
def test_can_uncheck_a_checkbox(self):
"should provide a way to uncheck a radio checkbox"
self.assertTrue(self.browser.find_by_name("checked-checkbox").checked)
self.browser.uncheck("checked-checkbox")
self.assertFalse(self.browser.find_by_name("checked-checkbox").checked)
def test_uncheck_should_keep_unchecked_if_called_multiple_times(self):
"should keep a checkbox unchecked if uncheck() is called multiple times"
self.assertTrue(self.browser.find_by_name("checked-checkbox").checked)
self.browser.uncheck("checked-checkbox")
self.browser.uncheck("checked-checkbox")
self.assertFalse(self.browser.find_by_name("checked-checkbox").checked)
def test_can_fill_text_field_in_form(self):
"should provide a away to change field value"
self.browser.fill_form({'query': 'new query'})
value = self.browser.find_by_name('query').value
self.assertEqual('new query', value)
def test_can_fill_password_field_in_form(self):
"should provide a way to change password value"
new_password = 'new password'
self.browser.fill_form({'password': new_password})
value = self.browser.find_by_name('password').value
self.assertEqual(new_password, value)
def test_can_fill_more_than_one_field_in_form(self):
"should provide a away to change field value"
self.browser.fill('query', 'my name')
self.assertFalse(self.browser.find_by_id("gender-m").checked)
self.assertFalse(self.browser.find_option_by_value("rj").selected)
self.assertFalse(self.browser.find_by_name("some-check").checked)
self.assertTrue(self.browser.find_by_name("checked-checkbox").checked)
self.browser.fill_form({
'query': 'another new query',
'description': 'Just another description value in the textarea',
'gender': 'M',
'uf': 'rj',
'some-check': True,
'checked-checkbox': False
})
query_value = self.browser.find_by_name('query').value
self.assertEqual('another new query', query_value)
desc_value = self.browser.find_by_name('description').value
self.assertEqual('Just another description value in the textarea', desc_value)
self.assertTrue(self.browser.find_by_id("gender-m").checked)
self.assertTrue(self.browser.find_option_by_value("rj").selected)
self.assertTrue(self.browser.find_by_name("some-check").checked)
self.assertFalse(self.browser.find_by_name("checked-checkbox").checked)
def test_can_fill_tel_text_field(self):
"should provide a way to change a tel field value"
new_telephone = '555-0042'
self.browser.fill_form({'telephone': new_telephone})
value = self.browser.find_by_name('telephone').value
self.assertEqual(new_telephone, value)
def test_can_fill_unknown_text_field(self):
"should provide a way to change a unknown text field type that isn't specifically defined"
new_search_keyword = 'foobar'
self.browser.fill_form({'search_keyword': new_search_keyword})
value = self.browser.find_by_name('search_keyword').value
self.assertEqual(new_search_keyword, value)
def test_can_clear_text_field_content(self):
self.browser.fill('query', 'random query')
value = self.browser.find_by_name('query').value
self.assertEqual('random query', value)
self.browser.find_by_name('query').clear()
value = self.browser.find_by_name('query').value
self.assertFalse(value)
def test_can_clear_password_field_content(self):
self.browser.fill('password', '1nF4m310')
value = self.browser.find_by_name('password').value
self.assertEqual('1nF4m310', value)
self.browser.find_by_name('password').clear()
value = self.browser.find_by_name('password').value
self.assertFalse(value)
def test_can_clear_tel_field_content(self):
self.browser.fill('telephone', '5553743980')
value = self.browser.find_by_name('telephone').value
self.assertEqual('5553743980', value)
self.browser.find_by_name('telephone').clear()
value = self.browser.find_by_name('telephone').value
self.assertFalse(value)