From 3d6f289a28f5e523dfc867d5c3f142e20dbb9745 Mon Sep 17 00:00:00 2001 From: Kirikami Date: Tue, 25 May 2021 12:39:07 +0900 Subject: [PATCH] JEN-001: Added yaml config file and os usage as priority --- config/data.yaml | 10 +++++++++ conftest.py | 12 +++++++++-- requirements.txt | 3 ++- utils/config.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 config/data.yaml create mode 100644 utils/config.py diff --git a/config/data.yaml b/config/data.yaml new file mode 100644 index 0000000..ca6d6aa --- /dev/null +++ b/config/data.yaml @@ -0,0 +1,10 @@ +common: +environment: + local: + url: http://localhost:8080/ + username: user + password: 12345678 + travis: + url: http://localhost:8080/ + username: user + password: 12345678 \ No newline at end of file diff --git a/conftest.py b/conftest.py index b6d21b0..61c9f18 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,4 @@ +import os import pytest import requests from requests.exceptions import ConnectionError @@ -9,6 +10,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from utils.config import Config REMOTE_URL = 'http://127.0.0.1:4444/wd/hub' @@ -65,10 +67,16 @@ def init_driver(request): print('Please pass the correct browser name: {}'.format(request.param)) raise Exception('driver is not found') + config = Config() + url = os.getenv('URL') if os.getenv('URL', None) else config.url + username = os.getenv('USERNAME') if os.getenv('USERNAME', None) else config.username + password = os.getenv('PASSWORD') if os.getenv('PASSWORD', None) else config.password + + driver.get(url) driver.implicitly_wait(5) driver.get(TD.BASE_URL) - WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, 'j_username'))).send_keys(TD.LOGIN) - driver.find_element(By.CSS_SELECTOR, 'input[name="j_password"]').send_keys(TD.PASSWORD) + WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, 'j_username'))).send_keys(username) + driver.find_element(By.CSS_SELECTOR, 'input[name="j_password"]').send_keys(password) driver.find_element(By.CSS_SELECTOR, 'input[name="Submit"]').click() request.cls.driver = driver diff --git a/requirements.txt b/requirements.txt index 24e9521..caf1864 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pytest selenium -webdriver_manager \ No newline at end of file +webdriver_manager +strictyaml \ No newline at end of file diff --git a/utils/config.py b/utils/config.py new file mode 100644 index 0000000..6e92f05 --- /dev/null +++ b/utils/config.py @@ -0,0 +1,53 @@ +import os +import strictyaml as yaml +from collections import namedtuple + + +class Config: + """ + Class for initializing config + """ + + config_file = os.environ.get('CONFIG', 'data/config.yaml').lower() + default_config = os.path.join(os.path.dirname(os.path.abspath(__file__)), config_file) + + @staticmethod + def get_yaml(file: str): + # Check that file exists in specified path + if not os.path.exists(file): + print(f'Config file not found: {file}') + raise Exception('Config file not found') + # Reading file + f = open(file, 'r').read() + print(f'Loading file: {file}') + conf = yaml.load(f) + return conf.data + + @staticmethod + def get_config(file: str = default_config): + """ + Method for parsing config file + :param file: path to config + :return: config object with the same structure as yaml file + """ + # Getting environment variable for running tests, if non set up default will be set to local + env = os.environ.get('ENV', 'local').lower() + # Allowing to set staging env values to stage, stg or staging + if env.lower() == 'travis': + env = 'travis' + # If not proper config specified + print(f'Picking environment: {env}') + print(f'Loading config file: {file}') + # Loading yaml file + conf = Config.get_yaml(file) + # Setting up local config for appropriate environment + env_config = conf['environment'][env.lower()] + + return Config.get_object_from_dict(env_config), env + + @staticmethod + def get_object_from_dict(dictionary): + for k, v in dictionary.items(): + if isinstance(v, dict): + dictionary[k] = Config.get_object_from_dict(v) + return namedtuple('object', dictionary.keys())(*dictionary.values())