Merge lp:~elopio/selenium-simple-test/download into lp:selenium-simple-test

Proposed by Leo Arias
Status: Work in progress
Proposed branch: lp:~elopio/selenium-simple-test/download
Merge into: lp:selenium-simple-test
Diff against target: 72 lines (+47/-4)
1 file modified
src/sst/actions.py (+47/-4)
To merge this branch: bzr merge lp:~elopio/selenium-simple-test/download
Reviewer Review Type Date Requested Status
Vincent Ladeuil (community) Needs Information
Corey Goldberg Pending
Review via email: mp+118999@code.launchpad.net

Commit message

Added the download function.

Description of the change

Added the download function.
This branch lacks the self test.

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

> This branch lacks the self test.

Or several.

Just reading quickly:

68 + if 'Content-Dispoisition' in info

'Content-Disposition' would have been caught by tests ;)

But more generally, sst is about delegating that kind of stuff to the browser (there are *many* weird cases you don't want to even try to support that are already properly handled there).

You can even write tests using 'file:///' urls

review: Needs Fixing
Revision history for this message
Vincent Ladeuil (vila) wrote :

@Leo: ping

Are you still working on this proposal (mark it wip) or not (mark it rejected) ?

;)

review: Needs Information
Revision history for this message
Leo Arias (elopio) wrote :

Marked as work in progress.
It's currently not needed, but it will be in the future. However, it should probably be moved to the test that's requiring it, not necessarily on SST.

Unmerged revisions

291. By Leo Arias

Typo.

290. By Leo Arias

Typo.

289. By Leo Arias

Added the name from the content disposition.

288. By Leo Arias

Merged with trunk.

287. By Leo Arias

Cleaned the download function a little.

286. By Leo Arias

Typo.

285. By Leo Arias

Fixed the call to urlibopen.

284. By Leo Arias

Removed the print statements.

283. By Leo Arias

Fixed the get_cookies call.

282. By Leo Arias

Fixed typo.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/sst/actions.py'
2--- src/sst/actions.py 2012-07-17 13:43:15 +0000
3+++ src/sst/actions.py 2012-08-09 17:35:20 +0000
4@@ -41,6 +41,7 @@
5 import os
6 import re
7 import time
8+import urllib2
9
10 from datetime import datetime
11 from pdb import set_trace as debug
12@@ -68,10 +69,10 @@
13 'assert_table_headers', 'assert_table_row_contains_text', 'assert_text',
14 'assert_text_contains', 'assert_textfield', 'assert_title',
15 'assert_title_contains', 'assert_url', 'assert_url_contains', 'check_flags',
16- 'click_link', 'close_window', 'debug', 'dismiss_alert', 'end_test',
17- 'click_button', 'click_element', 'exists_element', 'fails', 'get_argument',
18- 'get_base_url', 'get_cookies', 'get_current_url', 'get_element',
19- 'get_element_by_css', 'get_element_by_xpath', 'get_elements',
20+ 'click_link', 'close_window', 'debug', 'dismiss_alert', 'download_file',
21+ 'end_test', 'click_button', 'click_element', 'exists_element', 'fails',
22+ 'get_argument', 'get_base_url', 'get_cookies', 'get_current_url',
23+ 'get_element', 'get_element_by_css', 'get_element_by_xpath', 'get_elements',
24 'get_elements_by_css', 'get_elements_by_xpath', 'get_link_url',
25 'get_page_source', 'go_back', 'go_to', 'refresh', 'reset_base_url',
26 'run_test', 'set_base_url', 'set_checkbox_value', 'set_dropdown_value',
27@@ -1395,3 +1396,45 @@
28 def get_cookies():
29 """Gets the cookies of current session (set of dicts)."""
30 return browser.get_cookies()
31+
32+
33+def download_file(url, download_path):
34+ """
35+ Download the file stored in the URL specified and save it
36+ to the download path."""
37+ session_id = _get_session_id_cookie()
38+ headers = _make_cookie_header_with_session_id(session_id)
39+ web_file, file_name = _get_file_from_url(url, headers)
40+ file_path = os.path.join(download_path, file_name)
41+ with open(file_path, "wb") as local_file:
42+ local_file.write(web_file.read())
43+
44+
45+def _get_session_id_cookie():
46+ cookies = get_cookies()
47+ session = cookies[0]
48+ return session['value']
49+
50+
51+def _make_cookie_header_with_session_id(session_id):
52+ session_header = 'sessionid=' + session_id
53+ return {'Cookie': session_header}
54+
55+
56+def _get_file_from_url(url, headers):
57+ request = urllib2.Request(url, None, headers)
58+ web_file = urllib2.urlopen(request)
59+ file_name = _get_suggested_file_name(request)
60+ if not file_name:
61+ file_name = os.path.basename(url)
62+ return web_file, file_name
63+
64+
65+def _get_suggested_file_name(request):
66+ info = request.info()
67+ file_name = None
68+ if 'Content-Dispoisition' in info
69+ content_disposition = info['Content-Disposition']
70+ file_name = content_disposition.split('filename=')[1]
71+ file_name = file_name.strip('"\'')
72+ return file_name

Subscribers

People subscribed via source and target branches