Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/element_attribute_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
23
24
def test_should_return_null_when_getting_the_value_of_an_attribute_that_is_not_listed(driver, pages):
25
pages.load("simpleTest.html")
26
head = driver.find_element(By.XPATH, "/html")
27
attribute = head.get_attribute("cheese")
28
assert attribute is None
29
30
31
def test_should_return_null_when_getting_src_attribute_of_invalid_img_tag(driver, pages):
32
pages.load("simpleTest.html")
33
img = driver.find_element(By.ID, "invalidImgTag")
34
img_attr = img.get_attribute("src")
35
assert img_attr is None
36
37
38
def test_should_return_an_absolute_url_when_getting_src_attribute_of_avalid_img_tag(driver, pages):
39
pages.load("simpleTest.html")
40
img = driver.find_element(By.ID, "validImgTag")
41
img_attr = img.get_attribute("src")
42
assert "icon.gif" in img_attr
43
44
45
def test_should_return_an_absolute_url_when_getting_href_attribute_of_avalid_anchor_tag(driver, pages):
46
pages.load("simpleTest.html")
47
img = driver.find_element(By.ID, "validAnchorTag")
48
img_attr = img.get_attribute("href")
49
assert "icon.gif" in img_attr
50
51
52
def test_should_return_empty_attribute_values_when_present_and_the_value_is_actually_empty(driver, pages):
53
pages.load("simpleTest.html")
54
body = driver.find_element(By.XPATH, "//body")
55
assert "" == body.get_attribute("style")
56
57
58
def test_should_return_the_value_of_the_disabled_attribute_as_false_if_not_set(driver, pages):
59
pages.load("formPage.html")
60
inputElement = driver.find_element(By.XPATH, "//input[@id='working']")
61
assert inputElement.get_attribute("disabled") is None
62
assert inputElement.is_enabled()
63
64
pElement = driver.find_element(By.ID, "peas")
65
assert pElement.get_attribute("disabled") is None
66
assert pElement.is_enabled()
67
68
69
def test_should_return_the_value_of_the_index_attribute_even_if_it_is_missing(driver, pages):
70
pages.load("formPage.html")
71
multiSelect = driver.find_element(By.ID, "multi")
72
options = multiSelect.find_elements(By.TAG_NAME, "option")
73
assert "1" == options[1].get_attribute("index")
74
75
76
def test_should_indicate_the_elements_that_are_disabled_are_not_is_enabled(driver, pages):
77
pages.load("formPage.html")
78
inputElement = driver.find_element(By.XPATH, "//input[@id='notWorking']")
79
assert not inputElement.is_enabled()
80
81
inputElement = driver.find_element(By.XPATH, "//input[@id='working']")
82
assert inputElement.is_enabled()
83
84
85
def test_elements_should_be_disabled_if_they_are_disabled_using_random_disabled_strings(driver, pages):
86
pages.load("formPage.html")
87
disabledTextElement1 = driver.find_element(By.ID, "disabledTextElement1")
88
assert not disabledTextElement1.is_enabled()
89
90
disabledTextElement2 = driver.find_element(By.ID, "disabledTextElement2")
91
assert not disabledTextElement2.is_enabled()
92
93
disabledSubmitElement = driver.find_element(By.ID, "disabledSubmitElement")
94
assert not disabledSubmitElement.is_enabled()
95
96
97
def test_should_indicate_when_atext_area_is_disabled(driver, pages):
98
pages.load("formPage.html")
99
textArea = driver.find_element(By.XPATH, "//textarea[@id='notWorkingArea']")
100
assert not textArea.is_enabled()
101
102
103
@pytest.mark.xfail_safari
104
def test_should_throw_exception_if_sending_keys_to_element_disabled_using_random_disabled_strings(driver, pages):
105
pages.load("formPage.html")
106
disabledTextElement1 = driver.find_element(By.ID, "disabledTextElement1")
107
with pytest.raises(WebDriverException):
108
disabledTextElement1.send_keys("foo")
109
assert "" == disabledTextElement1.text
110
111
disabledTextElement2 = driver.find_element(By.ID, "disabledTextElement2")
112
with pytest.raises(WebDriverException):
113
disabledTextElement2.send_keys("bar")
114
assert "" == disabledTextElement2.text
115
116
117
def test_should_indicate_when_aselect_is_disabled(driver, pages):
118
pages.load("formPage.html")
119
enabled = driver.find_element(By.NAME, "selectomatic")
120
disabled = driver.find_element(By.NAME, "no-select")
121
122
assert enabled.is_enabled()
123
assert not disabled.is_enabled()
124
125
126
def test_should_return_the_value_of_checked_for_acheckbox_even_if_it_lacks_that_attribute(driver, pages):
127
pages.load("formPage.html")
128
checkbox = driver.find_element(By.XPATH, "//input[@id='checky']")
129
assert checkbox.get_attribute("checked") is None
130
checkbox.click()
131
assert "true" == checkbox.get_attribute("checked")
132
133
134
def test_should_return_the_value_of_selected_for_radio_buttons_even_if_they_lack_that_attribute(driver, pages):
135
pages.load("formPage.html")
136
neverSelected = driver.find_element(By.ID, "cheese")
137
initiallyNotSelected = driver.find_element(By.ID, "peas")
138
initiallySelected = driver.find_element(By.ID, "cheese_and_peas")
139
140
assert neverSelected.get_attribute("checked") is None
141
assert initiallyNotSelected.get_attribute("checked") is None
142
assert "true" == initiallySelected.get_attribute("checked")
143
144
initiallyNotSelected.click()
145
assert neverSelected.get_attribute("selected") is None
146
assert "true" == initiallyNotSelected.get_attribute("checked")
147
assert initiallySelected.get_attribute("checked") is None
148
149
150
def test_should_return_the_value_of_selected_for_options_in_selects_even_if_they_lack_that_attribute(driver, pages):
151
pages.load("formPage.html")
152
selectBox = driver.find_element(By.XPATH, "//select[@name='selectomatic']")
153
options = selectBox.find_elements(By.TAG_NAME, "option")
154
one = options[0]
155
two = options[1]
156
assert one.is_selected()
157
assert not two.is_selected()
158
assert "true" == one.get_attribute("selected")
159
assert two.get_attribute("selected") is None
160
161
162
def test_should_return_value_of_class_attribute_of_an_element(driver, pages):
163
pages.load("xhtmlTest.html")
164
heading = driver.find_element(By.XPATH, "//h1")
165
classname = heading.get_attribute("class")
166
assert "header" == classname
167
168
169
# Disabled due to issues with Frames
170
# def test_should_return_value_of_class_attribute_of_an_element_after_switching_iframe(driver, pages):
171
# pages.load("iframes.html")
172
# driver.switch_to.frame("iframe1")
173
#
174
# wallace = driver.find_element(By.XPATH, "//div[@id='wallace']")
175
# classname = wallace.get_attribute("class")
176
# assert "gromit" == classname
177
178
179
def test_should_return_the_contents_of_atext_area_as_its_value(driver, pages):
180
pages.load("formPage.html")
181
value = driver.find_element(By.ID, "withText").get_attribute("value")
182
assert "Example text" == value
183
184
185
def test_should_return_the_contents_of_atext_area_as_its_value_when_set_to_non_norminal_true(driver, pages):
186
pages.load("formPage.html")
187
e = driver.find_element(By.ID, "withText")
188
driver.execute_script("arguments[0].value = 'tRuE'", e)
189
value = e.get_attribute("value")
190
assert "tRuE" == value
191
192
193
def test_should_treat_readonly_as_avalue(driver, pages):
194
pages.load("formPage.html")
195
element = driver.find_element(By.NAME, "readonly")
196
readOnlyAttribute = element.get_attribute("readonly")
197
198
textInput = driver.find_element(By.NAME, "x")
199
notReadOnly = textInput.get_attribute("readonly")
200
201
assert readOnlyAttribute != notReadOnly
202
203
204
def test_should_get_numeric_attribute(driver, pages):
205
pages.load("formPage.html")
206
element = driver.find_element(By.ID, "withText")
207
assert "5" == element.get_attribute("rows")
208
209
210
def test_can_return_atext_approximation_of_the_style_attribute(driver, pages):
211
pages.load("javascriptPage.html")
212
style = driver.find_element(By.ID, "red-item").get_attribute("style")
213
assert "background-color" in style.lower()
214
215
216
def test_should_correctly_report_value_of_colspan(driver, pages):
217
pages.load("tables.html")
218
219
th1 = driver.find_element(By.ID, "th1")
220
td2 = driver.find_element(By.ID, "td2")
221
222
assert "th1" == th1.get_attribute("id")
223
assert "3" == th1.get_attribute("colspan")
224
225
assert "td2" == td2.get_attribute("id")
226
assert "2" == td2.get_attribute("colspan")
227
228
229
def test_can_retrieve_the_current_value_of_atext_form_field_text_input(driver, pages):
230
pages.load("formPage.html")
231
element = driver.find_element(By.ID, "working")
232
assert "" == element.get_attribute("value")
233
element.send_keys("hello world")
234
assert "hello world" == element.get_attribute("value")
235
236
237
def test_can_retrieve_the_current_value_of_atext_form_field_email_input(driver, pages):
238
pages.load("formPage.html")
239
element = driver.find_element(By.ID, "email")
240
assert "" == element.get_attribute("value")
241
element.send_keys("[email protected]")
242
assert "[email protected]" == element.get_attribute("value")
243
244
245
def test_can_retrieve_the_current_value_of_atext_form_field_text_area(driver, pages):
246
pages.load("formPage.html")
247
element = driver.find_element(By.ID, "emptyTextArea")
248
assert "" == element.get_attribute("value")
249
element.send_keys("hello world")
250
assert "hello world" == element.get_attribute("value")
251
252
253
def test_should_return_null_for_non_present_boolean_attributes(driver, pages):
254
pages.load("booleanAttributes.html")
255
element1 = driver.find_element(By.ID, "working")
256
assert element1.get_attribute("required") is None
257
258
259
@pytest.mark.xfail_ie
260
def test_should_return_true_for_present_boolean_attributes(driver, pages):
261
pages.load("booleanAttributes.html")
262
element1 = driver.find_element(By.ID, "emailRequired")
263
assert "true" == element1.get_attribute("required")
264
element2 = driver.find_element(By.ID, "emptyTextAreaRequired")
265
assert "true" == element2.get_attribute("required")
266
element3 = driver.find_element(By.ID, "inputRequired")
267
assert "true" == element3.get_attribute("required")
268
element4 = driver.find_element(By.ID, "textAreaRequired")
269
assert "true" == element4.get_attribute("required")
270
271
272
@pytest.mark.xfail_chrome
273
@pytest.mark.xfail_edge
274
@pytest.mark.xfail_firefox
275
@pytest.mark.xfail_safari
276
@pytest.mark.xfail_remote
277
def test_should_get_unicode_chars_from_attribute(driver, pages):
278
pages.load("formPage.html")
279
title = driver.find_element(By.ID, "vsearchGadget").get_attribute("title")
280
assert "Hvad s\xf8ger du?" == title
281
282
283
@pytest.mark.xfail_chrome
284
@pytest.mark.xfail_edge
285
@pytest.mark.xfail_firefox
286
@pytest.mark.xfail_safari
287
@pytest.mark.xfail_remote
288
def test_should_get_values_and_not_miss_items(driver, pages):
289
pages.load("attributes.html")
290
expected = "4b273a33fbbd29013nN93dy4F1A~"
291
result = driver.find_element(By.CSS_SELECTOR, "li").get_attribute("value")
292
assert expected == result
293
294