Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/form_handling_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import pytest
19
20
from selenium.common.exceptions import WebDriverException
21
from selenium.webdriver.common.by import By
22
from selenium.webdriver.support import expected_conditions as EC
23
from selenium.webdriver.support.ui import WebDriverWait
24
25
26
def test_should_click_on_submit_input_elements(driver, pages):
27
pages.load("formPage.html")
28
driver.find_element(By.ID, "submitButton").click()
29
WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))
30
31
32
def test_clicking_on_unclickable_elements_does_nothing(driver, pages):
33
pages.load("formPage.html")
34
driver.find_element(By.XPATH, "//body").click()
35
36
37
def test_should_be_able_to_click_image_buttons(driver, pages):
38
pages.load("formPage.html")
39
driver.find_element(By.ID, "imageButton").click()
40
WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))
41
42
43
def test_should_submit_input_in_form(driver, pages):
44
pages.load("formPage.html")
45
driver.find_element(By.NAME, "login").submit()
46
WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))
47
48
49
def test_should_submit_any_input_element_within_form(driver, pages):
50
pages.load("formPage.html")
51
driver.find_element(By.ID, "checky").submit()
52
WebDriverWait(driver, 3).until(EC.title_is("We Arrive Here"))
53
54
55
def test_should_submit_any_element_within_form(driver, pages):
56
pages.load("formPage.html")
57
driver.find_element(By.XPATH, "//form/p").submit()
58
WebDriverWait(driver, 5).until(EC.title_is("We Arrive Here"))
59
60
61
def test_should_submit_element_with_id_submit(driver, pages):
62
pages.load("formPage.html")
63
driver.find_element(By.ID, "submit").submit()
64
WebDriverWait(driver, 5).until(EC.title_is("We Arrive Here"))
65
66
67
def test_should_submit_element_with_name_submit(driver, pages):
68
pages.load("formPage.html")
69
driver.find_element(By.NAME, "submit").submit()
70
WebDriverWait(driver, 5).until(EC.title_is("We Arrive Here"))
71
72
73
def test_should_not_submit_button_outside_form(driver, pages):
74
pages.load("formPage.html")
75
with pytest.raises(WebDriverException):
76
driver.find_element(By.NAME, "SearchableText").submit()
77
78
79
def test_should_be_able_to_enter_text_into_atext_area_by_setting_its_value(driver, pages):
80
pages.load("javascriptPage.html")
81
textarea = driver.find_element(By.ID, "keyUpArea")
82
cheesey = "Brie and cheddar"
83
textarea.send_keys(cheesey)
84
assert textarea.get_attribute("value") == cheesey
85
86
87
def test_should_enter_data_into_form_fields(driver, pages):
88
pages.load("xhtmlTest.html")
89
element = driver.find_element(By.XPATH, "//form[@name='someForm']/input[@id='username']")
90
originalValue = element.get_attribute("value")
91
assert originalValue == "change"
92
93
element.clear()
94
element.send_keys("some text")
95
96
element = driver.find_element(By.XPATH, "//form[@name='someForm']/input[@id='username']")
97
newFormValue = element.get_attribute("value")
98
assert newFormValue == "some text"
99
100
101
def test_should_be_able_to_select_acheck_box(driver, pages):
102
pages.load("formPage.html")
103
checkbox = driver.find_element(By.ID, "checky")
104
assert checkbox.is_selected() is False
105
checkbox.click()
106
assert checkbox.is_selected() is True
107
checkbox.click()
108
assert checkbox.is_selected() is False
109
110
111
def test_should_toggle_the_checked_state_of_acheckbox(driver, pages):
112
pages.load("formPage.html")
113
checkbox = driver.find_element(By.ID, "checky")
114
assert checkbox.is_selected() is False
115
checkbox.click()
116
assert checkbox.is_selected() is True
117
checkbox.click()
118
assert checkbox.is_selected() is False
119
120
121
def test_toggling_acheckbox_should_return_its_current_state(driver, pages):
122
pages.load("formPage.html")
123
checkbox = driver.find_element(By.ID, "checky")
124
assert checkbox.is_selected() is False
125
checkbox.click()
126
assert checkbox.is_selected() is True
127
checkbox.click()
128
assert checkbox.is_selected() is False
129
130
131
def test_should_be_able_to_select_aradio_button(driver, pages):
132
pages.load("formPage.html")
133
radioButton = driver.find_element(By.ID, "peas")
134
assert radioButton.is_selected() is False
135
radioButton.click()
136
assert radioButton.is_selected() is True
137
138
139
def test_should_be_able_to_select_aradio_button_by_clicking_on_it(driver, pages):
140
pages.load("formPage.html")
141
radioButton = driver.find_element(By.ID, "peas")
142
assert radioButton.is_selected() is False
143
radioButton.click()
144
assert radioButton.is_selected() is True
145
146
147
def test_should_return_state_of_radio_buttons_before_interaction(driver, pages):
148
pages.load("formPage.html")
149
radioButton = driver.find_element(By.ID, "cheese_and_peas")
150
assert radioButton.is_selected() is True
151
152
radioButton = driver.find_element(By.ID, "cheese")
153
assert radioButton.is_selected() is False
154
155
156
def test_toggling_an_option_should_toggle_options_in_amulti_select(driver, pages):
157
pages.load("formPage.html")
158
159
select = driver.find_element(By.NAME, "multi")
160
option = select.find_elements(By.TAG_NAME, "option")[0]
161
162
selected = option.is_selected()
163
option.click()
164
assert not selected == option.is_selected()
165
166
option.click()
167
assert selected == option.is_selected()
168
169
170
def test_should_throw_an_exception_when_selecting_an_unselectable_element(driver, pages):
171
pages.load("formPage.html")
172
element = driver.find_element(By.XPATH, "//title")
173
with pytest.raises(WebDriverException):
174
element.click()
175
176
177
def test_sending_keyboard_events_should_append_text_in_inputs(driver, pages):
178
pages.load("formPage.html")
179
element = driver.find_element(By.ID, "working")
180
element.send_keys("Some")
181
value = element.get_attribute("value")
182
assert value == "Some"
183
184
element.send_keys(" text")
185
value = element.get_attribute("value")
186
assert value == "Some text"
187
188
189
def test_should_be_able_to_clear_text_from_input_elements(driver, pages):
190
pages.load("formPage.html")
191
element = driver.find_element(By.ID, "working")
192
element.send_keys("Some text")
193
value = element.get_attribute("value")
194
assert len(value) > 0
195
196
element.clear()
197
value = element.get_attribute("value")
198
assert len(value) == 0
199
200
201
def test_empty_text_boxes_should_return_an_empty_string_not_null(driver, pages):
202
pages.load("formPage.html")
203
emptyTextBox = driver.find_element(By.ID, "working")
204
assert emptyTextBox.get_attribute("value") == ""
205
206
emptyTextArea = driver.find_element(By.ID, "emptyTextArea")
207
assert emptyTextArea.get_attribute("value") == ""
208
209
210
def test_should_be_able_to_clear_text_from_text_areas(driver, pages):
211
pages.load("formPage.html")
212
element = driver.find_element(By.ID, "withText")
213
element.send_keys("Some text")
214
value = element.get_attribute("value")
215
assert len(value) > 0
216
217
element.clear()
218
value = element.get_attribute("value")
219
assert len(value) == 0
220
221
222
def test_radio_should_not_be_selected_after_selecting_sibling(driver, pages):
223
pages.load("formPage.html")
224
cheese = driver.find_element(By.ID, "cheese")
225
peas = driver.find_element(By.ID, "peas")
226
227
cheese.click()
228
assert cheese.is_selected() is True
229
assert peas.is_selected() is False
230
231
peas.click()
232
assert cheese.is_selected() is False
233
assert peas.is_selected() is True
234
235