Source

digrsser / tests / webtests / objects.py

Full commit
"""

"""
import logging
import os
import unittest
import urllib
import urlparse

from splinter.browser import Browser

from splinter.driver.webdriver.firefox import WebDriver as FirefoxWebDriver
from splinter.driver.webdriver.chrome import WebDriver as ChromeWebDriver

from splinter.driver import ElementAPI
from splinter.element_list import ElementList

class ChromeBrowser(ChromeWebDriver):

  def visit_path(self, path):
    # In this service, app is deployed at root. thus replace the path
    res = urlparse.urlsplit(self.url)
    newres = urlparse.SplitResult(res.scheme, res.netloc, path , res.query, res.fragment)
    newurl = urlparse.urlunsplit(newres)

    self.visit(newurl)

class WebBrowser(object):
  """
  Singleton browser
  """
  instance = None

  def __new__(cls, *arg):
    if not WebBrowser.instance:
      WebBrowser.instance = ChromeBrowser()
      #WebBrowser.instance = Browser('chrome')
    return WebBrowser.instance

class PageElement(object):
  """
  Singleton browser
  """

  def __new__(cls, selector):
    browser = WebBrowser()
    element = browser.find_by_css(selector)
    element.selector = selector

    if element:
      return element.first

    logging.warning('PageElement was not found with selector: %s' % selector)
    return ElementAPI()

class PageElements(object):
  """
  Singleton browser
  """

  def __new__(cls, selector):
    browser = WebBrowser()
    element = browser.find_by_css(selector)
    if element:
      return element

    logging.warning('PageElement was not found with selector: %s' % selector)
    return ElementList([])


class PageObject(unittest.TestCase):
  
  def __setitem__(self, key, value):
    if hasattr(self, key):
      setattr(self, key, value)

  def __getitem__(self, key):
    if hasattr(self, key):
      return getattr(self, key)
    raise Exception('Element %s cannot be found page object %s' % (key, self.__class__))

class TextInput(PageElement):
  pass

class Checkbox(PageElement):
  def check(self):
    self.browser.find_by_css_selector(self.selector).first.check()