How to use _get_selector_obj method in Airtest

Best Python code snippet using Airtest Github


Full Screen

...142        assert_value = False if self.assertion == 'assert_not_' else True143        if self.assertion:144            action = action_stack[-1]145            assert_result = self.obj == assert_value146            prev_selector_obj = self._get_selector_obj()147            # 必须预先判断所选对象是否存在,不存在的对象调用info属性时会抛出uiautomator.UiObjectNotFoundException148            # 因为即使是assert_not_checked时,也是可以获取对象info的149            if prev_selector_obj.obj.exists:150                uiobj = prev_selector_obj.obj.info151            else:152                uiobj = None153            assert_action = self.assertion + action154            genlog('assert', [assert_action, prev_selector_obj.selectors, assert_result] + list(args), uiobj)155            if not assert_result:156                try:157                    raise AutomatorAssertionFail('assert failed of {}, require {}, got {}'.format(assert_action, assert_value, self.obj))158                except AutomatorAssertionFail:159                    log_error('assert', traceback.format_exc(), assert_action, prev_selector_obj.selectors, assert_result, *args, **kwargs)160                    raise161            return None162        # handle selector expr163        if args or kwargs:164            if all([k in SELECTOR_ARGS for k in kwargs]):165                # get this select action name166                if len(action_stack) >= 1:167                    action = action_stack[-1]168                else:169                    action = 'global'170                # relative selector171                prev_selector_obj = self._get_selector_obj()172                if prev_selector_obj:173                    uiobj = try_getattr(prev_selector_obj.obj, 'info', prev_selector_obj.select_action, prev_selector_obj.selectors)174                    genlog('select', [prev_selector_obj.select_action, prev_selector_obj.selectors], uiobj)175                calling = try_call(self.obj, action, *args, **kwargs)176                ret = self.__class__(calling, self)177                ret.selectors = (args, kwargs) if args else kwargs178                ret.select_action = action179                return ret180        # 其他操作的log记录181        action, params = None, None182        if args or kwargs:183            last_log = list(args) or kwargs.values()184        else:185            last_log = []186        if len(action_stack) >= 3 and action_stack[-3] in TERTIARY_ACTION:187            taction = TERTIARY_ACTION[action_stack[-3]]188            if action_stack[-2] in taction:189                saction = taction[action_stack[-2]]190                if action_stack[-1] in saction:191                    action = action_stack[-3]192                    params = action_stack[-2:] + last_log193        if len(action_stack) >= 2 and action_stack[-2] in SECONDARY_ACTIONS:194            secondary_action = SECONDARY_ACTIONS[action_stack[-2]]195            if action_stack[-1] in secondary_action:196                action = action_stack[-2]197                params = action_stack[-1:] + last_log198        if len(action_stack) >= 1 and action_stack[-1] in PRIMARY_ACTION:199            action = action_stack[-1]200            params = last_log201        if action:202            # select操作log记录203            selector_obj = self._get_selector_obj()204            uiobj = None205            if selector_obj:206                uiobj = try_getattr(selector_obj.obj, 'info', selector_obj.select_action, selector_obj.selectors)207                genlog('select', [selector_obj.select_action, selector_obj.selectors], uiobj)208            genlog(action, params, uiobj)209        if not calling:210            calling = try_call(self.obj, 'action', *args, **kwargs)211        return self.__class__(calling, self)212    def __len__(self):213        return len(self.obj)214    def __nonzero__(self):215        """ python 2 only, for python 3, please override __bool__216        """217        return bool(self.obj)218    def __getitem__(self, item):219        return self.__class__(self.obj[item], self)220    def __iter__(self):221        objs, length = self, len(self)222        class Iter(object):223            def __init__(self):224                self.index = -1225            def next(self):226                self.index += 1227                if self.index < length:228                    return objs[self.index]229                else:230                    raise StopIteration()231            __next__ = next232        return Iter()233    def _get_selector_obj(self):234        # 下面的is not None的判断方法有点特殊235        # 因为该类实现了bool隐式转换接口,如果不这样判断的话,在and表达式返回时,还会自动bool隐式转换一次236        # 导致条件判断非预期237        ret = None238        if self.selectors:239            ret = self240        elif (self.parent and self.parent.selectors) is not None:241            ret = self.parent242        elif (self.parent and self.parent.parent and self.parent.parent.selectors) is not None:243            ret = self.parent.parent244        elif (self.parent and self.parent.parent and self.parent.parent.parent and self.parent.parent.parent.selectors) is not None:245            ret = self.parent.parent.parent246        return ret247    def end(self):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?