1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""The classes for web elements on a page.""" 6 7__author__ = 'cliffordcheng@google.com (Clifford Cheng)' 8 9import re 10from selenium.webdriver.common.keys import Keys 11 12 13class WebElements(object): 14 """The base class for all the web element controls.""" 15 16 def __init__(self, driver, element_id, name=None): 17 """Constructor.""" 18 self._driver = driver 19 self._element_id = element_id 20 self._name = name 21 self._element = driver.find_element_by_id(element_id) 22 23 24 def get_name(self): 25 """ 26 Returns the name of a web element. 27 28 @return The name of a web element 29 """ 30 return self._name 31 32 33 def get_element_id(self): 34 """ 35 Returns the element ID of a web element. 36 37 @return The element ID of a web element 38 """ 39 return self._element_id 40 41 42 def get_element(self): 43 """ 44 Returns the object of a web element. 45 46 @return The web element object 47 """ 48 return self._element 49 50 51class TextBox(WebElements): 52 """Web element textbox and its controls.""" 53 54 55 def get_value(self): 56 """ 57 Returns the value in the text box. 58 59 @return The value in the text box 60 """ 61 return self._element.get_attribute('value') 62 63 64 def set_value(self, value): 65 """ 66 Set a value in the text box. 67 68 @param value: The value to be set in the text box 69 @raises RuntimeError if an error occurred when setting values 70 """ 71 # Using backspace instead of clear() because of crbug/450812 72 # TODO(cliffordcheng): Revert this once the bug is fixed 73 original_value = self._element.get_attribute('value') 74 for i in range(len(original_value)): 75 self._element.send_keys('\b') 76 self._element.send_keys(value) 77 if self.get_value() != value: 78 raise RuntimeError( 79 'Failed to set value "%s"', self._element.get_name()) 80 81 82class Button(WebElements): 83 """Web element button and its controls.""" 84 85 def click(self): 86 """ 87 Click on the button. 88 """ 89 self._element.click() 90 91 92class CheckBox(WebElements): 93 """Web element checbkbox and its controls.""" 94 95 def set_value(self, value): 96 """ 97 Set a value in the check box. 98 99 @param value: The value ('on'/'off') to be set in the check box 100 """ 101 if value == 'on' and not self.get_value(): 102 self._element.click() 103 elif value == 'off' and self.get_value(): 104 self._element.click() 105 106 107 def get_value(self): 108 """ 109 Return the value of the check box. 110 111 @return True if the check box is checked, otherwise return False. 112 """ 113 return self._element.is_selected() 114 115 116class RadioButton(WebElements): 117 """Web element radio button and its controls.""" 118 119 def click(self): 120 """Click the radio button.""" 121 self._element.send_keys(Keys.SPACE) 122 # Click one more time to ensure it's clicked 123 # In some corner cases, it needs a second click 124 self._element.send_keys(Keys.SPACE) 125 126 127class ScrollBox(WebElements): 128 """Web element scroll box and its controls.""" 129 130 def get_value(self): 131 """ 132 Return the text in the scroll box. 133 134 @return The string in the scroll box 135 """ 136 get_inner_html = 'return document.getElementById("%s").innerHTML;' 137 get_inner_html %= self._element_id 138 inner_html = self._driver.execute_script(get_inner_html) 139 inner_text_search = re.search('<.*>(.*)<.*>', inner_html) 140 try: 141 return inner_text_search.group(1) 142 except AttributeError: 143 return None 144 145 146class WebElementBox(WebElements): 147 """Web element box and its controls.""" 148 149 def get_value(self): 150 """ 151 Return the text in the web element box. 152 153 @return The string in the web element box 154 """ 155 get_inner_html = 'return document.getElementById("%s").innerHTML;' 156 get_inner_html %= self._element_id 157 inner_html = self._driver.execute_script(get_inner_html) 158 inner_text_search = re.search('<.*>(.*)<.*>', inner_html) 159 try: 160 return inner_text_search.group(1) 161 except AttributeError: 162 return None 163