Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b720c9bd90
Fetching contributors…

Cannot retrieve contributors at this time

113 lines (93 sloc) 3.459 kb
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
'''
Created on Jun 21, 2010
'''
import re
import time
import base64
from unittestzero import Assert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotVisibleException
http_regex = re.compile('https?://((\w+\.)+\w+\.\w+)')
class Page(object):
"""
Base class for all Pages
"""
def __init__(self, testsetup):
self.testsetup = testsetup
self.base_url = testsetup.base_url
self.selenium = testsetup.selenium
self.timeout = testsetup.timeout
@property
def is_the_current_page(self):
if self._page_title:
page_title = self.page_title
Assert.equal(page_title, self._page_title,
"Expected page title: %s. Actual page title: %s" %
(self._page_title, page_title))
@property
def url_current_page(self):
return(self.selenium.current_url)
@property
def page_title(self):
WebDriverWait(self.selenium, self.timeout).until(lambda s: s.title)
return self.selenium.title
def refresh(self):
self.selenium.refresh()
def open(self, url_fragment):
self.selenium.get(self.base_url + url_fragment)
def is_element_present(self, *locator):
self.selenium.implicitly_wait(0)
try:
self.selenium.find_element(*locator)
return True
except NoSuchElementException:
# this will return a snapshot, which takes time.
return False
finally:
# set back to where you once belonged
self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
def is_element_visible(self, *locator):
try:
return self.selenium.find_element(*locator).is_displayed()
except (ElementNotVisibleException, NoSuchElementException):
# this will return a snapshot, which takes time.
return False
def wait_for_element_present(self, *locator):
count = 0
while not self.is_element_present(*locator):
time.sleep(1)
count += 1
if count == self.timeout:
raise Exception(*locator + ' has not loaded')
def wait_for_element_visible(self, *locator):
count = 0
while not self.is_element_visible(*locator):
time.sleep(1)
count += 1
if count == self.timeout:
raise Exception(*locator + " is not visible")
def wait_for_ajax(self):
count = 0
while count < self.timeout:
time.sleep(1)
count += 1
if self.selenium.execute_script("return jQuery.active == 0"):
return
raise Exception("Wait for AJAX timed out after %s seconds" % count)
def get_response_code(self, url):
# return the response status
import urllib2
try:
urllib2.urlopen(url)
return 200
except urllib2.HTTPError, e:
return e.code
def is_valid_link(self, url):
if self.get_response_code(url) == 200:
return True
return False
Jump to Line
Something went wrong with that request. Please try again.