def __init__(self, *, name="anon", url=None, oauth_token=None, request_content_handler=None, headers=None, max_redirects=None, auth=None, proxy=None, _auto_session=True): self.__name = name self.__session = HttpSession( url=url, oauth_token=oauth_token, request_content_handler=request_content_handler, headers=headers, max_redirects=max_redirects, auth=auth, proxy=proxy, _auto_session=_auto_session) from arjuna import C if name == "anon": self.__root_dir = C("httpauto.dir") else: self.__root_dir = os.path.join(C("httpauto.dir"), "service/{}".format(name)) self.__endpoints = HttpEndPointLoader(self)
def __init__(self, *, url=None, oauth_token=None, request_content_handler=None, headers=None, max_redirects=None, auth=None, proxy=None, _auto_session=True): self.__url = url is not None and url.strip() or None self.__request_content_handler = request_content_handler from .http import Http if self.__request_content_handler is None: self.__request_content_handler = Http.content.urlencoded self.__session = None self.__provided_headers = headers if _auto_session: self._set_session(Session()) if max_redirects is not None: self.__session.max_redirects = max_redirects if auth is not None: self.__session.auth = auth if proxy is not None: self.__session.proxies = proxy else: from arjuna import C, Http if C("http.proxy.enabled"): self.__session.proxies = Http.proxy( C('http.proxy.host'), C('http.proxy.port')) if oauth_token: self.__session.headers['Authorization'] = f'Bearer {oauth_token}'
def add_env_data(cls, config): from arjuna import C, Arjuna import pkg_resources config._metadata['Arjuna Version'] = pkg_resources.require("arjuna")[0].version config._metadata['Arjuna Test Project Directory'] = C("project.root.dir") config._metadata['Arjuna Test Project Name'] = C("project.name") config._metadata['Reference Configuration'] = Arjuna.get_config().name config._metadata['Pytest Command (Converted)'] = Arjuna.get_pytest_command_for_group() config._metadata['Pytest Command (Provided)'] = Arjuna._get_command()
def __load_sessions_file(cls): from arjuna import C if cls.__SESSIONS_YAML is None: cls.SESSIONS_YAML_FILE = C(ArjunaOption.CONF_SESSIONS_LOCAL_FILE) if not os.path.isfile(cls.SESSIONS_YAML_FILE): cls.SESSIONS_YAML_FILE = C(ArjunaOption.CONF_SESSIONS_FILE) try: cls.__SESSIONS_YAML = Yaml.from_file(cls.SESSIONS_YAML_FILE, allow_any=True) except FileNotFoundError as e: raise TestSessionsFileNotFoundError(file_path=cls.SESSIONS_YAML_FILE)
def __load_groups_file(cls): from arjuna import C if cls.__GROUPS_YAML is None: cls.GROUPS_YAML_FILE = C(ArjunaOption.CONF_GROUPS_LOCAL_FILE) if not os.path.isfile(cls.GROUPS_YAML_FILE): cls.GROUPS_YAML_FILE = C(ArjunaOption.CONF_GROUPS_FILE) try: cls.__GROUPS_YAML = Yaml.from_file(cls.GROUPS_YAML_FILE, allow_any=True) except FileNotFoundError: raise TestGroupsFileNotFoundError(file_path=cls.GROUPS_YAML_FILE)
def __get_group_yaml(cls, name): from arjuna import C gfile = C(ArjunaOption.CONF_GROUPS_LOCAL_FILE) if not os.path.isfile(gfile): gfile = C(ArjunaOption.CONF_GROUPS_FILE) try: gyaml = Yaml.from_file(gfile, allow_any=True) except FileNotFoundError: raise TestGroupsFileNotFoundError(file_path=gfile) try: return gyaml.get_section(name) except YamlUndefinedSectionError as e: raise UndefinedTestGroupError(name=name, file_path=gfile)
def import_name_in_arj_hook_module(*, mod_name, name, optional=False): from arjuna import C mod = import_arj_hook_module(mod_name) return import_name_in_module(mod_name=mod_name, name=name, prefix=C("hooks.package"), optional=optional)
def from_file(cls, session, msg_file_name, **fargs): # Process Yaml file from arjuna import C, Yaml file_path = os.path.join(C("httpauto.message.dir"), msg_file_name + ".yaml") f = open(file_path, "r") msg_yaml = f.read() f.close() from arjuna.core.fmt import arj_format_str, arj_convert # Convert Arjuna custom objects to raw Python objects before formatting. fargs = {k: arj_convert(v) for k, v in fargs.items()} msg_yaml = arj_format_str(msg_yaml, tuple(), fargs) msg_yaml = Yaml.from_str(msg_yaml, allow_any=True) if msg_yaml is None: return cls.root(session) if "request" in msg_yaml: req_repr = HttpRequestYamlRepr( session, CIStringDict(msg_yaml["request"].as_map())) del msg_yaml["request"] else: req_repr = HttpRequestYamlRepr(session, CIStringDict()) resp_proc = HttpResponseYamlRepr(session, CIStringDict(msg_yaml.as_map())) return HttpMessage(session, req_repr, resp_proc)
def __load_sessions_file(cls): from arjuna import C if cls.__SESSIONS_YAML is None: cls.SESSIONS_YAML_FILE = C(ArjunaOption.CONF_SESSIONS_FILE) try: cls.__SESSIONS_YAML = Yaml.from_file(cls.SESSIONS_YAML_FILE) except FileNotFoundError as e: raise TestSessionsFileNotFoundError(cls.SESSIONS_YAML_FILE)
def __load_stages_file(cls): from arjuna import C if cls.__STAGES_YAML is None: cls.STAGES_YAML_FILE = C(ArjunaOption.CONF_STAGES_FILE) try: cls.__STAGES_YAML = Yaml.from_file(cls.STAGES_YAML_FILE) except FileNotFoundError: raise TestStagesFileNotFoundError(cls.STAGES_YAML_FILE)
def __load_groups_file(cls): from arjuna import C if cls.__GROUPS_YAML is None: cls.GROUPS_YAML_FILE = C(ArjunaOption.CONF_GROUPS_FILE) try: cls.__GROUPS_YAML = Yaml.from_file(cls.GROUPS_YAML_FILE) except FileNotFoundError: raise TestGroupsFileNotFoundError(cls.GROUPS_YAML_FILE)
def launch(self, config): self.__config = config from .browser_launcher import BrowserLauncher svc_url = config["arjuna_options"]["SELENIUM_SERVICE_URL"] driver_download = config["arjuna_options"]["SELENIUM_DRIVER_DOWNLOAD"] browser_name = config["arjuna_options"]["BROWSER_NAME"] driver_path = config["arjuna_options"]["SELENIUM_DRIVER_PATH"] if svc_url.lower() == "not_set": from arjuna.tpi.constant import BrowserName driver_service = None driver_downloader = None if browser_name == BrowserName.CHROME: from selenium.webdriver.chrome.service import Service driver_service = Service from webdriver_manager.chrome import ChromeDriverManager driver_downloader = ChromeDriverManager elif browser_name == BrowserName.FIREFOX: from selenium.webdriver.firefox.service import Service driver_service = Service from webdriver_manager.firefox import GeckoDriverManager driver_downloader = GeckoDriverManager if driver_download: driver_path = driver_downloader().install() self.__driver_service = Service(driver_path) self.__driver_service.start() svc_url = self.__driver_service.service_url else: if not svc_url.lower().endswith("/wd/hub"): svc_url += "/wd/hub" # BrowserMob from arjuna import Arjuna bmproxy_server = Arjuna._get_bmproxy_server() if bmproxy_server is not None: self.__proxy = bmproxy_server.create_proxy() else: from arjuna import C if C("http.proxy.enabled"): self.__proxy = HttpProxy(C('http.proxy.host'), C('http.proxy.port')) self.__driver = BrowserLauncher.launch(config, svc_url=svc_url, proxy=self.__proxy)
def __init__(self, *, host=None, port=None, user=None, password=None, use_ssl=None): from arjuna import C cport = C("emailauto.imap.port") self.__host = host is None and C("emailauto.imap.host") or host if use_ssl is None: self.__use_ssl = C("emailauto.imap.usessl") else: self.__use_ssl = use_ssl if port is None: if cport != "not_set": self.__port = cport else: self.__port = self.__use_ssl and 993 or 143 self.__user = user and user or C("emailauto.user") if self.__user == "not_set": raise Exception( "One must provide IMAP server user in the constructor or in the reference configuration value for emailauto.user option." ) self.__password = password and password or C("emailauto.password") if self.__password == "not_set": raise Exception( "One must provide IMAP server password in the constructor or in the reference configuration value for emailauto.password option." ) self.__imap = None self.__connected = False self.__loggedin = False self.__mailbox = None self.__ready = False self.connect()
def file(cls, field_name, file_name, *, content_type='text/plain', headers=None): ''' Upload a file and send as multipart data. Content-Type is sent as the content type got from multipart encoding. ''' from arjuna import C, ArjunaOption file_path = os.path.join(C(ArjunaOption.DATA_FILE_DIR), file_name) encoder = MultipartEncoder({ field_name: (file_name, open(file_path, 'rb'), content_type, headers) }) return _HttpContent(content=encoder.to_string(), type=encoder.content_type)
def new_emails(self, *, sender=None, subject=None, content=None, max=5, max_wait=None): ''' New emails received since the previous saved state (saved using save_state call), as per provided optional filters. Keyword Arguments: sender: Filter with Email address from which email is received. subject: Filter with Case-insensitive sub-string in subject line. content: Filter with Case-insensitive sub-string in content. max: Maximum number of emails to be returned. Default is 5. max_wait: Maximum time in seconds to re-select mailbox or availablity of emails in the mailbox. Note: The wait is not a static wait and comes into play if no email meets the filtering criteria. When the logic finds the first matching email, it tries to find more, else the wait loop is exited. ''' from arjuna import C if max_wait is None: max_wait = C("emailauto.max.wait") return MailBoxConditions(self).ReceiveEmail(max=max, sender=sender, subject=subject, content=content, latest=True).wait(max_wait=max_wait)
def emails(self, *, sender=None, subject=None, content=None, max=5, latest=True): ''' Get Current Emails as per provided optional filters. Keyword Arguments: sender: Filter with Email address from which email is received. subject: Filter with Case-insensitive sub-string in subject line. content: Filter with Case-insensitive sub-string in content. max: Maximum number of emails to be returned. Default is 5. latest: If True, latest emails are considered. Default is True. Note: If no email is found in current emails that meets the filtering criteria, an automatic wait loop triggers and this method works just like new_emails method in such a context. When the logic finds the first matching email, it tries to find more, else the wait loop is exited. ''' from arjuna import C max_wait = C("emailauto.max.wait") MailBoxConditions(self).Select().wait(max_wait=max_wait) return MailBoxConditions(self).ReceiveEmail(max=max, sender=sender, subject=subject, content=content, consider_saved_state=False).wait(max_wait=max_wait)
def multipart(cls, *fields): ''' Send the provided HTTP fields as multipart data. Content-Type is sent as the content type got from multipart encoding. ''' from arjuna import C, ArjunaOption elist = list() for field in fields: if type(field) is dict: for k, v in field.items(): elist.append((k, str(v))) elif isinstance(field, _HttpField): if field.is_file: file_path = os.path.join(C(ArjunaOption.DATA_FILE_DIR), field.value) elist.append( (field.name, (field.value, open(file_path, 'rb'), field.content_type, field.headers))) else: elist.append((field.name, str(field.value))) encoder = MultipartEncoder(elist) return _HttpContent(content=encoder.to_string(), type=encoder.content_type)
def _init_arjuna(config, arg_dict): from arjuna import Arjuna from arjuna.configure.cli import CliArgsConfig cliconfig = CliArgsConfig(arg_dict) linked_projects = config.getoption("link.projects") if linked_projects is not None: out = [] for lp in linked_projects: if not os.path.isabs(lp): out.append(os.path.join(os.getcwd(), lp)) else: out.append(lp) linked_projects = out Arjuna.init(config.option.rootdir, run_id=config.getoption("run.id"), static_rid=config.getoption("static.rid"), linked_projects=linked_projects, arjuna_options=cliconfig.arjuna_options, user_options=cliconfig.user_options) from arjuna import C os.chdir(C("project.root.dir"))
def import_arj_resouce_hook_package(): from arjuna import C import_name_in_module(mod_name=C("hooks.config.package"), name='*', optional=True)
def set_report_title(cls, report): from arjuna import C report.title = "{} Automated Test Report".format( C("project.name").title())
def inject_arjuna_js(cls, prefix): from arjuna import C with open(C("arjuna.root.dir") + "/arjuna/res/arjuna.js", "r") as f: prefix += [html.script(raw(f.read()))] with open(C("arjuna.root.dir") + "/arjuna/res/arjuna.html", "r") as f: prefix += [raw(f.read())]
def select_tests(cls, pytest_items, pytest_config): ''' Select tests from items collected by pytest, based on Arjuna rules. Arguments: pytest_items: List of pytest `Item` objects. Each item represents a collected test function node. pytest_config: pytest Config object ''' from arjuna import log_debug def process_nodename(item): return item.name.split("[")[0] from arjuna import Arjuna from arjuna.core.error import ExclusionRuleMet, NoInclusionRuleMet from arjuna import C selector = Arjuna.get_test_selector() final_selection = [] deselected = [] qual_names = set() for item in pytest_items: nid = item.nodeid log_debug(f"Processing {nid} as collected by pytest") # if item.name.split("[")[0] == "test": # continue qual_name = None # For a test function # Root dir should be folder containing the module # E.g. check_config_03_create_conf.py::check_create_config[G] temp_full_path = os.path.join(pytest_config.rootdir, item.nodeid.split('::')[0]) full_dotted_notation = temp_full_path.replace("/", ".").replace( '\\', ".").replace(".py", "") full_dotted_notation = full_dotted_notation.replace("..", ".") project_name = C("project.name") project_index = full_dotted_notation.find(project_name + "." + "test") if project_index == -1: deselected.append(item) continue qual_name = full_dotted_notation[ project_index:] + "." + process_nodename(item) # if os.path.exists(temp_full_path): # if os.path.isfile(temp_full_path): # test_path_index = temp_full_path.find(os.path.join(C("project.root.dir"), "test")) # if test_path_index == -1: # continue # else: # dotted_root = str(pytest_config.rootdir).replace("/",".").replace('\\',"") # proj_suffix = dotted_root[dotted_root.find(project_name):] # qual_name = proj_suffix + "." + process_nodeid(item) + "." + process_nodename(item) # else: # qual_name = process_nodeid(item) + "." + process_nodename(item) # start_index = qual_name.find(project_name + "." + "test") # if start_index == -1: # if qual_name.startswith("test."): # qual_name = project_name + "." + qual_name # else: # deselected.append(item) # continue # else: # qual_name = qual_name[start_index:] try: selector.validate(Arjuna.get_test_meta_data(qual_name)) except (ExclusionRuleMet, NoInclusionRuleMet) as e: deselected.append(item) else: final_selection.append(item) if deselected: pytest_config.hook.pytest_deselected(items=deselected) pytest_items[:] = final_selection
def _send(self, request) -> HttpResponse: ''' Send the provided HttpRequest to server. In case of ConnectionError, retries the connection 5 times at a gap of 1 second. Currently, not configurable. Returns `HttpResponse` object. In case of redirections, this is the last HttpResponse object, which encapsulates all redirections which can be retrieved from it. ''' from arjuna import Arjuna, log_info, C from arjuna.tpi.helper.arjtype import NetworkPacketInfo log_info(request.label) max_connection_retries = 5 try: counter = 0 exc_flag = False exc_desc = None while counter < max_connection_retries: counter += 1 try: timeout = C("socket.timeout") if request.timeout is not None: timeout = request.timeout if self._session.proxies: response = HttpResponse( self, self._session.send( request._request, allow_redirects=request.allow_redirects, timeout=timeout, proxies=self._session.proxies, verify=False)) else: response = HttpResponse( self, self._session.send( request._request, allow_redirects=request.allow_redirects, timeout=timeout)) except (ProxyError, InvalidProxyURL) as e: raise HttpConnectError( request, "There is an error in connecting to the configured proxy. Proxy settings: {}. Error: {}" .format(self.__session.proxies, str(e))) except ConnectionError as f: exc_flag = True exc_desc = str(f) time.sleep(1) continue else: break if exc_flag: raise HttpConnectError( request, "Connection error despite trying 5 times. Error: {}". format(exc_desc)) except TooManyRedirects as e: response = HttpResponse(self._session, e.response) self.__register_network_info(request, response) raise HttpSendError( self, response, str(e) + ". Error redir URL: " + e.response.url) except Exception as e: import traceback response = "Error in sending the request\n" response += e.__class__.__name__ + ":" + str(e) + "\n" response += traceback.format_exc() Arjuna.get_report_metadata().add_network_packet_info( NetworkPacketInfo(label=request.label, request=str(request), response=str(response), sub_network_packets=tuple())) raise e else: self.__register_network_info(request, response) if request.xcodes is not None: self._validate_status_code(response, request.xcodes) return response
def import_arj_entity(name): from arjuna import C return import_name_in_module(mod_name=C("hooks.entity.package"), name=name, optional=False)
def import_arj_config_hook(name): from arjuna import C return import_name_in_module(mod_name=C("hooks.config.package"), name=name, optional=True)
def import_arj_hook_module(mod_name, optional=False): from arjuna import C return import_module(mod_name, prefix=C("hooks.package"), optional=optional)