This repository was archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbutton.py
More file actions
135 lines (113 loc) · 6.26 KB
/
button.py
File metadata and controls
135 lines (113 loc) · 6.26 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
# Copyright 2008-2011 Nokia Siemens Networks Oyj
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from runonfailure import RunOnFailure
class Button(RunOnFailure):
"""Contains keywords for operating on checkboxes and radio buttons."""
def select_checkbox(self, locator):
"""Selects checkbox identified by `locator`.
Does nothing if checkbox is already selected. Key attributes for
checkboxes are `id` and `name`. See `introduction` for details about
locating elements.
"""
self._info("Selecting checkbox '%s'." % locator)
locator = self._parse_locator(locator)
if not self._selenium.is_checked(locator):
self._selenium.check(locator)
def unselect_checkbox(self, locator):
"""Removes selection of checkbox identified by `locator`.
Does nothing if the checkbox is not checked. Key attributes for
checkboxes are `id` and `name`. See `introduction` for details about
locating elements.
"""
self._info("Unselecting checkbox '%s'." % locator)
locator = self._parse_locator(locator)
if self._selenium.is_checked(locator):
self._selenium.uncheck(locator)
def checkbox_should_be_selected(self, locator):
"""Verifies checkbox identified by `locator` is selected/checked.
Key attributes for checkboxes are `id` and `name`. See `introduction`
for details about locating elements.
"""
self._info("Verifying checkbox '%s' is selected." % locator)
if not self._selenium.is_checked(self._parse_locator(locator)):
raise AssertionError("Checkbox '%s' should have been selected "
"but was not" % locator)
def checkbox_should_not_be_selected(self, locator):
"""Verifies checkbox identified by `locator` is not selected/checked.
Key attributes for checkboxes are `id` and `name`. See `introduction`
for details about locating elements.
"""
self._info("Verifying checkbox '%s' is not selected." % locator)
if self._selenium.is_checked(self._parse_locator(locator)):
raise AssertionError("Checkbox '%s' should not have been selected"
% locator)
def select_radio_button(self, group_name, value, wait=''):
"""Sets selection of radio button group identified by `group_name` to `value`.
The radio button to be selected is located by two arguments:
- `group_name` is used as the name of the radio input
- `value` is used for the value attribute or for the id attribute
The XPath used to locate the correct radio button then looks like this:
//input[@type='radio' and @name='group_name' and (@value='value' or @id='value')]
Examples:
| Select Radio Button | size | XL | # Matches HTML like <input type="radio" name="size" value="XL">XL</input> |
| Select Radio Button | size | sizeXL | # Matches HTML like <input type="radio" name="size" value="XL" id="sizeXL">XL</input> |
See `introduction` for details about `wait` argument.
"""
self._info("Selecting '%s' from radio button '%s'." % (value, group_name))
xpath = "xpath=//input[@type='radio' and @name='%s' and (@value='%s' or @id='%s')]" \
% (group_name, value, value)
self._debug('Radio group locator: ' + xpath)
if not self._selenium.is_checked(xpath):
self._selenium.click(xpath)
if wait:
self.wait_until_page_loaded()
def radio_button_should_be_set_to(self, group_name, value):
"""Verifies radio button group identified by `group_name` has its selection set to `value`.
See `Select Radio Button` for information about how radio buttons are
located.
"""
self._info("Verifying radio button '%s' has selection '%s'." \
% (group_name, value))
xpath = "xpath=//input[@type='radio' and @name='%s' and @value='%s']" \
% (group_name, value)
self._debug('Radio group locator: ' + xpath)
if not self._selenium.is_checked(xpath):
actual_value = self._get_value_of_selected_radio_button(group_name)
raise AssertionError("Selection of radio button '%s' should have "
"been '%s' but was '%s'"
% (group_name, value, actual_value))
def radio_button_should_not_be_selected(self, group_name):
"""Verifies radio button group identified by `group_name` has no selection.
See `Select Radio Button` for information about how radio buttons are
located.
"""
self._info("Verifying radio button '%s' has no selection." % group_name)
value = self._get_value_of_selected_radio_button(group_name)
if value:
raise AssertionError("Radio button group '%s' should not have had "
"selection, but '%s' was selected"
% (group_name, value))
def _get_number_of_radio_buttons_in_group(self, group_name):
js = "window.document.getElementsByName('%s').length" % group_name
return int(self._selenium.get_eval(js))
def _get_value_of_selected_radio_button(self, group_name):
num = self._get_number_of_radio_buttons_in_group(group_name)
for i in range(num):
xpath = "xpath=//input[@name='%s'][%d]" % (group_name, i+1)
self._debug('Radio button locator: ' + xpath)
if self._selenium.is_checked(xpath):
# self._selenium.get_attribute(xpath+'@value') doesn't work in IE8
js = "window.document.getElementsByName('%s')[%d].value" % (group_name, i)
return self._selenium.get_eval(js)
return ''