Best Python code snippet using robotframework-pageobjects_python
page.py
Source:page.py  
...323            if self.browser == 'phantomjs':324                raise exceptions.MissingSauceOptionError("When running Sauce, browser option should not be phantomjs.")325        return sauce_desired326    @staticmethod327    def _vars_match_template(template, vars):328        """Validates that the provided variables match the template.329        :param template: The template330        :type template: str331        :param vars: The variables to match against the template332        :type vars: tuple or list333        :returns: bool"""334        keys = vars.keys()335        keys.sort()336        template_vars = list(uritemplate.variables(template))337        template_vars.sort()338        return template_vars == keys339    @not_keyword340    def _resolve_url(self, *args):341        """342        Figures out the URL that a page object should open at.343        Called by open().344        """345        pageobj_name = self.__class__.__name__346        # determine type of uri attribute347        if hasattr(self, 'uri'):348            uri_type = 'template' if re.search('{.+}', self.uri) else 'plain'349        else:350            raise exceptions.UriResolutionError("Page object \"%s\" must have a \"uri\" attribute set." % pageobj_name)351        # Don't allow absolute uri attribute.352        if self._is_url_absolute(self.uri):353            raise exceptions.UriResolutionError(354                "Page object \"%s\" must not have an absolute \"uri\" attribute set. Use a relative URL "355                "instead." % pageobj_name)356        # We always need a baseurl set. This enforces parameterization of the357        # domain under test.358        if self.baseurl is None:359            raise exceptions.UriResolutionError("To open page object, \"%s\" you must set a baseurl." % pageobj_name)360        elif len(args) > 0 and hasattr(self, "uri") and self._is_url_absolute(self.uri):361            # URI template variables are being passed in, so the page object encapsulates362            # a page that follows some sort of URL pattern. Eg, /pubmed/SOME_ARTICLE_ID.363            raise exceptions.UriResolutionError("The URI Template \"%s\" in \"%s\" is an absolute URL. "364                                                "It should be relative and used with baseurl" %365                                                (self.uri, pageobj_name))366        if len(args) > 0:367            # the user wants to open a non-default uri368            uri_vars = {}369            first_arg = args[0]370            if not self._is_robot:371                if isinstance(first_arg, basestring):372                    # In Python, if the first argument is a string and not a dict, it's a url or path.373                    arg_type = "url"374                else:375                    arg_type = "dict"376            elif self._is_robot:377                # We'll always get string args in Robot378                if self._is_url_absolute(first_arg) or first_arg.startswith("/"):379                    arg_type = "url"380                else:381                    arg_type = "robot"382            if arg_type != "url" and hasattr(self, "uri") and uri_type == 'plain':383                raise exceptions.UriResolutionError(384                    "URI %s is set for page object %s. It is not a template, so no arguments are allowed." %385                    (self.uri, pageobj_name))386            if arg_type == "url":387                if self._is_url_absolute(first_arg):388                    # In Robot, the first argument is always a string, so we need to check if it starts with "/" or a scheme.389                    # (We're not allowing relative paths right now._390                    return first_arg391                elif first_arg.startswith("//"):392                    raise exceptions.UriResolutionError("%s is neither a URL with a scheme nor a relative path"393                                                        % first_arg)394                else:  # starts with "/"395                    # so it doesn't need resolution, except for appending to baseurl and stripping leading slash396                    # if it needed: i.e., if the base url ends with "/" and the url starts with "/".397                    return re.sub("\/$", "", self.baseurl) + first_arg398            elif arg_type == "robot":399                # Robot args need to be parsed as "arg1=123", "arg2=foo", etc.400                for arg in args:401                    split_arg = arg.split("=")402                    uri_vars[split_arg[0]] = "=".join(split_arg[1:])403            else:404                # dict just contains the args as keys and values405                uri_vars = args[0]406            # Check that variables are correct and match template.407            if not self._vars_match_template(self.uri, uri_vars):408                raise exceptions.UriResolutionError(409                    "The variables %s do not match template %s for page object %s"410                    % (uri_vars, self.uri, pageobj_name)411                )412            self.uri_vars = uri_vars413            return uritemplate.expand(self.baseurl + self.uri, uri_vars)414        else:415            if uri_type == 'template':416                raise exceptions.UriResolutionError('%s has uri template %s , but no arguments were given to resolve it' %417                                                    (pageobj_name, self.uri))418            # the user wants to open the default uri419            return self.baseurl + self.uri420    @staticmethod421    def _is_url_absolute(url):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
