From 99ff7fb81c97c45c96d0df105fd0b3ee329c4ae1 Mon Sep 17 00:00:00 2001 From: sepi Date: Fri, 17 Feb 2017 15:36:47 +0200 Subject: [PATCH 1/2] env config set added --- selene/config.py | 35 ++++++++++++++++++++++++----------- selene/helpers.py | 7 +++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/selene/config.py b/selene/config.py index a8534998..16efcafb 100644 --- a/selene/config.py +++ b/selene/config.py @@ -4,24 +4,37 @@ import time from selene.browsers import Browser +from selene.helpers import env -timeout = 4 -poll_during_waits = 0.1 -app_host = '' +SELENE_TIMEOUT = "selene_timeout" +SELENE_POOLING_INTERVAL = "selene_polling_interval" +SELENE_BASE_URL = 'selene_base_url' +SELENE_BROWSER = 'selene_browser' +SELENE_START_MAXIMIZED = "selene_start_maximized" +SELENE_HOLD_BROWSER_OPEN = "selene_hold_browser_open" +SELENE_CACHE_ELEMENTS = "selene_cache_elements" +SELENE_SCREENSHOT_FOLDER = "selene_screenshot_folder" + +timeout = env(SELENE_TIMEOUT) or 4 +poll_during_waits = env(SELENE_POOLING_INTERVAL) or 0.1 +app_host = env(SELENE_BASE_URL) or '' # todo: make cashing work (currently will not work...) -cash_elements = False +cash_elements = env(SELENE_CACHE_ELEMENTS) == "True" or False """To cash all elements after first successful find config.cash_elements = True""" -browser_name = Browser.FIREFOX -maximize_window = True -hold_browser_open = False +browser_name = env(SELENE_BROWSER) or Browser.FIREFOX + +maximize_window = False if env(SELENE_START_MAXIMIZED) == "False" else True + +hold_browser_open = env(SELENE_HOLD_BROWSER_OPEN) == "True" or False + counter = itertools.count(start=int(round(time.time() * 1000))) -screenshot_folder = os.path.join(os.path.expanduser("~"), - ".selene", - "screenshots", - str(next(counter))) +screenshot_folder = env(SELENE_SCREENSHOT_FOLDER) or os.path.join(os.path.expanduser("~"), + ".selene", + "screenshots", + str(next(counter))) desired_capabilities = None diff --git a/selene/helpers.py b/selene/helpers.py index 418edb3f..98265bb1 100644 --- a/selene/helpers.py +++ b/selene/helpers.py @@ -50,3 +50,10 @@ def css_or_by_to_by(css_selector_or_by): if isinstance(css_selector_or_by, str): return (By.CSS_SELECTOR, css_selector_or_by) raise TypeError('css_selector_or_by should be str with CSS selector or Tuple[by:str, value:str]') + + +def env(key): + try: + return os.environ[key] + except KeyError: + return None From 171a77f542c7411466c591d045512b6436d77f90 Mon Sep 17 00:00:00 2001 From: Iakiv Kramarenko Date: Wed, 22 Feb 2017 19:21:29 -0500 Subject: [PATCH 2/2] #51: simplified implementation, renamed some sys variables names; deprecated config.app_host, now the config.base_url should be used --- CHANGELOG.md | 5 ++++- selene/config.py | 40 ++++++++++++++++++---------------------- selene/tools.py | 8 +++++--- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae4f387..2ea0db6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,10 @@ ## 1.0.0ax (next from master branch) - naming changes: - - tbd... + - tbd + - upcoming breaking changes: + - selene.config.app_host renamed to selene.config.base_url + - selene.config.app_host still works but will be removed in next versions ## 1.0.0a8 (released 16.02.2017) - new features added diff --git a/selene/config.py b/selene/config.py index 16efcafb..aca3cbc9 100644 --- a/selene/config.py +++ b/selene/config.py @@ -1,4 +1,4 @@ -# todo: make the properties also "object oriented" to support different configs per different SeleneDriver instances +# todo: make the properties also 'object oriented' to support different configs per different SeleneDriver instances import itertools import os import time @@ -6,35 +6,31 @@ from selene.browsers import Browser from selene.helpers import env -SELENE_TIMEOUT = "selene_timeout" -SELENE_POOLING_INTERVAL = "selene_polling_interval" -SELENE_BASE_URL = 'selene_base_url' -SELENE_BROWSER = 'selene_browser' -SELENE_START_MAXIMIZED = "selene_start_maximized" -SELENE_HOLD_BROWSER_OPEN = "selene_hold_browser_open" -SELENE_CACHE_ELEMENTS = "selene_cache_elements" -SELENE_SCREENSHOT_FOLDER = "selene_screenshot_folder" -timeout = env(SELENE_TIMEOUT) or 4 -poll_during_waits = env(SELENE_POOLING_INTERVAL) or 0.1 -app_host = env(SELENE_BASE_URL) or '' +timeout = env('selene_timeout') or 4 +poll_during_waits = env('selene_poll_during_waits') or 0.1 + +base_url = env('selene_base_url') or '' +app_host = None +# todo: we may probably refactor selene.config to selene.browser.config where config - is an object, not a module +# todo: then it would be better to add warnings.warn("use base_url instead", DeprecationWarning) # todo: make cashing work (currently will not work...) -cash_elements = env(SELENE_CACHE_ELEMENTS) == "True" or False -"""To cash all elements after first successful find - config.cash_elements = True""" +cash_elements = env('selene_cache_elements') == 'True' or False +'''To cash all elements after first successful find + config.cash_elements = True''' -browser_name = env(SELENE_BROWSER) or Browser.FIREFOX +browser_name = env('selene_browser_name') or Browser.FIREFOX -maximize_window = False if env(SELENE_START_MAXIMIZED) == "False" else True +maximize_window = False if env('selene_maximize_window') == 'False' else True -hold_browser_open = env(SELENE_HOLD_BROWSER_OPEN) == "True" or False +hold_browser_open = env('selene_hold_browser_open') == 'True' or False counter = itertools.count(start=int(round(time.time() * 1000))) -screenshot_folder = env(SELENE_SCREENSHOT_FOLDER) or os.path.join(os.path.expanduser("~"), - ".selene", - "screenshots", - str(next(counter))) +screenshot_folder = env('selene_screenshot_folder') or os.path.join(os.path.expanduser('~'), + '.selene', + 'screenshots', + str(next(counter))) desired_capabilities = None diff --git a/selene/tools.py b/selene/tools.py index cca6401e..06c34b65 100644 --- a/selene/tools.py +++ b/selene/tools.py @@ -31,18 +31,20 @@ def visit(absolute_or_relative_url): """ Loads a web page in the current browser session. :param absolute_or_relative_url: - an absolute url to web page in case of config.app_host is not specified, + an absolute url to web page in case of config.base_url is not specified, otherwise - relative url correspondingly :Usage: visit('http://mydomain.com/subpage1') visit('http://mydomain.com/subpage2') # OR - config.app_host = 'http://mydomain.com' + config.base_url = 'http://mydomain.com' visit('/subpage1') visit('/subpage2') """ - get_driver().get(selene.config.app_host + absolute_or_relative_url) + # todo: refactor next line when app_host is removed + base_url = selene.config.app_host if selene.config.app_host else selene.config.base_url + get_driver().get(base_url + absolute_or_relative_url) def s(css_selector_or_by):