Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 102 lines (90 sloc) 3.419 kb
381ef6ab »
2010-12-19 Added get_screenshot to elements
1 import base64
23a29e49 »
2011-03-19 Added get_computed_property to elements and load_jquery to the driver
2 import os
381ef6ab »
2010-12-19 Added get_screenshot to elements
3 from PIL import Image
c9572a8e »
2011-04-05 Renamed assertElementEqualsImage to assertScreenshot and updated to s…
4 from selenium.webdriver.remote.webdriver import WebDriver
5 from selenium.webdriver.remote.webelement import WebElement
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
6 import urllib
381ef6ab »
2010-12-19 Added get_screenshot to elements
7 try:
8 from cStringIO import StringIO
9 except ImportError:
10 from StringIO import StringIO
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
11
12 class NeedleWebElement(WebElement):
11e215e6 »
2011-04-05 Docs
13 """
14 An element on a page that Selenium has opened.
15
16 It is a Selenium :py:class:`~selenium.webdriver.remote.webelement.WebElement`
17 object with some extra methods for testing CSS.
18 """
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
19 def get_dimensions(self):
381ef6ab »
2010-12-19 Added get_screenshot to elements
20 """
21 Returns a dictionary containing, in pixels, the element's ``width`` and
22 ``height``, and it's ``left`` and ``top`` position relative to the document.
23 """
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
24 return self._parent.execute_script("""
25 var e = arguments[0];
26 var dimensions = {
27 'width': e.offsetWidth,
28 'height': e.offsetHeight,
29 'left': 0,
30 'top': 0
31 };
32 do {
33 dimensions['left'] += e.offsetLeft;
34 dimensions['top'] += e.offsetTop;
35 } while (e = e.offsetParent)
36 return dimensions;
37 """, self)
38
381ef6ab »
2010-12-19 Added get_screenshot to elements
39 def get_screenshot(self):
40 """
41 Returns a screenshot of this element as a PIL image.
42 """
43 d = self.get_dimensions()
44 return self._parent.get_screenshot_as_image().crop((
45 d['left'],
46 d['top'],
47 d['left'] + d['width'],
48 d['top'] + d['height'],
49 ))
50
23a29e49 »
2011-03-19 Added get_computed_property to elements and load_jquery to the driver
51 def get_computed_property(self, prop):
52 """
53 Returns the computed value of a CSS property.
54 """
55 self._parent.load_jquery()
56 return self._parent.execute_script("""
57 return $(arguments[0]).css(arguments[1]);
16930767 »
2011-06-13 Oops.
58 """, self, prop)
23a29e49 »
2011-03-19 Added get_computed_property to elements and load_jquery to the driver
59
381ef6ab »
2010-12-19 Added get_screenshot to elements
60
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
61 class NeedleWebDriver(WebDriver):
11e215e6 »
2011-04-05 Docs
62 """
63 A browser window that Selenium has opened.
64
65 It is a Selenium :py:class:`~selenium.webdriver.remote.webdriver.WebDriver`
66 object with some extra methods for testing CSS. See
67 `Selenium's documentation <http://seleniumhq.org/docs/03_webdriver.html>`_
68 for more information.
69 """
0e91ff87 »
2011-01-02 Renamed get_html to load_html
70 def load_html(self, html):
381ef6ab »
2010-12-19 Added get_screenshot to elements
71 """
11e215e6 »
2011-04-05 Docs
72 Similar to :py:meth:`get`, but instead of passing a URL to load in the
73 browser, the HTML for the page is provided.
381ef6ab »
2010-12-19 Added get_screenshot to elements
74 """
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
75 self.get('data:text/html,'+urllib.quote(html))
381ef6ab »
2010-12-19 Added get_screenshot to elements
76
77 def get_screenshot_as_image(self):
78 """
11e215e6 »
2011-04-05 Docs
79 Returns a screenshot of the current page as an RGB
80 `PIL image <http://www.pythonware.com/library/pil/handbook/image.htm>`_.
381ef6ab »
2010-12-19 Added get_screenshot to elements
81 """
0e91ff87 »
2011-01-02 Renamed get_html to load_html
82 fh = StringIO(base64.b64decode(self.get_screenshot_as_base64()))
83 return Image.open(fh).convert('RGB')
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
84
23a29e49 »
2011-03-19 Added get_computed_property to elements and load_jquery to the driver
85 def load_jquery(self):
86 """
11e215e6 »
2011-04-05 Docs
87 Loads jQuery 1.5 onto the current page so calls to
88 :py:meth:`execute_script` have access to it.
23a29e49 »
2011-03-19 Added get_computed_property to elements and load_jquery to the driver
89 """
90 # Bug in 1.5.1 means we can't load on data URL pages.
91 # Should be fixed in 1.5.2
92 # https://github.com/jquery/jquery/pull/269
93 self.execute_script(open(
94 os.path.join(self._get_js_path(), 'jquery-1.5.min.js')
95 ).read() + '\nreturn "";')
96
97 def _get_js_path(self):
98 return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'js')
99
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
100 def create_web_element(self, *args, **kwargs):
101 return NeedleWebElement(self, *args, **kwargs)
23a29e49 »
2011-03-19 Added get_computed_property to elements and load_jquery to the driver
102
103
7fb21539 »
2010-12-19 Reshuffled files, added setup.py
104
Something went wrong with that request. Please try again.