Exemple #1
0
    def _listener(self, evt):
        d = self.d

        # keep screenshot for every call
        if not evt.is_before and evt.flag == consts.EVENT_SCREENSHOT:
            self.__last_screenshot = evt.retval

        if evt.depth > 1: # base depth is 1
            return

        if evt.is_before: # call before function
            if evt.flag == consts.EVENT_CLICK:
                self.__last_screenshot = d.screenshot() # Maybe no need to set value here.
                (x, y) = evt.args
                cv_img = imutils.from_pillow(self.last_screenshot)
                cv_img = imutils.mark_point(cv_img, x, y)
                self.__last_screenshot = imutils.to_pillow(cv_img)
                self._add_to_gif(self.last_screenshot)
            return

        if evt.flag == consts.EVENT_CLICK:
            screen_before = self._save_screenshot(self.last_screenshot, name_prefix='before')
            screen_after = self._save_screenshot(name_prefix='after')

            (x, y) = evt.args
            self.add_step('click',
                screen_before=screen_before,
                screen_after=screen_after,
                position={'x': x, 'y': y})
        elif evt.flag == consts.EVENT_CLICK_IMAGE:
            kwargs = {
                'success': evt.traceback is None,
                'traceback': None if evt.traceback is None else evt.traceback.stack,
                'description': evt.kwargs.get('desc'),
            }
            # do not record if image not found and no trackback
            if evt.retval is None and evt.traceback is None:
                return
            
            # save before click image
            kwargs['screen_before'] = self._save_screenshot(self.last_screenshot, name_prefix='before')

            if evt.traceback is None or not isinstance(evt.traceback.exception, IOError):
                pattern = d.pattern_open(evt.args[0])
                kwargs['target'] = self._save_screenshot(pattern, name_prefix='target')
            if evt.traceback is None:
                # update image to add a click mark
                (x, y) = evt.retval.pos
                cv_img = imutils.from_pillow(self.last_screenshot)
                cv_img = imutils.mark_point(cv_img, x, y)
                self.__last_screenshot = imutils.to_pillow(cv_img)
                kwargs['screen_before'] = self._save_screenshot(self.last_screenshot, name=kwargs['screen_before'])

                kwargs['screen_after'] = self._save_screenshot(name_prefix='after')
                kwargs['confidence'] = evt.retval.confidence
                kwargs['position'] = {'x': x, 'y': y}
                
            self.add_step('click_image', **kwargs)
Exemple #2
0
 def __image_saver(self, name='', image=None):
     path = "%s/%s%s.png" % (self.image_path, name, time.time())
     if image is None:
         self.app.screen_image().save(path)
     elif isinstance(image, str) or isinstance(image, unicode):
         shutil.copyfile(image, path)
     elif isinstance(image, Image):
         image.save(path)
     elif isinstance(image, numpy.ndarray):
         imutils.to_pillow(image).save(path)
     return path.replace(self.path + '/', '')
Exemple #3
0
    def _uia_listener(self, evtjson):
        evt = json2obj(evtjson)
        if evt.name != '_click':
            return
        if evt.is_before:
            self.d.screenshot()
            self.__uia_last_position = center(evt.this.bounds)
        else:
            (x, y) = self.__uia_last_position
            # self.last_screenshot
            cv_last_img = imutils.from_pillow(self.last_screenshot)
            cv_last_img = imutils.mark_point(cv_last_img, x, y)
            screen = imutils.to_pillow(cv_last_img)
            screen_before = self._save_screenshot(screen=screen,
                                                  append_gif=True)
            # FIXME: maybe need sleep for a while
            screen_after = self._save_screenshot(append_gif=True)

            self.add_step('click',
                          screen_before=screen_before,
                          screen_after=screen_after,
                          position={
                              'x': x,
                              'y': y
                          })
Exemple #4
0
    def _uia_listener(self, evtjson):
        evt = json2obj(evtjson)
        if evt.name != '_click':
            return
        if evt.is_before:
            self.d.screenshot()
            self.__uia_last_position = center(evt.this.bounds)
        else:
            (x, y) = self.__uia_last_position
            # self.last_screenshot
            cv_last_img = imutils.from_pillow(self.last_screenshot)
            cv_last_img = imutils.mark_point(cv_last_img, x, y)
            screen = imutils.to_pillow(cv_last_img)
            screen_before = self._save_screenshot(screen=screen, name_prefix='click-before')
            # FIXME: maybe need sleep for a while
            screen_after = self._save_screenshot(name_prefix='click-after')

            self.add_step('click',
                screen_before=screen_before,
                screen_after=screen_after,
                position={'x': x, 'y': y})
Exemple #5
0
    def _listener(self, evt):
        d = self.d
        screen_before = 'images/before_%d.jpg' % time.time()
        screen_before_abspath = os.path.join(self.save_dir, screen_before)

        # keep screenshot for every call
        if not evt.is_before and evt.flag == consts.EVENT_SCREENSHOT:
            self.__last_screenshot = evt.retval

        if evt.depth > 1: # base depth is 1
            return

        if evt.is_before: # call before function
            if evt.flag == consts.EVENT_CLICK:
                self.__last_screenshot = d.screenshot() # Maybe no need to set value here.
                (x, y) = evt.args
                cv_img = imutils.from_pillow(self.last_screenshot)
                cv_img = imutils.mark_point(cv_img, x, y)
                self.__last_screenshot = imutils.to_pillow(cv_img)
                self._add_to_gif(self.last_screenshot)
            return

        if evt.flag == consts.EVENT_CLICK:
            if self.last_screenshot: # just in case
                self.last_screenshot.save(screen_before_abspath)
            screen_after = 'images/after_%d.jpg' % time.time()
            d.screenshot(os.path.join(self.save_dir, screen_after))

            (x, y) = evt.args
            self.add_step('click',
                screen_before=screen_before,
                screen_after=screen_after,
                position={'x': x, 'y': y})
        elif evt.flag == consts.EVENT_CLICK_IMAGE:
            kwargs = {
                'success': evt.traceback is None,
                'traceback': None if evt.traceback is None else evt.traceback.stack,
                'description': evt.kwargs.get('desc'),
            }
            # not record if image not found
            if evt.retval is None and evt.traceback is None:
                return
            
            if self.last_screenshot:
                self.last_screenshot.save(screen_before_abspath)
                kwargs['screen_before'] = screen_before
            if evt.traceback is None or not isinstance(evt.traceback.exception, IOError):
                target = 'images/target_%d.jpg' % time.time()
                pattern = d.pattern_open(evt.args[0])
                self._save_screenshot(pattern, name=target)
                kwargs['target'] = target
            if evt.traceback is None:
                (x, y) = evt.retval.pos
                # FIXME(ssx): quick hot fix
                cv_img = imutils.from_pillow(self.last_screenshot)
                cv_img = imutils.mark_point(cv_img, x, y)
                self.__last_screenshot = imutils.to_pillow(cv_img)
                self.last_screenshot.save(screen_before_abspath)
                
                screen_after = 'images/after_%d.jpg' % time.time()
                d.screenshot(os.path.join(self.save_dir, screen_after))
                kwargs['screen_after'] = screen_after
                kwargs['confidence'] = evt.retval.confidence
                kwargs['position'] = {'x': x, 'y': y}
            self.add_step('click_image', **kwargs)
        elif evt.flag == consts.EVENT_ASSERT_EXISTS: # this is image, not tested
            pattern = d.pattern_open(evt.args[0])
            target = 'images/target_%.2f.jpg' % time.time()
            self._save_screenshot(pattern, name=target)
            kwargs = {
                'target': target,
                'description': evt.kwargs.get('desc'),
                'screen': self._save_screenshot(name='images/screen_%.2f.jpg' % time.time()),
                'traceback': None if evt.traceback is None else evt.traceback.stack,
                'success': evt.traceback is None,
            }
            if evt.traceback is None:
                kwargs['confidence'] = evt.retval.confidence
                (x, y) = evt.retval.pos
                kwargs['position'] = {'x': x, 'y': y}
            self.add_step('assert_exists', **kwargs)
Exemple #6
0
    def _listener(self, evt):
        d = self.d

        # keep screenshot for every call
        if not evt.is_before and evt.flag == consts.EVENT_SCREENSHOT:
            self.__last_screenshot = evt.retval

        if evt.depth > 1:  # base depth is 1
            return

        if evt.is_before:  # call before function
            if evt.flag == consts.EVENT_CLICK:
                self.__last_screenshot = d.screenshot(
                )  # Maybe no need to set value here.
                (x, y) = evt.args
                cv_img = imutils.from_pillow(self.last_screenshot)
                cv_img = imutils.mark_point(cv_img, x, y)
                self.__last_screenshot = imutils.to_pillow(cv_img)
                self._add_to_gif(self.last_screenshot)
            return

        if evt.flag == consts.EVENT_CLICK:
            screen_before = self._save_screenshot(self.last_screenshot,
                                                  name_prefix='before')
            screen_after = self._save_screenshot(name_prefix='after')

            (x, y) = evt.args
            self.add_step('click',
                          screen_before=screen_before,
                          screen_after=screen_after,
                          position={
                              'x': x,
                              'y': y
                          })
        elif evt.flag == consts.EVENT_CLICK_IMAGE:
            kwargs = {
                'success': evt.traceback is None,
                'traceback':
                None if evt.traceback is None else evt.traceback.stack,
                'description': evt.kwargs.get('desc'),
            }
            # do not record if image not found and no trackback
            if evt.retval is None and evt.traceback is None:
                return

            # save before click image
            kwargs['screen_before'] = self._save_screenshot(
                self.last_screenshot, name_prefix='before')

            if evt.traceback is None or not isinstance(evt.traceback.exception,
                                                       IOError):
                pattern = d.pattern_open(evt.args[0])
                kwargs['target'] = self._save_screenshot(pattern,
                                                         name_prefix='target')
            if evt.traceback is None:
                # update image to add a click mark
                (x, y) = evt.retval.pos
                cv_img = imutils.from_pillow(self.last_screenshot)
                cv_img = imutils.mark_point(cv_img, x, y)
                self.__last_screenshot = imutils.to_pillow(cv_img)
                kwargs['screen_before'] = self._save_screenshot(
                    self.last_screenshot, name=kwargs['screen_before'])

                kwargs['screen_after'] = self._save_screenshot(
                    name_prefix='after')
                kwargs['confidence'] = evt.retval.confidence
                kwargs['position'] = {'x': x, 'y': y}

            self.add_step('click_image', **kwargs)