Add save_data_as() method

This commit is contained in:
Michael Mintz 2018-08-19 15:05:46 -04:00
parent 4b49534778
commit 2e29316c6d
2 changed files with 15 additions and 0 deletions

View File

@ -1629,6 +1629,14 @@ class BaseCase(unittest.TestCase):
page_utils._download_file_to(
file_url, destination_folder, new_file_name)
def save_data_as(self, data, file_name, destination_folder=None):
""" Saves the data specified to a file of the name specified.
If no destination folder is specified, the default one is used.
(The default downloads folder = "./downloaded_files") """
if not destination_folder:
destination_folder = constants.Files.DOWNLOADS_FOLDER
page_utils._save_data_as(data, destination_folder, file_name)
def get_downloads_folder(self):
""" Returns the OS path of the Downloads Folder.
(Works with Chrome and Firefox only, for now.) """

View File

@ -1,6 +1,7 @@
"""
This module contains useful utility methods.
"""
import codecs
import re
import requests
@ -76,6 +77,12 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
code.write(r.content)
def _save_data_as(data, destination_folder, file_name):
out_file = codecs.open(destination_folder + '/' + file_name, "w+")
out_file.writelines(data)
out_file.close()
def _jq_format(code):
"""
DEPRECATED - Use re.escape() instead, which performs the intended action.