Skip to content

Commit a3423c7

Browse files
authored
Merge pull request #203 from seleniumbase/fix-html-logging
Update html logging and screenshots
2 parents 5bb03c1 + 5fd4152 commit a3423c7

File tree

8 files changed

+100
-77
lines changed

8 files changed

+100
-77
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
Last_Page: http://xkcd.com/731/
1+
Last_Page: https://xkcd.com/731/
22
Browser: firefox
3-
Traceback: Traceback (most recent call last):
4-
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 331, in run
3+
Traceback: File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 615, in run
54
testMethod()
6-
File "/Users/michael/github/SeleniumBase/examples/test_fail.py", line 11, in test_find_army_of_robots_on_xkcd_desert_island
7-
self.wait_for_element_visible("div#ARMY_OF_ROBOTS", timeout=0.5)
8-
File "/Users/michael/github/SeleniumBase/seleniumbase/fixtures/base_case.py", line 206, in wait_for_element_visible
5+
File "/Users/michael/github/SeleniumBase/examples/test_fail.py", line 12, in test_find_army_of_robots_on_xkcd_desert_island
6+
self.assert_element("div#ARMY_OF_ROBOTS", timeout=1)
7+
File "/Users/michael/github/SeleniumBase/seleniumbase/fixtures/base_case.py", line 2166, in assert_element
8+
self.wait_for_element_visible(selector, by=by, timeout=timeout)
9+
File "/Users/michael/github/SeleniumBase/seleniumbase/fixtures/base_case.py", line 2143, in wait_for_element_visible
910
self.driver, selector, by, timeout)
10-
File "/Users/michael/github/SeleniumBase/seleniumbase/fixtures/page_actions.py", line 179, in wait_for_element_visible
11-
"Element %s was not visible in %s seconds!" % (selector, timeout))
12-
ElementNotVisibleException: Message: Element div#ARMY_OF_ROBOTS was not visible in 0.5 seconds!
13-
11+
File "/Users/michael/github/SeleniumBase/seleniumbase/fixtures/page_actions.py", line 239, in wait_for_element_visible
12+
selector, timeout, plural))
13+
Exception: Element {div#ARMY_OF_ROBOTS} was not visible after 1 second!
Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
<base href="http://xkcd.com"></base>
2-
<!DOCTYPE html>
3-
<html xmlns="http://www.w3.org/1999/xhtml"><head>
4-
<link title="Default" href="/s/b0dcca.css" type="text/css" rel="stylesheet" />
1+
<base href="https://xkcd.com">
2+
<html><head>
3+
<link rel="stylesheet" type="text/css" href="/s/b0dcca.css" title="Default">
54
<title>xkcd: Desert Island</title>
6-
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
7-
<link type="image/x-icon" href="/s/919f27.ico" rel="shortcut icon" />
8-
<link type="image/x-icon" href="/s/919f27.ico" rel="icon" />
9-
<link href="/atom.xml" title="Atom 1.0" type="application/atom+xml" rel="alternate" />
10-
<link href="/rss.xml" title="RSS 2.0" type="application/rss+xml" rel="alternate" />
11-
<script async="" src="//www.google-analytics.com/analytics.js"></script><script>
12-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
13-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
14-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
15-
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
16-
17-
ga('create', 'UA-25700708-7', 'auto');
18-
ga('send', 'pageview');
19-
</script>
20-
<script src="//xkcd.com/1350/jquery.min.js" type="text/javascript"></script>
21-
<script src="//xkcd.com/static/json2.js" type="text/javascript"></script>
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<link rel="shortcut icon" href="/s/919f27.ico" type="image/x-icon">
7+
<link rel="icon" href="/s/919f27.ico" type="image/x-icon">
8+
<link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="/atom.xml">
9+
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/rss.xml">
10+
<script type="text/javascript" src="/s/b66ed7.js" async=""></script>
11+
<script type="text/javascript" src="/s/1b9456.js" async=""></script>
2212

2313
</head>
2414
<body>
@@ -29,64 +19,58 @@
2919
<li><a href="http://what-if.xkcd.com">What If?</a></li>
3020
<li><a href="http://blag.xkcd.com">Blag</a></li>
3121
<li><a href="http://store.xkcd.com/">Store</a></li>
32-
<li><a href="/about" rel="author">About</a></li>
22+
<li><a rel="author" href="/about">About</a></li>
3323
</ul>
3424
</div>
3525
<div id="topRight">
3626
<div id="masthead">
37-
<span><a href="/"><img width="185" height="83" alt="xkcd.com logo" src="//imgs.xkcd.com/static/terrible_small_logo.png" /></a></span>
38-
<span id="slogan">A webcomic of romance,<br /> sarcasm, math, and language.</span>
27+
<span><a href="/"><img src="/s/0b7742.png" alt="xkcd.com logo" width="185" height="83"></a></span>
28+
<span id="slogan">A webcomic of romance,<br> sarcasm, math, and language.</span>
3929
</div>
4030
<div id="news">
41-
Thing Explainer is available at: <a href="http://amzn.to/1GCXMJ5" title="Thing Explainer Amazon purchase link">Amazon</a>, <a href="http://www.barnesandnoble.com/w/thing-explainer-randall-munroe/1121864432?ean=9780544668256" title="Thing Explainer Barnes and Noble purchase link">Barnes &amp; Noble</a>, <a href="http://www.indiebound.org/book/9780544668256" title="Thing Explainer Indie Bound purchase link">Indie Bound</a>, <a href="http://www.hudsonbooksellers.com/thingexplainer" title="Thing Explainer Hudson purchase link">Hudson</a><br />
42-
<br />
43-
I'll be in <a href="http://www.andersonsbookshop.com/event/naperville-reads-randall-munroe" title="Napierville Reads event page">Naperville, IL</a> and <a href="https://www.eventbrite.ca/e/indigo-exclusive-randall-munroe-tickets-18694384407" title="EventBrite event page">Toronto, ON</a> (with <a href="http://www.qwantz.com/" title="Dinosaur Comics">Ryan North</a>!) next week.
31+
xkcd updates every Monday, Wednesday, and Friday.
4432

4533
</div>
4634
</div>
47-
<div class="bg box" id="bgLeft"></div>
48-
<div class="bg box" id="bgRight"></div>
35+
<div id="bgLeft" class="bg box"></div>
36+
<div id="bgRight" class="bg box"></div>
4937
</div>
50-
<div class="box" id="middleContainer">
38+
<div id="middleContainer" class="box">
5139

5240
<div id="ctitle">Desert Island</div>
5341
<ul class="comicNav">
5442
<li><a href="/1/">|&lt;</a></li>
55-
<li><a accesskey="p" href="/730/" rel="prev">&lt; Prev</a></li>
43+
<li><a rel="prev" href="/730/" accesskey="p">&lt; Prev</a></li>
5644
<li><a href="//c.xkcd.com/random/comic/">Random</a></li>
57-
<li><a accesskey="n" href="/732/" rel="next">Next &gt;</a></li>
45+
<li><a rel="next" href="/732/" accesskey="n">Next &gt;</a></li>
5846
<li><a href="/">&gt;|</a></li>
5947
</ul>
6048
<div id="comic">
61-
<img alt="Desert Island" title="Telescopes and bathyscaphes and sonar probes of Scottish lakes, Tacoma Narrows bridge collapse explained with abstract phase-space maps, some x-ray slides, a music score, Minard's Napoleonic war: the most exciting new frontier is charting what's already here." src="//imgs.xkcd.com/comics/desert_island.png" />
49+
<img src="//imgs.xkcd.com/comics/desert_island.png" title="Telescopes and bathyscaphes and sonar probes of Scottish lakes, Tacoma Narrows bridge collapse explained with abstract phase-space maps, some x-ray slides, a music score, Minard's Napoleonic war: the most exciting new frontier is charting what's already here." alt="Desert Island">
6250
</div>
6351
<ul class="comicNav">
6452
<li><a href="/1/">|&lt;</a></li>
65-
<li><a accesskey="p" href="/730/" rel="prev">&lt; Prev</a></li>
53+
<li><a rel="prev" href="/730/" accesskey="p">&lt; Prev</a></li>
6654
<li><a href="//c.xkcd.com/random/comic/">Random</a></li>
67-
<li><a accesskey="n" href="/732/" rel="next">Next &gt;</a></li>
55+
<li><a rel="next" href="/732/" accesskey="n">Next &gt;</a></li>
6856
<li><a href="/">&gt;|</a></li>
6957
</ul>
70-
<br />
71-
Permanent link to this comic: http://xkcd.com/731/<br />
72-
Image URL (for hotlinking/embedding): http://imgs.xkcd.com/comics/desert_island.png
73-
<div style="display: none" id="transcript">[[A man sits writing in a diary on a desert island, only the sandy tip of which with a palm tree on it stands above the water. Beneath the surface is a kelp forest, some sharks, a stingray, a shipwreck, a submarine, several large jellyfish, a giant squid fighting a sperm whale, a crashed plane, some coral formations, a thermal vent emitting a plume of smoke surrounded by several annelids, and a snail.]]
58+
<br>
59+
Permanent link to this comic: https://xkcd.com/731/<br>
60+
Image URL (for hotlinking/embedding): https://imgs.xkcd.com/comics/desert_island.png
61+
<div id="transcript" style="display: none">[[A man sits writing in a diary on a desert island, only the sandy tip of which with a palm tree on it stands above the water. Beneath the surface is a kelp forest, some sharks, a stingray, a shipwreck, a submarine, several large jellyfish, a giant squid fighting a sperm whale, a crashed plane, some coral formations, a thermal vent emitting a plume of smoke surrounded by several annelids, and a snail.]]
7462
Man: Day 44: Still stranded, with nothing but flat empty water as far as the eye can see.
7563

7664
{{Title text: Telescopes and bathyscapes and sonar probes of Scottish lakes, Tacoma Narrows bridge collapse explained with abstract phase-space maps, some x-ray slides, a music score, Minard's Napoleonic war: the most exciting new frontier is charting what's already here.}}</div>
77-
<br />
78-
<hr width="80%" />
79-
<br />
80-
<a href="http://amzn.to/1GCXMJ5"><img border="0" src="//imgs.xkcd.com/store/te-pages-sb.png" /></a><br />
8165
</div>
82-
<div class="box" id="bottom">
83-
<img width="520" height="100" usemap="#comicmap" alt="Selected Comics" src="//imgs.xkcd.com/s/a899e84.jpg" />
84-
<map name="comicmap" id="comicmap">
85-
<area alt="Grownups" href="/150/" coords="0,0,100,100" shape="rect" />
86-
<area alt="Circuit Diagram" href="/730/" coords="104,0,204,100" shape="rect" />
87-
<area alt="Angular Momentum" href="/162/" coords="208,0,308,100" shape="rect" />
88-
<area alt="Self-Description" href="/688/" coords="312,0,412,100" shape="rect" />
89-
<area alt="Alternative Energy Revolution" href="/556/" coords="416,0,520,100" shape="rect" />
66+
<div id="bottom" class="box">
67+
<img src="//imgs.xkcd.com/s/a899e84.jpg" alt="Selected Comics" usemap="#comicmap" width="520" height="100">
68+
<map id="comicmap" name="comicmap">
69+
<area shape="rect" coords="0,0,100,100" href="/150/" alt="Grownups">
70+
<area shape="rect" coords="104,0,204,100" href="/730/" alt="Circuit Diagram">
71+
<area shape="rect" coords="208,0,308,100" href="/162/" alt="Angular Momentum">
72+
<area shape="rect" coords="312,0,412,100" href="/688/" alt="Self-Description">
73+
<area shape="rect" coords="416,0,520,100" href="/556/" alt="Alternative Energy Revolution">
9074
</map>
9175
<div>
9276
<!--
@@ -105,9 +89,9 @@
10589
-->
10690
<a href="/rss.xml">RSS Feed</a> - <a href="/atom.xml">Atom Feed</a>
10791
</div>
108-
<br />
92+
<br>
10993
<div id="comicLinks">
110-
Comics I enjoy:<br />
94+
Comics I enjoy:<br>
11195
<a href="http://threewordphrase.com/">Three Word Phrase</a>,
11296
<a href="http://www.smbc-comics.com/">SMBC</a>,
11397
<a href="http://www.qwantz.com">Dinosaur Comics</a>,
@@ -118,20 +102,29 @@
118102
<a href="http://questionablecontent.net/">Questionable Content</a>,
119103
<a href="http://www.buttercupfestival.com/">Buttercup Festival</a>,
120104
<a href="http://www.mspaintadventures.com/?s=6&amp;p=001901">Homestuck</a>,
121-
<a href="http://www.jspowerhour.com/">Junior Scientist Power Hour</a>
105+
<a href="http://www.jspowerhour.com/">Junior Scientist Power Hour</a>,
106+
</div>
107+
<br>
108+
<div id="comicLinks">
109+
Other things:<br>
110+
<a href="http://womenalsoknowstuff.com/">Women Also Know Stuff</a>,
111+
<a href="https://techsolidarity.org/">Tech Solidarity</a>,
112+
<a href="https://medium.com/civic-tech-thoughts-from-joshdata/so-you-want-to-reform-democracy-7f3b1ef10597">Tips on technology and government</a>
122113
</div>
123-
<p>Warning: this comic occasionally contains strong language (which may be unsuitable for children), unusual humor (which may be unsuitable for adults), and advanced mathematics (which may be unsuitable for liberal-arts majors).</p>
124-
<div id="footnote">BTC 1FhCLQK2ZXtCUQDtG98p6fVH7S6mxAsEey<br />We did not invent the algorithm. The algorithm consistently finds Jesus. The algorithm killed Jeeves. <br />The algorithm is banned in China. The algorithm is from Jersey. The algorithm constantly finds Jesus.<br />This is not the algorithm. This is close.</div>
114+
<br>
115+
<center>
116+
<div id="footnote" style="width:70%">xkcd.com is best viewed with Netscape Navigator 4.0 or below on a Pentium 3±1 emulated in Javascript on an Apple IIGS<br>at a screen resolution of 1024x1. Please enable your ad blockers, disable high-heat drying, and remove your device<br>from Airplane Mode and set it to Boat Mode. For security reasons, please leave caps lock on while browsing.</div>
117+
</center>
125118
<div id="licenseText">
126119
<p>
127120
This work is licensed under a
128121
<a href="http://creativecommons.org/licenses/by-nc/2.5/">Creative Commons Attribution-NonCommercial 2.5 License</a>.
129122
</p><p>
130-
This means you're free to copy and share these comics (but not to sell them). <a href="/license.html" rel="license">More details</a>.</p>
123+
This means you're free to copy and share these comics (but not to sell them). <a rel="license" href="/license.html">More details</a>.</p>
131124
</div>
132125
</div>
133126

134127

135128

136129

137-
</body><!-- Layout by Ian Clasbey, davean, and chromakode --><canvas id="fxdriver-screenshot-canvas" style="display: none;" width="960" height="1943"></canvas></html>
130+
</body><!-- Layout by Ian Clasbey, davean, and chromakode --></html>

examples/example_logs/screenshot.png

-373 KB
Loading

seleniumbase/core/log_helper.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ def log_screenshot(test_logpath, driver):
1111
screenshot_name = settings.SCREENSHOT_NAME
1212
screenshot_path = "%s/%s" % (test_logpath, screenshot_name)
1313
try:
14-
driver.get_screenshot_as_file(screenshot_path)
14+
element = driver.find_element_by_tag_name('body')
15+
element_png = element.screenshot_as_png
16+
with open(screenshot_path, "wb") as file:
17+
file.write(element_png)
1518
except Exception:
16-
print("WARNING: Unable to get screenshot for failure logs!")
19+
try:
20+
driver.get_screenshot_as_file(screenshot_path)
21+
except Exception:
22+
print("WARNING: Unable to get screenshot for failure logs!")
1723

1824

1925
def log_test_failure_data(test, test_logpath, driver, browser):
@@ -82,7 +88,7 @@ def get_base_href_html(full_url):
8288
''' The base href line tells the html what the base page really is.
8389
This is important when trying to open the page outside it's home. '''
8490
base_url = get_base_url(full_url)
85-
return '<base href="%s"></base>' % base_url
91+
return '<base href="%s">' % base_url
8692

8793

8894
def get_html_source_with_base_href(driver, page_source):

seleniumbase/fixtures/base_case.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,7 @@ def switch_to_default_window(self):
24492449
self.switch_to_window(0)
24502450

24512451
def save_screenshot(self, name, folder=None):
2452+
""" The screenshot will be in PNG format. """
24522453
return page_actions.save_screenshot(self.driver, name, folder)
24532454

24542455
def get_new_driver(self, browser=None, headless=None,
@@ -3256,5 +3257,18 @@ def tearDown(self):
32563257
data_payload.logURL = index_file
32573258
self.testcase_manager.update_testcase_log_url(data_payload)
32583259
else:
3259-
# (Nosetests) Finally close all open browser windows
3260+
# (Nosetests)
3261+
if has_exception:
3262+
test_id = "%s.%s.%s" % (self.__class__.__module__,
3263+
self.__class__.__name__,
3264+
self._testMethodName)
3265+
test_logpath = "latest_logs/" + test_id
3266+
if not os.path.exists(test_logpath):
3267+
os.makedirs(test_logpath)
3268+
log_helper.log_test_failure_data(
3269+
self, test_logpath, self.driver, self.browser)
3270+
if len(self._drivers_list) > 0:
3271+
log_helper.log_screenshot(test_logpath, self.driver)
3272+
log_helper.log_page_source(test_logpath, self.driver)
3273+
# Finally close all open browser windows
32603274
self.__quit_all_drivers()

seleniumbase/fixtures/page_actions.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,25 @@ def save_screenshot(driver, name, folder=None):
369369
"""
370370
Saves a screenshot to the current directory (or to a subfolder if provided)
371371
If the folder provided doesn't exist, it will get created.
372+
The screenshot will be in PNG format.
372373
"""
374+
if "." not in name:
375+
name = name + ".png"
373376
if folder:
374377
abs_path = os.path.abspath('.')
375378
file_path = abs_path + "/%s" % folder
376379
if not os.path.exists(file_path):
377380
os.makedirs(file_path)
378-
screenshot_file = "%s/%s" % (file_path, name)
381+
screenshot_path = "%s/%s" % (file_path, name)
379382
else:
380-
screenshot_file = name
381-
driver.get_screenshot_as_file(screenshot_file)
383+
screenshot_path = name
384+
try:
385+
element = driver.find_element_by_tag_name('body')
386+
element_png = element.screenshot_as_png
387+
with open(screenshot_path, "wb") as file:
388+
file.write(element_png)
389+
except Exception:
390+
driver.get_screenshot_as_file(screenshot_path)
382391

383392

384393
def _get_last_page(driver):

seleniumbase/plugins/base_plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def add_fails_or_errors(self, test):
155155
test, self.test_count, br, self.duration))
156156

157157
def addFailure(self, test, err, capt=None, tbinfo=None):
158-
self.__log_all_options_if_none_specified(test)
158+
# self.__log_all_options_if_none_specified(test)
159159
self.add_fails_or_errors(test)
160160

161161
def addError(self, test, err, capt=None):
@@ -171,7 +171,8 @@ def addError(self, test, err, capt=None):
171171
'''begin captured logging'''
172172
''' << --------------------''', 1)[0])
173173
else:
174-
self.__log_all_options_if_none_specified(test)
174+
# self.__log_all_options_if_none_specified(test)
175+
pass
175176
self.add_fails_or_errors(test)
176177

177178
def handleError(self, test, err, capt=None):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='seleniumbase',
20-
version='1.15.5',
20+
version='1.15.6',
2121
description='All-In-One Test Automation Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)