-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Daksha Download & Read functionality. #59
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
""" | ||
|
||
import csv | ||
import glob | ||
import os | ||
import pathlib | ||
import traceback | ||
import time | ||
from selenium import webdriver | ||
|
@@ -24,13 +27,14 @@ | |
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.ui import WebDriverWait, Select | ||
from selenium.webdriver.support import expected_conditions as EC | ||
from daksha.settings import DOWNLOAD_PATH | ||
|
||
from .logs import logger | ||
from .models import TestExecutor | ||
from .utils.screenshot_utils import take_screenshot | ||
|
||
|
||
def browser_config(config) -> WebDriver: | ||
def browser_config(config,test_uuid) -> WebDriver: | ||
rchetan98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Configures the browser in accordance with mentioned specifications(env,browser,driverAddress) in YAML | ||
:param config: Browser configuration fetched from YAML | ||
|
@@ -44,18 +48,23 @@ def browser_config(config) -> WebDriver: | |
env = config['env'] | ||
brow = config['browser'] | ||
path = config['driverAddress'] | ||
download_path = os.path.join(DOWNLOAD_PATH, test_uuid) | ||
prefs = {'download.default_directory': download_path} | ||
except KeyError: | ||
raise Exception( | ||
"Ill formatted arguments, 'env', 'browser' and 'driverAddress' must be present in the list of args") | ||
if brow.lower() == 'chrome' and env.lower() == 'local': | ||
web_driver = webdriver.Chrome(executable_path=path) | ||
options = webdriver.ChromeOptions() | ||
options.add_experimental_option('prefs', prefs) | ||
web_driver = webdriver.Chrome(executable_path=path, options=options) | ||
elif brow.lower() == 'chrome' and env.lower() == 'remote': | ||
options = webdriver.ChromeOptions() | ||
options.add_argument("no-sandbox") | ||
options.add_argument("--disable-gpu") | ||
options.add_argument("--window-size=800,600") | ||
options.add_argument("--disable-dev-shm-usage") | ||
options.add_argument("--start-maximized") | ||
options.add_experimental_option('prefs', prefs) | ||
web_driver = webdriver.Remote( | ||
command_executor=path, | ||
desired_capabilities=DesiredCapabilities.CHROME, | ||
|
@@ -520,3 +529,68 @@ def scroll_to(test_executor: TestExecutor, **kwargs): | |
logger.error("Attempt " + str(i) + " to scroll to element failed") | ||
return False, error_stack | ||
|
||
def download_file(test_executor: TestExecutor, **kwargs): | ||
locator, locator_value = get_locator_info(**kwargs) | ||
test_uuid = test_executor.test_uuid | ||
wait = True | ||
download_path = os.path.join(DOWNLOAD_PATH, test_uuid) | ||
try: | ||
logger.info("Going to download file") | ||
element = WebDriverWait(test_executor.web_driver, 10).until( | ||
EC.visibility_of_element_located((locator, locator_value)) | ||
) | ||
element.click() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If download confirmation page is present thenn this won't work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explore browser settings |
||
logger.info("Downloading File....") | ||
time.sleep(2) | ||
# waiting for download to complete | ||
while wait: | ||
files = glob.glob(download_path + "/*") | ||
downloaded_file = max(files, key=os.path.getctime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break if 2 tests with same uuid (in case of folder execution) runs and download_file is run at the same time |
||
file_extension = pathlib.Path(downloaded_file).suffix | ||
if file_extension.lower() == ".crdownload" or file_extension.lower() == ".part": | ||
wait = True | ||
time.sleep(1) | ||
else: | ||
wait = False | ||
# Printing file name & download path | ||
file_name = pathlib.Path(downloaded_file).stem | ||
file_downloaded = file_name+file_extension | ||
logger.info("File "+file_downloaded+" downloaded in Daksha/"+test_uuid+" downloads folder") | ||
rchetan98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# saving downloaded file in save_in variable | ||
if 'save_in' in kwargs.keys(): | ||
save_in = kwargs['save_in'] | ||
test_executor.variable_dictionary[save_in] = downloaded_file | ||
logger.info("File saved in: "+save_in+". Can be used for reading file ") | ||
rchetan98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else : | ||
logger.info("No SAVE_IN key passed. Read file won't be supported for this download") | ||
except Exception as e: | ||
logger.error(traceback.format_exc()) | ||
return False, None | ||
return True, None | ||
|
||
def read_file(test_executor: TestExecutor, **kwargs): | ||
rchetan98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Reading text & CSV files | ||
try: | ||
save_in = kwargs['read_from'] | ||
rchetan98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except KeyError: | ||
return False, "Ill formatted arguments, 'save_in' must be present in the list of args" | ||
file = test_executor.variable_dictionary[save_in] | ||
logger.info("Read file: "+file) | ||
file_extension = pathlib.Path(file).suffix | ||
if file_extension == '.txt': | ||
logger.info("Reading Text File: \n") | ||
with open(file) as f: | ||
contents = f.read() | ||
logger.info(contents) | ||
rchetan98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f.close() | ||
elif file_extension == '.csv': | ||
logger.info("Reading CSV File: \n") | ||
with open(file) as f: | ||
csvFile = csv.reader(f) | ||
for lines in csvFile: | ||
logger.info(lines) | ||
f.close() | ||
else: | ||
logger.info("Daksha currently only support txt & csv files readability !") | ||
return True, None | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
config: | ||
env: local | ||
browser: chrome | ||
driverAddress: /Users/wayne/Desktop/chromedriver | ||
name: downloadTest | ||
task: | ||
- open_url: | ||
url: https://wsform.com/knowledgebase/sample-csv-files/ | ||
- wait_for: | ||
mode: hardwait | ||
value: 1 | ||
- download_file: | ||
xpath: //a[contains(text(),'month.csv')] | ||
save_in: downloadF | ||
- download_file: | ||
xpath: //a[contains(text(),'time.csv')] | ||
- read_file: | ||
read_from: downloadF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Download_path value here