At least I have what I wanted (following code work for me, I just simplified some really specific parts):
class Session(object): #pages factory == site builder
def __init__(self, driver):
self.driver = driver
def navigateInstance(self, instance):
self.driver.get(...)
return self.instance_page()
def instance_page(self):
PageFactory.create_page(InstancePage.InstancePage, self.driver)
def skipIfTurnedOff(testcase):
if str(WTF_CONFIG_READER.get("selenium.on", False)).lower() not in ("yes", "true", "t", "1"): #need weird conversions to support string env variables as bool
name = unicode(type(testcase).__name__) + u"_" + unicode(testcase._testMethodName)
raise unittest.SkipTest(name+"skipped due to selenium turned off")
def withSessionBrowser():
def wrapper(func):
@wraps(func)
def wrapped_func(*args, **kwargs):
self = args[0]
skipIfTurnedOff(self)
self.webdriver_manager = WTF_WEBDRIVER_MANAGER
driver = WTF_WEBDRIVER_MANAGER.new_driver()
capture = CaptureScreenShot.CaptureScreenShot() #almost as watcher
if not driver.current_url.startswith(my_props['tenant']): #scala play manipulations
driver.get(my_props['tenant']+'/404')
cook = platform.auth.cookies['PLAY_SESSION']
driver.add_cookie({'name' :'PLAY_SESSION', 'value': cook})
session = Session(driver)
kwargs.update({"session": session}) #magic line to set session
try:
func(*args, **kwargs)
except AssertionError as e:
capture.on_test_failure(self, None, e) #I just change format a bit, and do not rely on inheritance from unittest2
raise e
except Exception as e:
capture.on_test_error(self, None, e)
raise e
finally:
do_and_ignore(lambda: WTF_WEBDRIVER_MANAGER.close_driver())
return wrapped_func
return wrapper
class MyDummyTest(MyTestCase): #MyTestCase used as simplification
@instance(byApplication=name) #injects instance to test param, project specific decorator for api
@withSessionBrowser() #as shown above injects session to test param
def test_browser(self, instance, session):
session.navigateInstance(instance)
assert False #to check screenshot ability
@instance(byApplication=name) #injects instance to test param
def test_without_browser(self, instance):
#some asserts with instance
assert True
test_browser is uses browser, but test_without_browser doesn't need browser at all.
If I set WTF_selenium_on=False, I will have only test_without_browser executed and test_browser ignored.
I think this should provide explanation on a bit unusual usage.
P.S. Btw, what is the philosophy to validate page in constructor?
Regards,
Dmytro