PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/pyccuracy/selenium_browser_driver.py

https://github.com/bardusco/pyccuracy
Python | 226 lines | 204 code | 12 blank | 10 comment | 10 complexity | afc98da38b1ac692208a69b9bc7b7c2d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. # -*- coding: utf-8 -*-
  2. # Licensed under the Open Software License ("OSL") v. 3.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.opensource.org/licenses/osl-3.0.php
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import os
  12. import sys
  13. import time
  14. import urllib2
  15. from selenium import *
  16. from browser_driver import *
  17. from selenium_element_selector import SeleniumElementSelector
  18. class SeleniumBrowserDriver(BrowserDriver):
  19. def __init__(self, browser_to_run, tests_dir, extra_args):
  20. super(type(self),self).__init__(browser_to_run, tests_dir)
  21. if extra_args.has_key("selenium.server"):
  22. self.__host__ = extra_args["selenium.server"]
  23. else:
  24. self.__host__ = "localhost"
  25. if extra_args.has_key("selenium.port"):
  26. self.__port__ = int(extra_args["selenium.port"])
  27. else:
  28. self.__port__ = 4444
  29. def resolve_element_key(self, context, element_type, element_key):
  30. if context == None: return element_key
  31. return SeleniumElementSelector.element(element_type, element_key)
  32. def start_test(self, url = "http://localhost"):
  33. self.selenium = selenium(self.__host__, self.__port__, self.__browser__, url)
  34. try:
  35. self.selenium.start()
  36. except Exception, e:
  37. sys.stderr.write("Error when starting selenium. Is it running ? Error: %s\n" % unicode(e))
  38. sys.exit(1)
  39. def page_open(self, url):
  40. self.selenium.open(url)
  41. def type(self, input_selector, text):
  42. self.selenium.type(input_selector, text)
  43. def clean_input(self, input_selector):
  44. self.selenium.type(input_selector, "")
  45. def click_element(self, element_selector):
  46. self.selenium.click(element_selector)
  47. def is_element_visible(self, element_selector):
  48. error_message = "ERROR: Element %s not found" % (element_selector)
  49. is_present = self.selenium.is_element_present(element_selector)
  50. if is_present:
  51. try:
  52. is_present = self.selenium.is_visible(element_selector)
  53. except Exception, error:
  54. if error.message == error_message:
  55. is_present = False
  56. else:
  57. raise
  58. return is_present
  59. def wait_for_page(self, timeout=10000):
  60. self.selenium.wait_for_page_to_load(timeout)
  61. def get_title(self):
  62. return self.selenium.get_title()
  63. def get_xpath_count(self, xpath):
  64. return self.selenium.get_xpath_count(xpath)
  65. def get_class(self, name):
  66. klass = self.__get_attribute_value(name, 'class')
  67. return klass
  68. def is_element_enabled(self, element):
  69. script = """this.page().findElement("%s").disabled;"""
  70. script_return = self.selenium.get_eval(script % element)
  71. if script_return == "null":
  72. is_disabled = self.__get_attribute_value(element, "disabled")
  73. else:
  74. is_disabled = script_return[0].upper()=="T" # is it 'True'?
  75. return not is_disabled
  76. def checkbox_is_checked(self, checkbox_selector):
  77. return self.selenium.is_checked(checkbox_selector)
  78. def checkbox_check(self, checkbox_selector):
  79. self.selenium.check(checkbox_selector)
  80. def checkbox_uncheck(self, checkbox_selector):
  81. self.selenium.uncheck(checkbox_selector)
  82. def get_selected_index(self, element_selector):
  83. return int(self.selenium.get_selected_index(element_selector))
  84. def get_selected_value(self, element_selector):
  85. return self.selenium.get_selected_value(element_selector)
  86. def get_selected_text(self, element_selector):
  87. return self.selenium.get_selected_label(element_selector)
  88. def get_element_text(self, element_selector):
  89. text = ""
  90. tag_name_script = """this.page().findElement("%s").tagName;"""
  91. tag_name = self.selenium.get_eval(tag_name_script % element_selector).lower()
  92. properties = {
  93. "input" : "value",
  94. "textarea" : "value",
  95. "div" : "innerHTML"
  96. }
  97. script = """this.page().findElement("%s").%s;"""
  98. try:
  99. # if the element is not in the dict above, I'll assume that we need to use "innerHTML"
  100. script_return = self.selenium.get_eval(script % (element_selector, properties.get(tag_name, "innerHTML")))
  101. except KeyError, err:
  102. raise ValueError("The tag for element selector %s is %s and Pyccuracy only supports the following tags: %s",
  103. (element_selector, tag_name, ", ".join(properties.keys)))
  104. if script_return != "null":
  105. text = script_return
  106. return text
  107. def get_element_markup(self, element_selector):
  108. script = """this.page().findElement("%s").innerHTML;"""
  109. script_return = self.selenium.get_eval(script % element_selector)
  110. return script_return != "null" and script_return or ""
  111. def get_html_source(self):
  112. return self.selenium.get_html_source()
  113. def select_option_by_index(self, element_selector, index):
  114. return self.__select_option(element_selector, "index", index)
  115. def select_option_by_value(self, element_selector, value):
  116. return self.__select_option(element_selector, "value", value)
  117. def select_option_by_text(self, element_selector, text):
  118. return self.__select_option(element_selector, "label", text)
  119. def __select_option(self, element_selector, option_selector, option_value):
  120. error_message = "Option with %s '%s' not found" % (option_selector, option_value)
  121. try:
  122. self.selenium.select(element_selector, "%s=%s" % (option_selector, option_value))
  123. except Exception, error:
  124. if error.message == error_message:
  125. return False
  126. else:
  127. raise
  128. return True
  129. def get_link_href(self, link_selector):
  130. return self.__get_attribute_value(link_selector, "href")
  131. def get_image_src(self, image_selector):
  132. return self.__get_attribute_value(image_selector, "src")
  133. def get_link_text(self, link_selector):
  134. return self.selenium.get_text(link_selector)
  135. def mouseover_element(self, element_selector):
  136. self.selenium.mouse_over(element_selector)
  137. def mouseout_element(self, element_selector):
  138. self.selenium.mouse_out(element_selector)
  139. def is_element_empty(self, element_selector):
  140. current_text = self.get_element_text(element_selector)
  141. return current_text == ""
  142. def wait_for_element_present(self, element_selector, timeout):
  143. elapsed = 0
  144. interval = 0.5
  145. while (elapsed < timeout):
  146. elapsed += interval
  147. if self.is_element_visible(element_selector):
  148. return True
  149. time.sleep(interval)
  150. return False
  151. def wait_for_element_to_disappear(self, element_selector, timeout):
  152. elapsed = 0
  153. interval = 0.5
  154. while (elapsed < timeout):
  155. elapsed += interval
  156. if not self.is_element_visible(element_selector):
  157. return True
  158. time.sleep(interval)
  159. return False
  160. def drag_element(self, from_element_selector, to_element_selector):
  161. self.selenium.drag_and_drop_to_object(from_element_selector, to_element_selector)
  162. def stop_test(self):
  163. self.selenium.stop()
  164. def __get_attribute_value(self, element, attribute):
  165. try:
  166. locator = element + "/@" + attribute
  167. attr_value = self.selenium.get_attribute(locator)
  168. except Exception, inst:
  169. if "Could not find element attribute" in str(inst):
  170. attr_value = None
  171. else:
  172. raise
  173. return attr_value