Created
July 18, 2010 17:20
-
-
Save gabrielfalcao/480549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# #!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# <terrain.py - selenium wrapper for lettuce bdd> | |
# Copyright (C) <2010> Gabriel Falcão <[email protected]> | |
# | |
# Permission is hereby granted, free of charge, to any person | |
# obtaining a copy of this software and associated documentation | |
# files (the "Software"), to deal in the Software without | |
# restriction, including without limitation the rights to use, | |
# copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the | |
# Software is furnished to do so, subject to the following | |
# conditions: | |
# | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
import sys | |
import time | |
from lxml import html | |
from lxml.cssselect import CSSSelector | |
from lettuce import world, before, after | |
from lettuce.django import django_url | |
from selenium import selenium | |
from django.utils import simplejson | |
from django.core.urlresolvers import reverse | |
@before.all | |
def setup_browser(): | |
try: | |
world.browser = SeleniumBrowser() | |
except Exception, e: | |
if 'Connection refused' in unicode(e): | |
print u"O selenium não está rodando" | |
sys.exit(1) | |
raise e | |
@after.all | |
def teardown_browser(total): | |
world.browser.quit() | |
class SeleniumBrowser(object): | |
def __init__(self, host='localhost', port=4444, browser='*firefox'): | |
self.host = host | |
self.port = port | |
self.browser = browser | |
self.driver = selenium(host, port, browser, django_url('/')) | |
self.driver.start() | |
def goto_url(self, url): | |
if not url.startswith("/"): | |
url = reverse(url) | |
self.driver.open(url) | |
def goto_django_url(self, url): | |
url = reverse(url) | |
self.goto_url(url) | |
def wait(self, seconds=5): | |
self.driver.wait_for_page_to_load(1000 * seconds) | |
def cssselect(self, selector): | |
dom = self.get_dom() | |
return dom.cssselect(selector) | |
def xpath(self, selector): | |
dom = self.get_dom() | |
return dom.xpath(selector) | |
def get_dom(self): | |
return html.fromstring(self.get_source()) | |
def get_attribute(self, selector, name): | |
selector = self.translate_css_to_xpath(selector) | |
script = 'this.page().findElement("%s").getAttribute("%s")' | |
json = self.driver.get_eval(script % (selector, name)) | |
return simplejson.loads(json) | |
def value_of(self, selector): | |
selector = self.translate_css_to_xpath(selector) | |
script = 'this.page().findElement("%s").value' | |
json = self.driver.get_eval(script % selector) | |
return json | |
def get_source(self): | |
return self.driver.get_html_source() | |
def translate_css_to_xpath(self, selector): | |
if not selector.startswith("/"): | |
css = CSSSelector(selector) | |
selector = css.path.replace('descendant-or-self::', '//') | |
return selector | |
def select_combo_by_value(self, selector, value): | |
selector = self.translate_css_to_xpath(selector) | |
self.driver.select(selector, 'value=%s' % value) | |
def select_combo_by_text(self, selector, label): | |
selector = self.translate_css_to_xpath(selector) | |
self.driver.select(selector, 'label=%s' % label) | |
def select_combo_by_index(self, selector, index): | |
selector = self.translate_css_to_xpath(selector) | |
self.driver.select(selector, 'index=%s' % index) | |
def select_combo_by_id(self, selector, id): | |
selector = self.translate_css_to_xpath(selector) | |
self.driver.select(selector, 'id=%s' % id) | |
def click(self, selector): | |
self.driver.click(self.translate_css_to_xpath(selector)) | |
def mouse_over(self, selector): | |
self.driver.mouse_over(self.translate_css_to_xpath(selector)) | |
def blur(self, selector): | |
selector = world.browser.translate_css_to_xpath(selector) | |
world.browser.driver.fire_event(selector, 'blur') | |
def is_visible(self, selector, timeout=10): | |
old = selector | |
selector = self.translate_css_to_xpath(selector) | |
r = timeout * 10 | |
for index in range(r): | |
time.sleep(0.1) | |
visible = self.driver.is_visible(selector) | |
if visible: | |
break | |
assert index < r, '%s still hidden, even after %d seconds' % (old, timeout) | |
return visible | |
def wait_until_present(self, selector, timeout=10): | |
old = selector | |
selector = self.translate_css_to_xpath(selector) | |
r = timeout * 10 | |
for index in range(r): | |
time.sleep(0.1) | |
present = self.xpath(selector) | |
if present: | |
break | |
assert index < r, '%s still does not exist, even after %d seconds' % (old, timeout) | |
return present | |
def wait_until_vanish(self, selector, timeout=10): | |
old = selector | |
selector = self.translate_css_to_xpath(selector) | |
r = timeout * 10 | |
for index in range(r): | |
time.sleep(0.1) | |
invisible = not self.xpath(selector) | |
if invisible: | |
break | |
assert index < r, '%s still exists, even after %d seconds' % (old, timeout) | |
return invisible | |
def is_hidden(self, selector, timeout=10): | |
old = selector | |
selector = self.translate_css_to_xpath(selector) | |
r = timeout * 10 | |
for index in range(r): | |
time.sleep(0.1) | |
hidden = not self.driver.is_visible(selector) | |
if hidden: | |
break | |
assert index < r-1, '%s still visible, even after %d seconds' % (old, timeout) | |
return hidden | |
def type(self, selector, text): | |
self.driver.type(self.translate_css_to_xpath(selector), text) | |
def empty(self, selector): | |
self.driver.type(self.translate_css_to_xpath(selector), "") | |
def drag_and_drop_to_object(self, obj_1, obj_2): | |
xpath_1 = self.translate_css_to_xpath(obj_1) | |
xpath_2 = self.translate_css_to_xpath(obj_2) | |
world.browser.driver.drag_and_drop_to_object(xpath_1, xpath_2) | |
def quit(self): | |
self.driver.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment