Best Python code snippet using fMBT_python
fmbtgti.py
Source:fmbtgti.py  
...1616                  path that will prefix all relative paths in1617                  bitmapPath.1618        Example:1619          gui.setBitmapPath("bitmaps:icons:/tmp", "/home/X")1620          gui.tapBitmap("start.png")1621          will look for /home/X/bitmaps/start.png,1622          /home/X/icons/start.png and /tmp/start.png, in this order.1623        """1624        self._paths.bitmapPath = bitmapPath1625        if rootForRelativePaths != None:1626            self._paths.relativeRoot = rootForRelativePaths1627    def setConnection(self, conn):1628        """1629        Set the connection object that performs actions on real target.1630        Parameters:1631          conn (GUITestConnection instance):1632                  The connection to be used.1633        """1634        self._conn = conn1635    def setOcrEngine(self, ocrEngine):1636        """1637        Set OCR (optical character recognition) engine that will be1638        used by default in new screenshots.1639        Returns previous default.1640        """1641        prevDefault = self._ocrEngine1642        self._ocrEngine = ocrEngine1643        return prevDefault1644    def setOirEngine(self, oirEngine):1645        """1646        Set OIR (optical image recognition) engine that will be used1647        by default in new screenshots.1648        Returns previous default.1649        """1650        prevDefault = self._oirEngine1651        self._oirEngine = oirEngine1652        return prevDefault1653    def setScreenshotArchiveMethod(self, screenshotArchiveMethod):1654        """1655        Set method for archiving screenshots when screenshotLimit is exceeded.1656        Parameters:1657          screenshotArchiveMethod (string)1658                  Supported methods are "resize [WxH]" and "remove"1659                  where W and H are integers that define maximum width and1660                  height for an archived screenshot.1661                  The default method is "resize".1662        """1663        if screenshotArchiveMethod == "remove":1664            pass1665        elif screenshotArchiveMethod == "resize":1666            pass1667        elif screenshotArchiveMethod.startswith("resize"):1668            try:1669                w, h = screenshotArchiveMethod.split(" ")[1].split("x")1670            except:1671                raise ValueError("Invalid resize syntax")1672            try:1673                w, h = int(w), int(h)1674            except:1675                raise ValueError(1676                    "Invalid resize width or height, integer expected")1677        else:1678            raise ValueError('Unknown archive method "%s"' %1679                             (screenshotArchiveMethod,))1680        self._screenshotArchiveMethod = screenshotArchiveMethod1681    def setScreenshotDir(self, screenshotDir):1682        self._screenshotDir = screenshotDir1683        self._newScreenshotFilepath() # make directories1684    def setScreenshotLimit(self, screenshotLimit):1685        """1686        Set maximum number for unarchived screenshots.1687        Parameters:1688          screenshotLimit (integer)1689                  Maximum number of unarchived screenshots that are1690                  free for archiving (that is, not referenced by test code).1691                  The default is None, that is, there is no limit and1692                  screenshots are never archived.1693        See also:1694          setScreenshotArchiveMethod()1695        """1696        self._screenshotLimit = screenshotLimit1697    def setScreenshotSubdir(self, screenshotSubdir):1698        """1699        Define a subdirectory under screenshotDir() for screenshot files.1700        Parameters:1701          screenshotSubdir (string)1702                  Name of a subdirectory. The name should contain1703                  conversion specifiers supported by strftime.1704        Example:1705          sut.setScreenshotSubdir("%m-%d-%H")1706                  A screenshot taken on June 20th at 4.30pm will1707                  be stored to screenshotDir/06-20-16. That is,1708                  screenshots taken on different hours will be1709                  stored to different subdirectories.1710        By default, all screenshots are stored directly to screenshotDir().1711        """1712        self._screenshotSubdir = screenshotSubdir1713    def setTapDefaults(self, **tapDefaults):1714        """1715        Define default parameters for tap methods.1716        Parameters:1717          **tapDefaults (keyword arguments):1718                  default arguments to be used in sendTap call unless1719                  explicitely overridden by user.1720        Example:1721          sut.setTapDefaults(button=1)1722                  after this sut.tapBitmap("ref.png") does the same as1723                  sut.tapBitmap("ref.png", button=1) did before.1724        """1725        self._tapDefaults.update(tapDefaults)1726    def swipe(self, (x, y), direction, distance=1.0, **dragArgs):1727        """1728        swipe starting from coordinates (x, y) to given direction.1729        Parameters:1730          coordinates (floats in range [0.0, 1.0] or integers):1731                  floating point coordinates in range [0.0, 1.0] are1732                  scaled to full screen width and height, others are1733                  handled as absolute coordinate values.1734          direction (string or integer):1735                  Angle (0..360 degrees), or "north", "south", "east"1736                  and "west" (corresponding to angles 90, 270, 0 and1737                  180).1738          distance (float, optional):1739                  Swipe distance. Values in range [0.0, 1.0] are1740                  scaled to the distance from the coordinates to the1741                  edge of the screen. The default is 1.0: swipe to the1742                  edge of the screen.1743          rest of the parameters: refer to drag documentation.1744        Returns True on success, False if sending input failed.1745        """1746        if type(direction) == str:1747            d = direction.lower()1748            if d in ["n", "north"]: direction = 901749            elif d in ["s", "south"]: direction = 2701750            elif d in ["e", "east"]: direction = 01751            elif d in ["w", "west"]: direction = 1801752            else: raise ValueError('Illegal direction "%s"' % (direction,))1753        direction = direction % 3601754        x, y = self.intCoords((x, y))1755        dirRad = math.radians(direction)1756        distToEdge = _edgeDistanceInDirection((x, y), self.screenSize(), direction)1757        if distance > 1.0: distance = float(distance) / distToEdge1758        x2 = int(x + math.cos(dirRad) * distToEdge * distance)1759        y2 = int(y - math.sin(dirRad) * distToEdge * distance)1760        return self.drag((x, y), (x2, y2), **dragArgs)1761    def swipeBitmap(self, bitmap, direction, distance=1.0, **dragAndOirArgs):1762        """1763        swipe starting from bitmap to given direction.1764        Parameters:1765          bitmap (string)1766                  bitmap from which swipe starts1767          direction, distance1768                  refer to swipe documentation.1769          startPos, startOffset1770                  refer to swipeItem documentation.1771          optical image recognition arguments (optional)1772                  refer to help(obj.oirEngine()).1773          delayBeforeMoves, delayBetweenMoves, delayAfterMoves,1774          movePoints1775                  refer to drag documentation.1776        Returns True on success, False if sending input failed.1777        """1778        assert self._lastScreenshot != None, "Screenshot required."1779        dragArgs, rest = _takeDragArgs(dragAndOirArgs)1780        oirArgs, _ = _takeOirArgs(self._lastScreenshot, rest, thatsAll=True)1781        oirArgs["limit"] = 11782        items = self._lastScreenshot.findItemsByBitmap(bitmap, **oirArgs)1783        if len(items) == 0:1784            return False1785        return self.swipeItem(items[0], direction, distance, **dragArgs)1786    def swipeItem(self, viewItem, direction, distance=1.0, **dragArgs):1787        """1788        swipe starting from viewItem to given direction.1789        Parameters:1790          viewItem (ViewItem)1791                  item from which swipe starts1792          direction, distance1793                  refer to swipe documentation.1794          startPos (pair of floats (x, y)):1795                  position of starting swipe, relative to the item.1796                  (0.0, 0.0) is the top-left corner,1797                  (1.0, 0.0) is the top-right corner,1798                  (1.0, 1.0) is the lower-right corner.1799                  Values < 0.0 and > 1.0 start swiping from coordinates1800                  outside the item.1801                  The default is (0.5, 0.5), in the middle of the item.1802          startOffset (pair of integers or floats (x, y)):1803                  offset of swipe start coordinates. Integers are1804                  pixels, floats are relative to screensize.1805                  Example:1806                  startOffset=(0, 0.1) will keep the X coordinate1807                  unchagned and add 10 % of screensize to Y.1808          delayBeforeMoves, delayBetweenMoves, delayAfterMoves,1809          movePoints1810                  refer to drag documentation.1811        Returns True on success, False if sending input failed.1812        """1813        if "startPos" in dragArgs:1814            posX, posY = dragArgs["startPos"]1815            del dragArgs["startPos"]1816            x1, y1, x2, y2 = viewItem.bbox()1817            swipeCoords = (x1 + (x2-x1) * posX,1818                           y1 + (y2-y1) * posY)1819        else:1820            swipeCoords = viewItem.coords()1821        if "startOffset" in dragArgs:1822            offX, offY = dragArgs["startOffset"]1823            del dragArgs["startOffset"]1824            x, y = swipeCoords1825            if isinstance(offX, int):1826                x += offX1827            elif isinstance(offX, float):1828                width, _ = self.screenSize()1829                x += offX * width1830            else:1831                raise TypeError('invalid offset %s (int or float expected)' %1832                                (repr(offX),))1833            if isinstance(offY, int):1834                y += offY1835            elif isinstance(offY, float):1836                _, height = self.screenSize()1837                y += offY * height1838            else:1839                raise TypeError('invalid offset %s (int or float expected)' %1840                                (repr(offY),))1841            swipeCoords = (x, y)1842        return self.swipe(swipeCoords, direction, distance, **dragArgs)1843    def swipeOcrText(self, text, direction, distance=1.0, **dragAndOcrArgs):1844        """1845        Find text from the latest screenshot using OCR, and swipe it.1846        Parameters:1847          text (string):1848                  the text to be swiped.1849          direction, distance1850                  refer to swipe documentation.1851          startPos1852                  refer to swipeItem documentation.1853          delayBeforeMoves, delayBetweenMoves, delayAfterMoves,1854          movePoints1855                  refer to drag documentation.1856          OCR engine specific arguments1857                  refer to help(obj.ocrEngine())1858        Returns True on success, False otherwise.1859        """1860        assert self._lastScreenshot != None, "Screenshot required."1861        dragArgs, rest = _takeDragArgs(dragAndOcrArgs)1862        ocrArgs, _ = _takeOcrArgs(self._lastScreenshot, rest, thatsAll=True)1863        items = self._lastScreenshot.findItemsByOcr(text, **ocrArgs)1864        if len(items) == 0:1865            return False1866        return self.swipeItem(items[0], direction, distance, **dragArgs)1867    def tap(self, (x, y), long=_USE_DEFAULTS, hold=_USE_DEFAULTS,1868            count=_USE_DEFAULTS, delayBetweenTaps=_USE_DEFAULTS,1869            button=_USE_DEFAULTS):1870        """1871        Tap screen on coordinates (x, y).1872        Parameters:1873          coordinates (floats in range [0.0, 1.0] or integers):1874                  floating point coordinates in range [0.0, 1.0] are1875                  scaled to full screen width and height, others are1876                  handled as absolute coordinate values.1877          count (integer, optional):1878                  number of taps to the coordinates. The default is 1.1879          delayBetweenTaps (float, optional):1880                  time (seconds) between taps when count > 1.1881                  The default is 0.175 (175 ms).1882          long (boolean, optional):1883                  if True, touch the screen for a long time.1884          hold (float, optional):1885                  time in seconds to touch the screen.1886          button (integer, optional):1887                  send tap using given mouse button. The default is1888                  None: button parameter is not passed to the1889                  underlying connection layer (sendTouchDown etc.),1890                  the default in the underlying layer will be used.1891                  Note that all connection layers may not support1892                  this parameter.1893        Returns True if successful, otherwise False.1894        """1895        if long == _USE_DEFAULTS:1896            long = self._tapDefaults.get("long", False)1897        if hold == _USE_DEFAULTS:1898            hold = self._tapDefaults.get("hold", 0.0)1899        if count == _USE_DEFAULTS:1900            count = self._tapDefaults.get("count", 1)1901        if delayBetweenTaps == _USE_DEFAULTS:1902            delayBetweenTaps = self._tapDefaults.get("delayBetweenTaps", 0.175)1903        if button == _USE_DEFAULTS:1904            button = self._tapDefaults.get("button", None)1905        x, y = self.intCoords((x, y))1906        count = int(count)1907        if long and hold == 0.0:1908            hold = self._longTapHoldTime1909        extraParams = {}1910        if button != None:1911            extraParams['button'] = button1912        if count == 0:1913            self.existingConnection().sendTouchMove(x, y)1914        while count > 0:1915            if hold > 0.0:1916                try:1917                    assert self.existingConnection().sendTouchDown(x, y, **extraParams)1918                    time.sleep(hold)1919                    assert self.existingConnection().sendTouchUp(x, y, **extraParams)1920                except AssertionError:1921                    return False1922            else:1923                if not self.existingConnection().sendTap(x, y, **extraParams):1924                    return False1925            count = int(count) - 11926        return True1927    def tapBitmap(self, bitmap, **tapAndOirArgs):1928        """1929        Find a bitmap from the latest screenshot, and tap it.1930        Parameters:1931          bitmap (string):1932                  filename of the bitmap to be tapped.1933          optical image recognition arguments (optional)1934                  refer to help(obj.oirEngine()).1935          tapPos (pair of floats (x,y)):1936                  refer to tapItem documentation.1937          tapOffset (pair of floats or integers (x, y)):1938                  refer to tapItem documentation.1939          long, hold, count, delayBetweenTaps, button (optional):1940                  refer to tap documentation.1941        Returns True if successful, otherwise False....ivi_apps.py
Source:ivi_apps.py  
...99        time.sleep(1)100        return ret101    def refAndTapBitmap(self, png, wait_time=1):102        print "++ refresh screen and tapBitmap " + png103        ret = self.refreshScreenshot() and self.tapBitmap(png)104        time.sleep(wait_time)105        return ret106    def refAndVerifyBitmap(self, png, wait_time=1):107        print "++ refresh screen and verify bitmap " + png108        ret = self.refreshScreenshot() and self.verifyBitmap(png)109        time.sleep(wait_time)110        return ret111        112    def refAndWaitAllBitmap(self, picture_list):113        print "++ refresh screen and wait all bitmap: " \114               + ', '.join(picture_list)115        self.refreshScreenshot()116        ret = True117        for p in picture_list:118            if not self.waitBitmap(p):119                print "++ %s not found" % p120                ret = False121        return ret122    def findPid(self, process_name):123        cmd = "pgrep -f '%s'" % process_name124        (r, so, se ) = self.shellSOE(cmd)125        pids = []126        if r == 0:127            pids = [string.atoi(n) for n in filter(lambda l: len(l)>0, so.split("\n"))]128        return r, pids129    def verifyAllProcess(self, process_list):130        print "++ check process: " + ', '.join(process_list)131        ret = True132        for p in process_list:133            if len(self.findPid(p)[1]) == 0:134                print "++ %s not found" % p135                ret = False136            else:137                print "++ found %s" % p138        return ret139    def _hoover(self, x, y):140        self.drag((x, y), (x, y), delayBeforeMoves=-1, delayAfterMoves=-1, movePoints=1)141    def refreshScreenshot(self):142        ret = False143        if self.move_cursor == True:144            if self.maxX != 0:145                print "++ move cursor to %d, %d" % (self.maxX, self.maxY)146                self._hoover(self.maxX, self.maxY)147            ret = fmbttizen.Device.refreshScreenshot(self)148            if self.maxX != 0:149                print "++ move cursor to 0, 0"150                self._hoover(0, 0)151        else:152            ret = fmbttizen.Device.refreshScreenshot(self)153        return ret154            155    def check_call(self, cmd):156        (ret, so, se) = self.shellSOE(cmd)157        if ret != 0:158            print "++ fail to run cmd `%s'" % (cmd)159            print "++ standard output: " + so 160            print "++ standard error:" + se161            raise TizenError("cmd_issue", "fail to run cmd `%s'" % (cmd))162        return so163class cl_app():164    def __init__(self, name, process, controller):165        self.name = name166        self.process = process167        self.controllerImp = controller168class NightModeController():169    def __init__(self, controller):170        self.controllerImp = controller171        self.theme = ""172    173    def refNightMode(self):174        self.theme = ""175        (ret, so, se) = self.controllerImp.shellSOE("amb-get NightMode")176        if ret == 0:177            if '"NightMode": 1' in so:178                self.theme = "night"179            elif '"NightMode": 0' in so:180                self.theme = "day"181        if self.theme:182            print "++ get NightMode successfully, the theme is " + self.theme183        else:184            self.theme = "day"185            print "++ fail to get NightMode, use default theme `%s'" % self.theme186        theme_str = app_icons_str.replace("%theme%", self.theme)187        exec(theme_str)188        print "++ app_icons: %s" % app_icons189    def setNightMode(self, night_mode=True):190        if night_mode:191            pass192        else:193            pass194        self.refNightMode()195class WestonDesktop(cl_app):196    def __init__(self, controller):197        cl_app.__init__(self, "WestonDesktop", "", controller)198        self.night_mode_ctrl = NightModeController(controller)199    def wrt_launch(self, app_name):200        out = self.controllerImp.check_call("wrt-launcher -l")201        for l in out.splitlines():202            if l.find(app_name) != -1:203                app_id = l.split()[-1]204                if app_id.find(".%s" % app_name) == -1:205                    print "++ app_id format is not correct `%s'" % (app_id)206                    return False207                print "++ found app_id `%s'" % (app_id)208                break209        else:210            print "++ not found app_id for %s" % (app_name)211            return False212        self.controllerImp.type("wrt-launcher -s %s\n" % app_id)213    def cmd_launch(self, app_name):214        self.controllerImp.type("%s\n" % app_name)215    def launch_app(self, app_name):216        self.controllerImp.type("launch_app %s\n" % app_name)217    def launch(self, app_name, picture_list=[], process_list=[]):218        if self.controllerImp.refAndTapBitmap("term.icon.png") or \219           self.controllerImp.refAndTapBitmap("term.icon.png"):220            print "++ sucessfully tap western term icon"221        else:222            return False223        # to fetch focus, tricky !!224        self.controllerImp._hoover(self.controllerImp.maxX, self.controllerImp.maxY)225        print "++ proper launch app"226        if app_name in ["saythis", "Settings", "GhostCluster", "MediaPlayer"]:227            self.wrt_launch(app_name)228        elif app_name in ["org.tizen.dialer"]:229            self.launch_app(app_name)230        else:231            self.cmd_launch(app_name)232        print "++ try to refresh screenshot"233        self.controllerImp.refreshScreenshot()234        time.sleep(2)235        if process_list:236            print "++ verify started process..."237            if not self.controllerImp.verifyAllProcess(process_list):238                print "++ expected process not found"239                return False240        if picture_list:241            print "++ verify expected bitmap..."242            if not self.controllerImp.refAndWaitAllBitmap(picture_list):243                print "++ fail to verify bitmap"244                return False245        print "++ launch %s successfully" % (app_name)246        time.sleep(2)247        return True248    def refNightMode(self):249        self.night_mode_ctrl.refNightMode()250class ICOHomeScreen(cl_app):251    def __init__(self, controller):252        cl_app.__init__(self, "homescreen", "", controller)253        self.current_page = 0254        self.app_status = {}255        self.max_page = 3256        self.night_mode_ctrl = NightModeController(controller)257        self.apps = []258    def registerApp(self, app):259        self.apps.append(app)260    def clickApplist(self):261        return self.controllerImp.refAndTapBitmap(app_icons["applist"])262    263    def swipeToWest(self):264        ret = self.controllerImp.swipeToWest()265        if self.current_page < self.max_page - 1:266            self.current_page += 1267        print "++ current page: %d" % self.current_page268        assert(self.current_page < self.max_page)269        return ret270    def swipeToEast(self):271        ret = self.controllerImp.swipeToEast()272        if self.current_page > 0:273            self.current_page -= 1274        print "++ current page: %d" % self.current_page275        assert(self.current_page >= 0)276        return ret277    def swipePages(self, start, end):278        assert start >= 0, "wrong start page %d"  % start279        assert end < self.max_page, "wrong end page %d" % end280        print "++ current_page: %d, start page: %d, end page: %d" \281            % (self.current_page, start, end)282        if start > end:283            for i in range(start - end):284                if not self.swipeToEast():285                    return False286        else:287            for i in range(end - start):288                if not self.swipeToWest():289                    return False290        return True291    def swipeToPage(self, end):292        return self.swipePages(self.current_page, end)293    def swipeToLeftmost(self):294        return self.swipePages(self.current_page, 0)295    def launch(self, app_name, app_icon, picture_list=[], process_list=[]):296        print "++ click applist button"297        if not self.clickApplist():298            print "++ fail to click applist button!"299            return False300        301        first_launch = False302        icon = app_icon303        print "++ try to launch %s" % app_name304        if app_name in self.app_status.keys():305            print "++ %s is already launched" % app_name306            app_page = self.app_status[app_name][0]307            print "++ swipe to app page %d" % app_page308            if not self.swipeToPage(app_page):309                print "++ fail to swipe to the specified page! cur page: %d" \310                    % self.current_page311                return False312            print "++ try to tap the old item"313            if not self.controllerImp.tapItem((self.app_status[app_name])[1]):314                print "++ fail to tap item!"315                return False316        else: # first launch317            print "++ %s is the first launch" % app_name318            first_launch = True319            print "++ swipe to the left most side"320            if not self.swipeToLeftmost():321                print "++ fail to swipe to the left most side!"322                return False323            for i in range(self.max_page):324                print "++ check app page %d" % i325                if self.controllerImp.refreshScreenshot() == None:326                    print "++ fail to refresh screenshot!"327                    return False328                if self.controllerImp.tapBitmap(icon) == True:329                    print "++ found icon " + icon330                    app_item = self.controllerImp._lastScreenshot.findItemsByBitmap(icon)[0]331                    break332                else:333                    print "++ swipe to the next page"334                    if not self.swipeToWest():335                        print "++ fail to swipe to next page!"336                        return False337            else:338                print "++ not found app icon %s" % icon339                if not self.clickApplist():340                    print "++ fail to click applist button!"341                return False342        ...pages.py
Source:pages.py  
...11class PagesTest(unittest.TestCase):12    def setUp(self):13        s.refreshScreenshot()14    def tearDown(self):15        s.tapBitmap("seur.png", colorMatch=0.5)16        sleep(DELAY)17        s.refreshScreenshot()18    def test_moveToPage2(self):19        s.tapBitmap("input-username.png", colorMatch=0.9)20        sleep(DELAY)21        s.refreshScreenshot()22        found = s.verifyBitmap("kuvas6.png", colorMatch=0.9)23        self.assertTrue(found, "Header2 not found")24if __name__ == '__main__':...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!!
