Skip to content

Commit 9913fbb

Browse files
author
Og B. Maciel
committed
Renamed get-scrreenshot_by_filename to screenshot and added tests.
1 parent d2dacbd commit 9913fbb

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

‎splinter/driver/__init__.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ def get_iframe(self, name):
9898
"""
9999
raise NotImplementedError("%s doesn't support frames." % self.driver_name)
100100

101-
def get_screenshot_as_file(self, name=None, suffix=None):
102-
"""
103-
Takes a screenshot of the current page and saves it locally.
104-
"""
105-
raise NotImplementedError("%s doesn't support taking screenshots." % self.driver_name)
106-
107101
def execute_script(self, script):
108102
"""
109103
Executes a given JavaScript in the browser.
@@ -426,6 +420,12 @@ def is_element_not_present_by_id(self, id, wait_time=None):
426420
"""
427421
raise NotImplementedError("%s doesn't support verifying if element is not present by id" % self.driver_name)
428422

423+
def screenshot(self, name=None, suffix=None):
424+
"""
425+
Takes a screenshot of the current page and saves it locally.
426+
"""
427+
raise NotImplementedError("%s doesn't support taking screenshots." % self.driver_name)
428+
429429
@property
430430
def cookies(self):
431431
"""

‎splinter/driver/webdriver/__init__.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ def is_element_not_present_by_id(self, id, wait_time=None):
117117
def get_alert(self):
118118
return AlertElement(self.driver.switch_to_alert())
119119

120-
def get_screenshot_as_file(self, name=None, suffix='.png'):
121-
(fd, filename) = tempfile.mkstemp(prefix=name, suffix=suffix)
122-
123-
self.driver.get_screenshot_as_file(filename)
124-
return filename
125-
126120
def is_text_present(self, text, wait_time=None):
127121
wait_time = wait_time or self.wait_time
128122
end_time = time.time() + wait_time
@@ -265,6 +259,15 @@ def check(self, name):
265259
def uncheck(self, name):
266260
self.find_by_name(name).first.uncheck()
267261

262+
def screenshot(self, name=None, suffix='.png'):
263+
264+
name = name or ''
265+
266+
(fd, filename) = tempfile.mkstemp(prefix=name, suffix=suffix)
267+
268+
self.driver.get_screenshot_as_file(filename)
269+
return filename
270+
268271
def select(self, name, value):
269272
self.find_by_xpath('//select[@name="%s"]/option[@value="%s"]' % (name, value)).first._element.click()
270273

‎tests/base.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from is_text_present import IsTextPresentTest
1818
from mouse_interaction import MouseInteractionTest
1919
from status_code import StatusCodeTest
20+
from screenshot import ScreenshotTest
2021
from type import SlowlyTypeTest
2122
from popups import PopupWindowsTest
2223

@@ -86,7 +87,7 @@ def test_should_receive_browser_on_parent(self):
8687
self.assertEqual(self.browser, element.parent)
8788

8889

89-
class WebDriverTests(BaseBrowserTests, IFrameElementsTest, ElementDoestNotExistTest, IsElementPresentTest, AsyncFinderTests, StatusCodeTest, MouseInteractionTest, PopupWindowsTest):
90+
class WebDriverTests(BaseBrowserTests, IFrameElementsTest, ElementDoestNotExistTest, IsElementPresentTest, AsyncFinderTests, StatusCodeTest, MouseInteractionTest, PopupWindowsTest, ScreenshotTest):
9091

9192
def test_can_execute_javascript(self):
9293
"should be able to execute javascript"

‎tests/screenshot.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2013 splinter authors. All rights reserved.
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file.
6+
7+
class ScreenshotTest(object):
8+
9+
def test_take_screenshot(self):
10+
"should take a screenshot of the current page"
11+
filename = self.browser.screenshot()
12+
self.assertTrue('tmp' in filename)
13+
14+
def test_take_screenshot_with_prefix(self):
15+
"should add the prefix to the screenshot file name"
16+
filename = self.browser.screenshot(name='foobar')
17+
self.assertTrue('foobar' in filename)
18+
19+
def test_take_screenshot_with_suffix(self):
20+
"should add the suffix to the screenshot file name"
21+
filename = self.browser.screenshot(suffix='jpeg')
22+
self.assertEqual('jpg', filename[-3:])

0 commit comments

Comments
 (0)