Best Python code snippet using SeleniumBase
test_download.py
Source:test_download.py
...12 YoutubeDownloader,13 save_large_file,14 stream_file,15)16def assert_downloaded_file(url, file):17 assert file.exists()18 # our google test urls dont support HEAD19 req = requests.get(url)20 # we test against binary response: Content-Length not accurate as gzip-encoded21 assert file.stat().st_size == len(req.content)22def assert_headers(returned_headers):23 assert isinstance(returned_headers, requests.structures.CaseInsensitiveDict)24 assert returned_headers["Content-Type"] == "image/x-icon"25def get_dest_file(tmp_path):26 return tmp_path.joinpath("favicon.ico")27def test_missing_dest(tmp_path):28 with pytest.raises(requests.exceptions.ConnectionError):29 stream_file(url="http://some_url", byte_stream=io.BytesIO())30def test_invalid_url(tmp_path, invalid_url):31 dest_file = tmp_path / "favicon.ico"32 with pytest.raises(requests.exceptions.ConnectionError):33 stream_file(url=invalid_url, fpath=dest_file)34def test_no_output_supplied(valid_http_url):35 with pytest.raises(36 ValueError, match="Either file path or a bytesIO object is needed"37 ):38 stream_file(url=valid_http_url)39def test_first_block_download(valid_http_url):40 byte_stream = io.BytesIO()41 size, ret = stream_file(42 url=valid_http_url, byte_stream=byte_stream, only_first_block=True43 )44 assert_headers(ret)45 # valid_http_url randomly returns gzip-encoded content.46 # otherwise, expected size is default block size47 expected = 3062 if ret.get("Content-Encoding") == "gzip" else 102448 assert len(byte_stream.read()) <= expected49@pytest.mark.slow50def test_save_http(tmp_path, valid_http_url):51 dest_file = tmp_path / "favicon.ico"52 size, ret = stream_file(url=valid_http_url, fpath=dest_file)53 assert_headers(ret)54 assert_downloaded_file(valid_http_url, dest_file)55@pytest.mark.slow56def test_save_https(tmp_path, valid_https_url):57 dest_file = tmp_path / "favicon.ico"58 size, ret = stream_file(url=valid_https_url, fpath=dest_file)59 assert_headers(ret)60 assert_downloaded_file(valid_https_url, dest_file)61@pytest.mark.slow62def test_stream_to_bytes(valid_https_url):63 byte_stream = io.BytesIO()64 size, ret = stream_file(url=valid_https_url, byte_stream=byte_stream)65 assert_headers(ret)66 assert byte_stream.read() == requests.get(valid_https_url).content67@pytest.mark.slow68def test_save_parent_folder_missing(tmp_path, valid_http_url):69 dest_file = tmp_path / "some-folder" / "favicon.ico"70 with pytest.raises(IOError):71 stream_file(url=valid_http_url, fpath=dest_file)72@pytest.mark.slow73def test_save_http_error(tmp_path, http_error_url):74 dest_file = tmp_path / "favicon.ico"75 with pytest.raises(requests.exceptions.HTTPError):76 stream_file(url=http_error_url, fpath=dest_file)77@pytest.mark.slow78def test_large_download_http(tmp_path, valid_http_url):79 dest_file = tmp_path / "favicon.ico"80 save_large_file(valid_http_url, dest_file)81 assert_downloaded_file(valid_http_url, dest_file)82@pytest.mark.slow83def test_large_download_https(tmp_path, valid_https_url):84 dest_file = tmp_path / "favicon.ico"85 save_large_file(valid_https_url, dest_file)86 assert_downloaded_file(valid_https_url, dest_file)87@pytest.mark.slow88@pytest.mark.parametrize(89 "url,video_id",90 [91 ("https://vimeo.com/619427082", "619427082"),92 ("https://vimeo.com/619427082", "619427082"),93 ],94)95def test_youtube_download_serial(url, video_id, tmp_path):96 yt_downloader = YoutubeDownloader(threads=1)97 options = BestMp4.get_options(98 target_dir=tmp_path,99 filepath=pathlib.Path("%(id)s/video.%(ext)s"),100 )...
test_download_files.py
Source:test_download_files.py
...8 "https://chromedriver.storage.googleapis.com"9 "/101.0.4951.41/%s" % notes_file10 )11 self.download_file(notes_link)12 self.assert_downloaded_file(notes_file)13 notes_path = self.get_path_of_downloaded_file(notes_file)14 with open(notes_path, "r") as f:15 notes_data = f.read()16 self.assert_true(len(notes_data) > 100) # Verify file not empty17 text = "Switching to nested frame fails with chrome/chromedriver 100"18 self.assert_true(text in notes_data) # Verify file has expected data19 def test_download_files_from_pypi_with_edge(self):20 if self.browser != "edge":21 self.open("data:,")22 print("\n This test is only for Microsoft Edge (Chromium)!")23 print(' (Run this test using "--edge" or "--browser=edge")')24 self.skip('Use "--edge" or "--browser=edge"')25 self.open("https://pypi.org/project/seleniumbase/#files")26 pkg_header = self.get_text("h1.package-header__name").strip()27 pkg_name = pkg_header.replace(" ", "-")28 whl_file = pkg_name + "-py2.py3-none-any.whl"29 tar_gz_file = pkg_name + ".tar.gz"30 # Click the links to download the files into: "./downloaded_files/"31 # (If using Safari, IE, or Chromium Guest Mode: download directly.)32 # (The default Downloads Folder can't be changed when using those.)33 # (The same problem occurs when using an out-of-date chromedriver.)34 # (Use self.get_browser_downloads_folder() to get the folder used.)35 whl_selector = 'div#files a[href$="%s"]' % whl_file36 tar_selector = 'div#files a[href$="%s"]' % tar_gz_file37 if (38 self.browser == "safari"39 or self.browser == "ie"40 or (self.is_chromium() and self.guest_mode and not self.headless)41 or (42 self.browser == "chrome"43 and self.is_chromedriver_too_old()44 and self.headless45 )46 ):47 whl_href = self.get_attribute(whl_selector, "href")48 tar_href = self.get_attribute(tar_selector, "href")49 self.download_file(whl_href)50 self.download_file(tar_href)51 else:52 self.click(whl_selector)53 self.click(tar_selector)54 # Verify that the downloaded files appear in the [Downloads Folder]55 # (This only guarantees that the exact file name is in the folder.)56 # (This does not guarantee that the downloaded files are complete.)57 # (Later, we'll check that the files were downloaded successfully.)58 self.assert_downloaded_file(whl_file)59 self.assert_downloaded_file(tar_gz_file)60 self.sleep(1) # Add more time to make sure downloads have completed61 # Get the actual size of the downloaded files (in bytes)62 whl_path = self.get_path_of_downloaded_file(whl_file)63 with open(whl_path, "rb") as f:64 whl_file_bytes = len(f.read())65 print("\n%s | Download = %s bytes." % (whl_file, whl_file_bytes))66 tar_gz_path = self.get_path_of_downloaded_file(tar_gz_file)67 with open(tar_gz_path, "rb") as f:68 tar_gz_file_bytes = len(f.read())69 print("%s | Download = %s bytes." % (tar_gz_file, tar_gz_file_bytes))70 # Check to make sure the downloaded files are not empty or too small71 self.assert_true(whl_file_bytes > 5000)72 self.assert_true(tar_gz_file_bytes > 5000)73 # Get file sizes in kB to compare actual values with displayed values...
__init__.py
Source:__init__.py
1# Copyright (c) 2021 Red Hat, Inc.2#3# All Rights Reserved.4#5# Licensed under the Apache License, Version 2.0 (the "License"); you may6# not use this file except in compliance with the License. You may obtain7# a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14# License for the specific language governing permissions and limitations15# under the License.16from __future__ import absolute_import17from tobiko.shell.curl import _execute18from tobiko.shell.curl import _process19execute_curl = _execute.execute_curl20CurlHeader = _process.CurlHeader21CurlProcessFixture = _process.CurlProcessFixture22assert_downloaded_file = _process.assert_downloaded_file23download_file = _process.download_file24default_download_dir = _process.default_download_dir...
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!!