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
=== modified file 'src/sst/actions.py'
--- src/sst/actions.py 2012-07-17 13:43:15 +0000
+++ src/sst/actions.py 2012-08-09 17:35:20 +0000
@@ -41,6 +41,7 @@
41import os41import os
42import re42import re
43import time43import time
44import urllib2
4445
45from datetime import datetime46from datetime import datetime
46from pdb import set_trace as debug47from pdb import set_trace as debug
@@ -68,10 +69,10 @@
68 'assert_table_headers', 'assert_table_row_contains_text', 'assert_text',69 'assert_table_headers', 'assert_table_row_contains_text', 'assert_text',
69 'assert_text_contains', 'assert_textfield', 'assert_title',70 'assert_text_contains', 'assert_textfield', 'assert_title',
70 'assert_title_contains', 'assert_url', 'assert_url_contains', 'check_flags',71 'assert_title_contains', 'assert_url', 'assert_url_contains', 'check_flags',
71 'click_link', 'close_window', 'debug', 'dismiss_alert', 'end_test',72 'click_link', 'close_window', 'debug', 'dismiss_alert', 'download_file',
72 'click_button', 'click_element', 'exists_element', 'fails', 'get_argument',73 'end_test', 'click_button', 'click_element', 'exists_element', 'fails',
73 'get_base_url', 'get_cookies', 'get_current_url', 'get_element',74 'get_argument', 'get_base_url', 'get_cookies', 'get_current_url',
74 'get_element_by_css', 'get_element_by_xpath', 'get_elements',75 'get_element', 'get_element_by_css', 'get_element_by_xpath', 'get_elements',
75 'get_elements_by_css', 'get_elements_by_xpath', 'get_link_url',76 'get_elements_by_css', 'get_elements_by_xpath', 'get_link_url',
76 'get_page_source', 'go_back', 'go_to', 'refresh', 'reset_base_url',77 'get_page_source', 'go_back', 'go_to', 'refresh', 'reset_base_url',
77 'run_test', 'set_base_url', 'set_checkbox_value', 'set_dropdown_value',78 'run_test', 'set_base_url', 'set_checkbox_value', 'set_dropdown_value',
@@ -1395,3 +1396,45 @@
1395def get_cookies():1396def get_cookies():
1396 """Gets the cookies of current session (set of dicts)."""1397 """Gets the cookies of current session (set of dicts)."""
1397 return browser.get_cookies()1398 return browser.get_cookies()
1399
1400
1401def download_file(url, download_path):
1402 """
1403 Download the file stored in the URL specified and save it
1404 to the download path."""
1405 session_id = _get_session_id_cookie()
1406 headers = _make_cookie_header_with_session_id(session_id)
1407 web_file, file_name = _get_file_from_url(url, headers)
1408 file_path = os.path.join(download_path, file_name)
1409 with open(file_path, "wb") as local_file:
1410 local_file.write(web_file.read())
1411
1412
1413def _get_session_id_cookie():
1414 cookies = get_cookies()
1415 session = cookies[0]
1416 return session['value']
1417
1418
1419def _make_cookie_header_with_session_id(session_id):
1420 session_header = 'sessionid=' + session_id
1421 return {'Cookie': session_header}
1422
1423
1424def _get_file_from_url(url, headers):
1425 request = urllib2.Request(url, None, headers)
1426 web_file = urllib2.urlopen(request)
1427 file_name = _get_suggested_file_name(request)
1428 if not file_name:
1429 file_name = os.path.basename(url)
1430 return web_file, file_name
1431
1432
1433def _get_suggested_file_name(request):
1434 info = request.info()
1435 file_name = None
1436 if 'Content-Dispoisition' in info
1437 content_disposition = info['Content-Disposition']
1438 file_name = content_disposition.split('filename=')[1]
1439 file_name = file_name.strip('"\'')
1440 return file_name

Subscribers

People subscribed via source and target branches