Best Python code snippet using autotest_python
decorators.py
Source:decorators.py  
...40    def decorator(test_item):41        if not isinstance(test_item, types.FunctionType):42            raise Exception('Decorator only supported for functions')43        @functools.wraps(test_item)44        def skip_wrapper(self, *args, **kwargs):45            with self.marionette.using_context('chrome'):46                multi_process_browser = not self.marionette.execute_script("""47                    try {48                      return Services.appinfo.browserTabsRemoteAutostart;49                    } catch (e) {50                      return false;51                    }52                """)53                if multi_process_browser:54                    raise SkipTest(reason)55            return test_item(self, *args, **kwargs)56        return skip_wrapper57    return decorator58def run_if_manage_instance(reason):59    """Decorator which runs a test if Marionette manages the application instance."""60    def decorator(test_item):61        if not isinstance(test_item, types.FunctionType):62            raise Exception('Decorator only supported for functions')63        @functools.wraps(test_item)64        def skip_wrapper(self, *args, **kwargs):65            if self.marionette.instance is None:66                raise SkipTest(reason)67            return test_item(self, *args, **kwargs)68        return skip_wrapper69    return decorator70def skip_if_chrome(reason):71    """Decorator which skips a test if chrome context is active."""72    def decorator(test_item):73        if not isinstance(test_item, types.FunctionType):74            raise Exception('Decorator only supported for functions')75        @functools.wraps(test_item)76        def skip_wrapper(self, *args, **kwargs):77            if self.marionette._send_message('getContext', key='value') == 'chrome':78                raise SkipTest(reason)79            return test_item(self, *args, **kwargs)80        return skip_wrapper81    return decorator82def skip_if_desktop(reason):83    """Decorator which skips a test if run on desktop."""84    def decorator(test_item):85        if not isinstance(test_item, types.FunctionType):86            raise Exception('Decorator only supported for functions')87        @functools.wraps(test_item)88        def skip_wrapper(self, *args, **kwargs):89            if self.marionette.session_capabilities.get('browserName') == 'firefox':90                raise SkipTest(reason)91            return test_item(self, *args, **kwargs)92        return skip_wrapper93    return decorator94def skip_if_e10s(reason):95    """Decorator which skips a test if e10s mode is active."""96    def decorator(test_item):97        if not isinstance(test_item, types.FunctionType):98            raise Exception('Decorator only supported for functions')99        @functools.wraps(test_item)100        def skip_wrapper(self, *args, **kwargs):101            with self.marionette.using_context('chrome'):102                multi_process_browser = self.marionette.execute_script("""103                    try {104                      return Services.appinfo.browserTabsRemoteAutostart;105                    } catch (e) {106                      return false;107                    }108                """)109                if multi_process_browser:110                    raise SkipTest(reason)111            return test_item(self, *args, **kwargs)112        return skip_wrapper113    return decorator114def skip_if_mobile(reason):115    """Decorator which skips a test if run on mobile."""116    def decorator(test_item):117        if not isinstance(test_item, types.FunctionType):118            raise Exception('Decorator only supported for functions')119        @functools.wraps(test_item)120        def skip_wrapper(self, *args, **kwargs):121            if self.marionette.session_capabilities.get('browserName') == 'fennec':122                raise SkipTest(reason)123            return test_item(self, *args, **kwargs)124        return skip_wrapper125    return decorator126def skip_unless_browser_pref(reason, pref, predicate=bool):127    """Decorator which skips a test based on the value of a browser preference.128    :param reason: Message describing why the test need to be skipped.129    :param pref: the preference name130    :param predicate: a function that should return false to skip the test.131                      The function takes one parameter, the preference value.132                      Defaults to the python built-in bool function.133    Note that the preference must exist, else a failure is raised.134    Example: ::135      class TestSomething(MarionetteTestCase):136          @skip_unless_browser_pref("Sessionstore needs to be enabled for crashes",137                                    "browser.sessionstore.resume_from_crash",138                                    lambda value: value is True,139                                    )140          def test_foo(self):141              pass  # test implementation here142    """143    def decorator(test_item):144        if not isinstance(test_item, types.FunctionType):145            raise Exception('Decorator only supported for functions')146        if not callable(predicate):147            raise ValueError('predicate must be callable')148        @functools.wraps(test_item)149        def skip_wrapper(self, *args, **kwargs):150            value = self.marionette.get_pref(pref)151            if value is None:152                self.fail("No such browser preference: {0!r}".format(pref))153            if not predicate(value):154                raise SkipTest(reason)155            return test_item(self, *args, **kwargs)156        return skip_wrapper157    return decorator158def skip_unless_protocol(reason, predicate):159    """Decorator which skips a test if the predicate does not match the current protocol level."""160    def decorator(test_item):161        if not isinstance(test_item, types.FunctionType):162            raise Exception('Decorator only supported for functions')163        if not callable(predicate):164            raise ValueError('predicate must be callable')165        @functools.wraps(test_item)166        def skip_wrapper(self, *args, **kwargs):167            level = self.marionette.client.protocol168            if not predicate(level):169                raise SkipTest(reason)170            return test_item(self, *args, **kwargs)171        return skip_wrapper172    return decorator173def with_parameters(parameters):174    """Decorator which generates methods given a base method and some data.175    Acts like :func:`parameterized`, but define all methods in one call.176    Example::177      # This example will generate two methods:178      #179      # - MyTestCase.test_it_1180      # - MyTestCase.test_it_2...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!!
