Best Python code snippet using toolium_python
test_download_files.py
Source:test_download_files.py
...129 assert _get_remote_node_for_download(context) == "localhost"130def test_get_download_directory_url_download_directory(context):131 context.download_directory = "/downloads"132 toolium.utils.download_files._get_remote_node_for_download = mock.Mock(return_value='localhost')133 assert _get_download_directory_url(context) == "http://localhost:{}/downloads".format(DOWNLOADS_SERVICE_PORT)134def test_get_download_directory_url_download_directory_none(context):135 context.download_directory = None136 toolium.utils.download_files._get_remote_node_for_download = mock.Mock(return_value='localhost')137 assert _get_download_directory_url(context) == "http://localhost:{}".format(DOWNLOADS_SERVICE_PORT)138def test_delete_remote_downloaded_file_no_server_section(context):139 context.driver_wrapper.config.remove_option('Server', 'enabled')140 response = mock.Mock()141 response.status_code.return_value = 200142 toolium.utils.download_files.requests.delete = mock.Mock(return_value=response)143 delete_remote_downloaded_file(context, "filename")144 toolium.utils.download_files.requests.delete.assert_not_called()145def test_delete_remote_downloaded_file_server_section(context):146 context.driver_wrapper.config.set('Server', 'enabled', 'true')147 toolium.utils.download_files._get_download_directory_url = mock.Mock(return_value='https://host:8001')148 response_mock = mock.MagicMock()149 type(response_mock).status_code = mock.PropertyMock(return_value=200)150 toolium.utils.download_files.requests.delete = mock.Mock(return_value=response_mock)151 delete_remote_downloaded_file(context, "filename")...
download_files.py
Source:download_files.py
...75 """76 destination_filepath = None77 if context.download_directory is not None and context.driver_wrapper.config.getboolean_optional('Server',78 'enabled'):79 url = _get_download_directory_url(context)80 file_url = '%s/%s' % (url, filename)81 destination_folder = os.path.join(DriverWrappersPool.output_directory, DOWNLOADS_FOLDER,82 context.download_directory)83 makedirs_safe(destination_folder)84 destination_filename = destination_filename if destination_filename else filename85 destination_filepath = os.path.join(destination_folder, destination_filename)86 context.logger.info('Retrieving downloaded file from "%s" to "%s"', file_url, destination_filepath)87 urlretrieve(file_url.encode('utf-8').decode(), destination_filepath)88 return destination_filepath89def get_downloaded_files_list(context):90 if context.download_directory is not None and context.driver_wrapper.config.getboolean_optional('Server',91 'enabled'):92 url = _get_download_directory_url(context)93 context.logger.info("Getting downloads list from '%s'", url)94 content = urlopen(url).read()95 return html.fromstring(content).xpath('//li/a/text()')96 if context.download_directory is None:97 destination_folder = os.path.join(DriverWrappersPool.output_directory, DOWNLOADS_FOLDER)98 else:99 destination_folder = os.path.join(DriverWrappersPool.output_directory, DOWNLOADS_FOLDER,100 context.download_directory)101 return os.listdir(destination_folder)102def _get_remote_node_for_download(context):103 remote_node = context.driver_wrapper.remote_node104 return remote_node105def _get_download_directory_url(context):106 remote_node = _get_remote_node_for_download(context)107 if context.download_directory:108 host = 'http://{}:{}'.format(remote_node, DOWNLOADS_SERVICE_PORT)109 url = urljoin(host, context.download_directory)110 else:111 url = 'http://{}:{}'.format(remote_node, DOWNLOADS_SERVICE_PORT)112 return url113def compare_downloaded_file(context, file_name, expected_folder, max_wait):114 """115 Compare downloaded file with the expected file116 :param context: behave context117 :param file_name: downloaded file118 :param expected_folder: folder with expected files119 :param max_wait: max time to wait120 """121 # Get downloaded file and compare with expected file122 template_file = os.path.join(os.getcwd(), expected_folder, file_name)123 start_time = time.time()124 while time.time() < start_time + max_wait:125 downloaded_file = get_downloaded_file_path(context, file_name)126 try:127 equals = filecmp.cmp(template_file, downloaded_file)128 except Exception:129 # File not found130 equals = False131 if equals:132 break133 time.sleep(1)134 end_time = time.time()135 # Show different lines in txt files136 delta = ''137 if not equals and file_name.endswith('.txt'):138 with open(downloaded_file) as downloaded, open(template_file) as template:139 diff = difflib.ndiff(downloaded.readlines(), template.readlines())140 delta = ''.join(x[2:] for x in diff if x.startswith('- '))141 delta = ':\n%s' % delta142 assert equals, ('The downloaded file "%s" is not equal to the expected file "%s" %s' % (143 file_name, os.path.join(expected_folder, file_name), delta))144 context.logger.debug('File downloaded in %f seconds', end_time - start_time)145def wait_until_remote_file_downloaded(context, filename, wait_sec=15):146 """147 Wait until remote file is downloaded.148 :param context: where you and behave can store information to share around. Automatically managed by behave.149 :param filename: (string) name of the file.150 :param wait_sec: time to wait in seconds151 """152 url = _get_download_directory_url(context)153 file_url = '{url}/{filename}'.format(url=url, filename=filename)154 response = 'ERROR'155 end_time = time.time() + wait_sec156 while 'ERROR' in response:157 assert time.time() <= end_time, 'File "{}" has not been downloaded in {} seconds: {}' \158 .format(file_url, wait_sec, response)159 time.sleep(1)160 try:161 response = requests.get(file_url).text162 except Exception as e:163 response = 'ERROR Exception in get method: \n %s' % e164def delete_remote_downloaded_file(context, file_dwn):165 """166 Delete from url given file name.167 :param context: where you and behave can store information to share around. Automatically managed by behave.168 :param file_dwn: (string) name of the file downloaded.169 """170 if context.driver_wrapper.config.getboolean_optional('Server', 'enabled'):171 url = _get_download_directory_url(context)172 file_dwn_url = '{url}/{filename}'.format(url=url, filename=file_dwn)173 r_dwn = requests.delete(file_dwn_url)174 print(r_dwn.status_code)175 assert r_dwn.status_code == 200, 'ERROR deleting file "{}": "{}"'.format(file_dwn_url, r_dwn.text)176def delete_retrieved_downloaded_file(context, file_dwn, file_retr):177 """178 Delete from local directory given file name.179 :param context: where you and behave can store information to share around. Automatically managed by behave.180 :param file_dwn: (string) name of the file downloaded.181 :param file_retr: (string) name of the file.182 """183 destination_filename = file_retr if file_retr else file_dwn184 destination_filepath = os.path.join(DriverWrappersPool.output_directory, DOWNLOADS_FOLDER,185 context.download_directory)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!