Permalink
Browse files

Add the is_element_visble and is_element_not_visible functions to the…

… BaseWebDriver
  • Loading branch information...
1 parent 908b0af commit 62a5130ceb5c25357a4596e071c39fd1a15991a4 @karlwnw karlwnw committed Apr 20, 2012
Showing with 79 additions and 1 deletion.
  1. +79 −1 splinter/driver/webdriver/__init__.py
@@ -6,7 +6,7 @@
from contextlib import contextmanager
from lxml.cssselect import CSSSelector
-from selenium.common.exceptions import NoSuchElementException
+from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException, StaleElementReferenceException
from selenium.webdriver.common.action_chains import ActionChains
from splinter.driver import DriverAPI, ElementAPI
@@ -100,6 +100,48 @@ def is_element_not_present(self, finder, selector, wait_time=None):
return True
return False
+ # return True if all found elements are visible
+ def is_element_visible(self, finder, selector, wait_time=None):
+ wait_time = wait_time or self.wait_time
+ end_time = time.time() + wait_time
+
+ while time.time() < end_time:
+ try:
+ elements = finder(selector)
+ if elements:
+ count_visible_elements = 0
+ for element in elements:
+ if not element.visible:
+ break
+ else:
+ count_visible_elements += 1
+ if count_visible_elements == len(elements):
+ return True
+ except ElementNotVisibleException:
+ continue
+ return False
+
+ # return False if all the found elements are not visible or don't exist
+ def is_element_not_visible(self, finder, selector, wait_time=None):
+ wait_time = wait_time or self.wait_time
+ end_time = time.time() + wait_time
+
+ while time.time() < end_time:
+ try:
+ elements = finder(selector)
+ if elements:
+ count_invisible_elements = 0
+ for element in elements:
+ if element.visible:
+ break
+ else:
+ count_invisible_elements += 1
+ if count_invisible_elements == len(elements):
+ return True
+ except (ElementNotVisibleException, StaleElementReferenceException) as e:
+ continue
+ return False
+
def is_element_present_by_css(self, css_selector, wait_time=None):
return self.is_element_present(self.find_by_css, css_selector, wait_time)
@@ -136,6 +178,42 @@ def is_element_present_by_id(self, id, wait_time=None):
def is_element_not_present_by_id(self, id, wait_time=None):
return self.is_element_not_present(self.find_by_id, id, wait_time)
+ def is_element_visible_by_css(self, css_selector, wait_time=None):
+ return self.is_element_visible(self.find_by_css, css_selector, wait_time)
+
+ def is_element_not_visible_by_css(self, css_selector, wait_time=None):
+ return self.is_element_not_visible(self.find_by_css, css_selector, wait_time)
+
+ def is_element_visible_by_xpath(self, xpath, wait_time=None):
+ return self.is_element_visible(self.find_by_xpath, xpath, wait_time)
+
+ def is_element_not_visible_by_xpath(self, xpath, wait_time=None):
+ return self.is_element_not_visible(self.find_by_xpath, xpath, wait_time)
+
+ def is_element_visible_by_tag(self, tag, wait_time=None):
+ return self.is_element_visible(self.find_by_tag, tag, wait_time)
+
+ def is_element_not_visible_by_tag(self, tag, wait_time=None):
+ return self.is_element_not_visible(self.find_by_tag, tag, wait_time)
+
+ def is_element_visible_by_name(self, name, wait_time=None):
+ return self.is_element_visible(self.find_by_name, name, wait_time)
+
+ def is_element_not_visible_by_name(self, name, wait_time=None):
+ return self.is_element_not_visible(self.find_by_name, name, wait_time)
+
+ def is_element_visible_by_value(self, value, wait_time=None):
+ return self.is_element_visible(self.find_by_value, value, wait_time)
+
+ def is_element_not_visible_by_value(self, value, wait_time=None):
+ return self.is_element_not_visible(self.find_by_value, value, wait_time)
+
+ def is_element_visible_by_id(self, id, wait_time=None):
+ return self.is_element_visible(self.find_by_id, id, wait_time)
+
+ def is_element_not_visible_by_id(self, id, wait_time=None):
+ return self.is_element_not_visible(self.find_by_id, id, wait_time)
+
def get_alert(self):
return AlertElement(self.driver.switch_to_alert())

0 comments on commit 62a5130

Please sign in to comment.