3
3
# distributed with this work for additional information
4
4
# regarding copyright ownership. The SFC licenses this file
5
5
# to you under the Apache License, Version 2.0 (the
6
- # "License"); you may not use this file except in compliance
6
+ # "License") you may not use this file except in compliance
7
7
# with the License. You may obtain a copy of the License at
8
8
#
9
9
# http://www.apache.org/licenses/LICENSE-2.0
21
21
22
22
class SelectElementHandlingTests (unittest .TestCase ):
23
23
24
- def testShouldBeAbleToChangeTheSelectedOptionInASelect (self ):
24
+ def testShouldBePossibleToDeselectASingleOptionFromASelectWhichAllowsMultipleChoice (self ):
25
25
self ._loadPage ("formPage" )
26
- selectBox = self .driver .find_element (by = By .XPATH , value = "//select[@name='selectomatic']" )
27
- options = selectBox .find_elements (by = By .TAG_NAME , value = "option" )
26
+
27
+ multiSelect = self .driver .find_element (By .ID , "multi" )
28
+ options = multiSelect .find_elements (By .TAG_NAME , "option" )
29
+
30
+ option = options [0 ]
31
+ self .assertTrue (option .is_selected ())
32
+ option .click ()
33
+ self .assertFalse (option .is_selected ())
34
+ option .click ()
35
+ self .assertTrue (option .is_selected ())
36
+
37
+ option = options [2 ]
38
+ self .assertTrue (option .is_selected ())
39
+
40
+ def testShouldBeAbleToChangeTheSelectedOptionInASelec (self ):
41
+ self ._loadPage ("formPage" )
42
+ selectBox = self .driver .find_element (By .XPATH , "//select[@name='selectomatic']" )
43
+ options = selectBox .find_elements (By .TAG_NAME , "option" )
28
44
one = options [0 ]
29
45
two = options [1 ]
30
46
self .assertTrue (one .is_selected ())
@@ -34,18 +50,49 @@ def testShouldBeAbleToChangeTheSelectedOptionInASelect(self):
34
50
self .assertFalse (one .is_selected ())
35
51
self .assertTrue (two .is_selected ())
36
52
37
- def testShouldBeAbleToSelectMoreThanOneOptionFromASelectWhichAllowsMultipleChoices (self ):
53
+ def testShouldBeAbleToSelectMoreThanOneOptionFromASelectWhichAllowsMultipleChoice (self ):
38
54
self ._loadPage ("formPage" )
39
55
40
- multiSelect = self .driver .find_element (by = By .ID , value = "multi" )
41
- options = multiSelect .find_elements (by = By .TAG_NAME , value = "option" )
56
+ multiSelect = self .driver .find_element (By .ID , "multi" )
57
+ options = multiSelect .find_elements (By .TAG_NAME , "option" )
42
58
for option in options :
43
59
if not option .is_selected ():
44
60
option .click ()
45
61
46
62
for i in range (len (options )):
47
63
option = options [i ]
48
- self .assertTrue (option .is_selected (), "Option at index is not selected but should be: " + str (i ))
64
+ self .assertTrue (option .is_selected (), "Option at index is not selected but should be: {0}" .format (i ))
65
+
66
+ def testShouldSelectFirstOptionaultIfNoneIsSelecte (self ):
67
+ self ._loadPage ("formPage" )
68
+ selectBox = self .driver .find_element (By .XPATH , "//select[@name='select-default']" )
69
+ options = selectBox .find_elements (By .TAG_NAME , "option" )
70
+ one = options [0 ]
71
+ two = options [1 ]
72
+ self .assertTrue (one .is_selected ())
73
+ self .assertFalse (two .is_selected ())
74
+
75
+ two .click ()
76
+ self .assertFalse (one .is_selected ())
77
+ self .assertTrue (two .is_selected ())
78
+
79
+ def testCanSelectElementsInOptGroup (self ):
80
+ self ._loadPage ("selectPage" )
81
+ element = self .driver .find_element (By .ID , "two-in-group" )
82
+ element .click ()
83
+ self .assertTrue (element .is_selected (), "Expected to be selected" )
84
+
85
+ def testCanGetValueFromOptionViaAttributeWhenAttributeDoesntExis (self ):
86
+ self ._loadPage ("formPage" )
87
+ element = self .driver .find_element (By .CSS_SELECTOR , "select[name='select-default'] option" )
88
+ self .assertEqual (element .get_attribute ("value" ), "One" )
89
+ element = self .driver .find_element (By .ID , "blankOption" )
90
+ self .assertEqual (element .get_attribute ("value" ), "" )
91
+
92
+ def testCanGetValueFromOptionViaAttributeWhenAttributeIsEmptyString (self ):
93
+ self ._loadPage ("formPage" )
94
+ element = self .driver .find_element (By .ID , "optionEmptyValueSet" )
95
+ self .assertEqual (element .get_attribute ("value" ), "" )
49
96
50
97
def _pageURL (self , name ):
51
98
return self .webserver .where_is (name + '.html' )
@@ -54,4 +101,9 @@ def _loadSimplePage(self):
54
101
self ._loadPage ("simpleTest" )
55
102
56
103
def _loadPage (self , name ):
104
+ try :
105
+ # just in case a previous test left open an alert
106
+ self .driver .switch_to .alert ().dismiss ()
107
+ except Exception :
108
+ pass
57
109
self .driver .get (self ._pageURL (name ))
0 commit comments