PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/pyccuracy/selenium_browser_driver.py

https://github.com/kenjiyamamoto/pyccuracy
Python | 176 lines | 154 code | 12 blank | 10 comment | 6 complexity | a69587215417043d0d33b724be9f6e53 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):
  20. super(type(self),self).__init__(browser_to_run, tests_dir)
  21. self.__port__ = 4444
  22. self.__host__ = "localhost"
  23. def resolve_element_key(self, context, element_type, element_key):
  24. if context == None: return element_key
  25. return SeleniumElementSelector.element(element_type, element_key)
  26. def start_test(self, url = "http://localhost"):
  27. self.selenium = selenium(self.__host__, self.__port__, self.__browser__, url)
  28. try:
  29. self.selenium.start()
  30. except Exception, e:
  31. sys.stderr.write("Error when starting selenium. Is it running ? Error: %s\n" % unicode(e))
  32. sys.exit(1)
  33. def page_open(self, url):
  34. self.selenium.open(url)
  35. def type(self, input_selector, text):
  36. self.selenium.type(input_selector, text)
  37. def clean_input(self, input_selector):
  38. self.selenium.type(input_selector, "")
  39. def click_element(self, element_selector):
  40. self.selenium.click(element_selector)
  41. def is_element_visible(self, element_selector):
  42. return self.selenium.is_element_present(element_selector) and self.selenium.is_visible(element_selector)
  43. def wait_for_page(self, timeout = 20000):
  44. self.selenium.wait_for_page_to_load(timeout)
  45. def get_title(self):
  46. return self.selenium.get_title()
  47. def get_xpath_count(self, xpath):
  48. return self.selenium.get_xpath_count(xpath)
  49. def get_class(self, name):
  50. klass = self.__get_attribute_value(name, 'class')
  51. return klass
  52. def is_element_enabled(self, element):
  53. script = """this.page().findElement("%s").disabled;"""
  54. script_return = self.selenium.get_eval(script % element)
  55. if script_return == "null":
  56. is_disabled = self.__get_attribute_value(element, "disabled")
  57. else:
  58. is_disabled = script_return[0].upper()=="T"
  59. return not is_disabled
  60. def checkbox_is_checked(self, checkbox_selector):
  61. return self.selenium.is_checked(checkbox_selector)
  62. def checkbox_check(self, checkbox_selector):
  63. self.selenium.check(checkbox_selector)
  64. def checkbox_uncheck(self, checkbox_selector):
  65. self.selenium.uncheck(checkbox_selector)
  66. def get_selected_index(self, element_selector):
  67. return int(self.selenium.get_selected_index(element_selector))
  68. def get_selected_value(self, element_selector):
  69. return self.selenium.get_selected_value(element_selector)
  70. def get_selected_text(self, element_selector):
  71. return self.selenium.get_selected_label(element_selector)
  72. def get_element_text(self, element_selector):
  73. text = ""
  74. tag_name_script = """this.page().findElement("%s").tagName;"""
  75. tag_name = self.selenium.get_eval(tag_name_script % element_selector).lower()
  76. properties = {
  77. "input" : "value",
  78. "textarea" : "value",
  79. "div" : "innerHTML"
  80. }
  81. script = """this.page().findElement("%s").%s;"""
  82. try:
  83. script_return = self.selenium.get_eval(script % (element_selector, properties[tag_name]))
  84. except KeyError, err:
  85. raise ValueError("The tag for element selector %s is %s and Pyccuracy only supports the following tags: %s",
  86. (element_selector, tag_name, ", ".join(properties.keys)))
  87. if script_return != "null":
  88. text = script_return
  89. return text
  90. def get_element_markup(self, element_selector):
  91. script = """this.page().findElement("%s").innerHTML;"""
  92. script_return = self.selenium.get_eval(script % element_selector)
  93. return script_return != "null" and script_return or ""
  94. def select_option_by_index(self, element_selector, index):
  95. return self.__select_option(element_selector, "index", index)
  96. def select_option_by_value(self, element_selector, value):
  97. return self.__select_option(element_selector, "value", value)
  98. def select_option_by_text(self, element_selector, text):
  99. return self.__select_option(element_selector, "label", text)
  100. def __select_option(self, element_selector, option_selector, option_value):
  101. error_message = "Option with %s '%s' not found" % (option_selector, option_value)
  102. try:
  103. self.selenium.select(element_selector, "%s=%s" % (option_selector, option_value))
  104. except Exception, error:
  105. if error.message == error_message:
  106. return False
  107. else:
  108. raise
  109. return True
  110. def get_link_href(self, link_selector):
  111. return self.__get_attribute_value(link_selector, "href")
  112. def get_image_src(self, image_selector):
  113. return self.__get_attribute_value(image_selector, "src")
  114. def get_link_text(self, link_selector):
  115. return self.selenium.get_text(link_selector)
  116. def mouseover_element(self, element_selector):
  117. self.selenium.mouse_over(element_selector)
  118. def is_element_empty(self, element_selector):
  119. current_text = self.get_element_text(element_selector)
  120. return current_text == ""
  121. def stop_test(self):
  122. self.selenium.stop()
  123. def __get_attribute_value(self, element, attribute):
  124. try:
  125. locator = element + "/@" + attribute
  126. attr_value = self.selenium.get_attribute(locator)
  127. except Exception, inst:
  128. if "Could not find element attribute" in str(inst):
  129. attr_value = None
  130. else:
  131. raise
  132. return attr_value