Best Python code snippet using uiautomator
test_device.py
Source:test_device.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3import unittest4from mock import MagicMock, call, patch5from uiautomator import AutomatorDevice, Selector6class TestDevice(unittest.TestCase):7 def setUp(self):8 self.device = AutomatorDevice()9 self.device.server = MagicMock()10 self.device.server.jsonrpc = MagicMock()11 self.device.server.jsonrpc_wrap = MagicMock()12 def test_info(self):13 self.device.server.jsonrpc.deviceInfo = MagicMock()14 self.device.server.jsonrpc.deviceInfo.return_value = {}15 self.assertEqual(self.device.info, {})16 self.device.server.jsonrpc.deviceInfo.assert_called_once_with()17 def test_click(self):18 self.device.server.jsonrpc.click = MagicMock()19 self.device.server.jsonrpc.click.return_value = True20 self.assertEqual(self.device.click(1, 2), True)21 self.device.server.jsonrpc.click.assert_called_once_with(1, 2)22 def test_swipe(self):23 self.device.server.jsonrpc.swipe = MagicMock()24 self.device.server.jsonrpc.swipe.return_value = True25 self.assertEqual(self.device.swipe(1, 2, 3, 4, 100), True)26 self.device.server.jsonrpc.swipe.assert_called_once_with(1, 2, 3, 4, 100)27 def test_long_click(self):28 self.device.server.jsonrpc.swipe = MagicMock()29 self.device.server.jsonrpc.swipe.return_value = True30 x, y = 100, 20031 self.assertEqual(self.device.long_click(x, y), True)32 self.device.server.jsonrpc.swipe.assert_called_once_with(x, y, x+1, y+1, 100)33 def test_drag(self):34 self.device.server.jsonrpc.drag = MagicMock()35 self.device.server.jsonrpc.drag.return_value = True36 self.assertEqual(self.device.drag(1, 2, 3, 4, 100), True)37 self.device.server.jsonrpc.drag.assert_called_once_with(1, 2, 3, 4, 100)38 def test_dump(self):39 self.device.server.jsonrpc.dumpWindowHierarchy = MagicMock()40 self.device.server.jsonrpc.dumpWindowHierarchy.return_value = "<?xml>"41 self.assertEqual(self.device.dump("/tmp/test.xml"), "<?xml>")42 self.device.server.jsonrpc.dumpWindowHierarchy.assert_called_once_with(True, None)43 def test_screenshot(self):44 self.device.server.jsonrpc.takeScreenshot = MagicMock()45 self.device.server.jsonrpc.takeScreenshot.return_value = "1.png"46 self.device.server.adb.cmd = cmd = MagicMock()47 cmd.return_value.returncode = 048 self.assertEqual(self.device.screenshot("a.png", 1.0, 99), "a.png")49 self.device.server.jsonrpc.takeScreenshot.assert_called_once_with("screenshot.png", 1.0, 99)50 self.assertEqual(cmd.call_args_list, [call("pull", "1.png", "a.png"), call("shell", "rm", "1.png")])51 self.device.server.jsonrpc.takeScreenshot.return_value = None52 self.assertEqual(self.device.screenshot("a.png", 1.0, 100), None)53 def test_freeze_rotation(self):54 self.device.server.jsonrpc.freezeRotation = MagicMock()55 self.device.freeze_rotation(True)56 self.device.freeze_rotation(False)57 self.assertEqual(self.device.server.jsonrpc.freezeRotation.call_args_list, [call(True), call(False)])58 def test_orientation(self):59 self.device.server.jsonrpc.deviceInfo = MagicMock()60 orientation = {61 0: "natural",62 1: "left",63 2: "upsidedown",64 3: "right"65 }66 for i in range(4):67 self.device.server.jsonrpc.deviceInfo.return_value = {"displayRotation": i}68 self.assertEqual(self.device.orientation, orientation[i])69 # set70 orientations = [71 (0, "natural", "n", 0),72 (1, "left", "l", 90),73 (2, "upsidedown", "u", 180),74 (3, "right", "r", 270)75 ]76 for values in orientations:77 for value in values:78 self.device.server.jsonrpc.setOrientation = MagicMock()79 self.device.orientation = value80 self.device.server.jsonrpc.setOrientation.assert_called_once_with(values[1])81 with self.assertRaises(ValueError):82 self.device.orientation = "invalid orientation"83 def test_last_traversed_text(self):84 self.device.server.jsonrpc.getLastTraversedText = MagicMock()85 self.device.server.jsonrpc.getLastTraversedText.return_value = "abcdef"86 self.assertEqual(self.device.last_traversed_text, "abcdef")87 self.device.server.jsonrpc.getLastTraversedText.assert_called_once_with()88 def test_clear_traversed_text(self):89 self.device.server.jsonrpc.clearLastTraversedText = MagicMock()90 self.device.clear_traversed_text()91 self.device.server.jsonrpc.clearLastTraversedText.assert_called_once_with()92 def test_open(self):93 self.device.server.jsonrpc.openNotification = MagicMock()94 self.device.open.notification()95 self.device.server.jsonrpc.openNotification.assert_called_once_with()96 self.device.server.jsonrpc.openQuickSettings = MagicMock()97 self.device.open.quick_settings()98 self.device.server.jsonrpc.openQuickSettings.assert_called_once_with()99 def test_watchers(self):100 names = ["a", "b", "c"]101 self.device.server.jsonrpc.getWatchers = MagicMock()102 self.device.server.jsonrpc.getWatchers.return_value = names103 self.assertEqual(self.device.watchers, names)104 self.device.server.jsonrpc.getWatchers.assert_called_once_with()105 self.device.server.jsonrpc.hasAnyWatcherTriggered = MagicMock()106 self.device.server.jsonrpc.hasAnyWatcherTriggered.return_value = True107 self.assertEqual(self.device.watchers.triggered, True)108 self.device.server.jsonrpc.hasAnyWatcherTriggered.assert_called_once_with()109 self.device.server.jsonrpc.removeWatcher = MagicMock()110 self.device.watchers.remove("a")111 self.device.server.jsonrpc.removeWatcher.assert_called_once_with("a")112 self.device.server.jsonrpc.removeWatcher = MagicMock()113 self.device.watchers.remove()114 self.assertEqual(self.device.server.jsonrpc.removeWatcher.call_args_list, [call(name) for name in names])115 self.device.server.jsonrpc.resetWatcherTriggers = MagicMock()116 self.device.watchers.reset()117 self.device.server.jsonrpc.resetWatcherTriggers.assert_called_once_with()118 self.device.server.jsonrpc.runWatchers = MagicMock()119 self.device.watchers.run()120 self.device.server.jsonrpc.runWatchers.assert_called_once_with()121 def test_watcher(self):122 self.device.server.jsonrpc.hasWatcherTriggered = MagicMock()123 self.device.server.jsonrpc.hasWatcherTriggered.return_value = False124 self.assertFalse(self.device.watcher("name").triggered)125 self.device.server.jsonrpc.hasWatcherTriggered.assert_called_once_with("name")126 self.device.server.jsonrpc.removeWatcher = MagicMock()127 self.device.watcher("a").remove()128 self.device.server.jsonrpc.removeWatcher.assert_called_once_with("a")129 self.device.server.jsonrpc.registerClickUiObjectWatcher = MagicMock()130 condition1 = {"text": "my text", "className": "android"}131 condition2 = {"description": "my desc", "clickable": True}132 target = {"className": "android.widget.Button", "text": "OK"}133 self.device.watcher("watcher").when(**condition1).when(**condition2).click(**target)134 self.device.server.jsonrpc.registerClickUiObjectWatcher.assert_called_once_with("watcher", [Selector(**condition1), Selector(**condition2)], Selector(**target))135 self.device.server.jsonrpc.registerPressKeyskWatcher = MagicMock()136 self.device.watcher("watcher2").when(**condition1).when(**condition2).press.back.home.power("menu")137 self.device.server.jsonrpc.registerPressKeyskWatcher.assert_called_once_with(138 "watcher2", [Selector(**condition1), Selector(**condition2)], ("back", "home", "power", "menu"))139 def test_press(self):140 key = ["home", "back", "left", "right", "up", "down", "center",141 "menu", "search", "enter", "delete", "del", "recent",142 "volume_up", "volume_down", "volume_mute", "camera", "power"]143 self.device.server.jsonrpc.pressKey = MagicMock()144 self.device.server.jsonrpc.pressKey.return_value = True145 self.assertTrue(self.device.press.home())146 self.device.server.jsonrpc.pressKey.return_value = False147 self.assertFalse(self.device.press.back())148 self.device.server.jsonrpc.pressKey.return_value = False149 for k in key:150 self.assertFalse(self.device.press(k))151 self.assertEqual(self.device.server.jsonrpc.pressKey.call_args_list, [call("home"), call("back")] + [call(k) for k in key])152 self.device.server.jsonrpc.pressKeyCode.return_value = True153 self.assertTrue(self.device.press(1))154 self.assertTrue(self.device.press(1, 2))155 self.assertEqual(self.device.server.jsonrpc.pressKeyCode.call_args_list, [call(1), call(1, 2)])156 def test_wakeup(self):157 self.device.server.jsonrpc.wakeUp = MagicMock()158 self.device.wakeup()159 self.device.server.jsonrpc.wakeUp.assert_called_once_with()160 self.device.server.jsonrpc.wakeUp = MagicMock()161 self.device.screen.on()162 self.device.server.jsonrpc.wakeUp.assert_called_once_with()163 self.device.server.jsonrpc.wakeUp = MagicMock()164 self.device.screen("on")165 self.device.server.jsonrpc.wakeUp.assert_called_once_with()166 def test_sleep(self):167 self.device.server.jsonrpc.sleep = MagicMock()168 self.device.sleep()169 self.device.server.jsonrpc.sleep.assert_called_once_with()170 self.device.server.jsonrpc.sleep = MagicMock()171 self.device.screen.off()172 self.device.server.jsonrpc.sleep.assert_called_once_with()173 self.device.server.jsonrpc.sleep = MagicMock()174 self.device.screen("off")175 self.device.server.jsonrpc.sleep.assert_called_once_with()176 def test_wait_idle(self):177 self.device.server.jsonrpc_wrap.return_value.waitForIdle = MagicMock()178 self.device.server.jsonrpc_wrap.return_value.waitForIdle.return_value = True179 self.assertTrue(self.device.wait.idle(timeout=10))180 self.device.server.jsonrpc_wrap.return_value.waitForIdle.assert_called_once_with(10)181 self.device.server.jsonrpc_wrap.return_value.waitForIdle = MagicMock()182 self.device.server.jsonrpc_wrap.return_value.waitForIdle.return_value = False183 self.assertFalse(self.device.wait("idle", timeout=10))184 self.device.server.jsonrpc_wrap.return_value.waitForIdle.assert_called_once_with(10)185 def test_wait_update(self):186 self.device.server.jsonrpc_wrap.return_value.waitForWindowUpdate = MagicMock()187 self.device.server.jsonrpc_wrap.return_value.waitForWindowUpdate.return_value = True188 self.assertTrue(self.device.wait.update(timeout=10, package_name="android"))189 self.device.server.jsonrpc_wrap.return_value.waitForWindowUpdate.assert_called_once_with("android", 10)190 self.device.server.jsonrpc_wrap.return_value.waitForWindowUpdate = MagicMock()191 self.device.server.jsonrpc_wrap.return_value.waitForWindowUpdate.return_value = False192 self.assertFalse(self.device.wait("update", timeout=100, package_name="android"))193 self.device.server.jsonrpc_wrap.return_value.waitForWindowUpdate.assert_called_once_with("android", 100)194 def test_get_info_attr(self):195 info = {"test_a": 1, "test_b": "string", "displayWidth": 720, "displayHeight": 1024}196 self.device.server.jsonrpc.deviceInfo = MagicMock()197 self.device.server.jsonrpc.deviceInfo.return_value = info198 for k in info:199 self.assertEqual(getattr(self.device, k), info[k])200 self.assertEqual(self.device.width, info["displayWidth"])201 self.assertEqual(self.device.height, info["displayHeight"])202 with self.assertRaises(AttributeError):203 self.device.not_exists204 def test_device_obj(self):205 with patch("uiautomator.AutomatorDeviceObject") as AutomatorDeviceObject:206 kwargs = {"text": "abc", "description": "description...", "clickable": True}207 self.device(**kwargs)208 AutomatorDeviceObject.assert_called_once_with(self.device, Selector(**kwargs))209 with patch("uiautomator.AutomatorDeviceObject") as AutomatorDeviceObject:210 AutomatorDeviceObject.return_value.exists = True211 self.assertTrue(self.device.exists(clickable=True))212 AutomatorDeviceObject.return_value.exists = False213 self.assertFalse(self.device.exists(text="..."))214class TestDeviceWithSerial(unittest.TestCase):215 def test_serial(self):216 with patch('uiautomator.AutomatorServer') as AutomatorServer:217 AutomatorDevice("abcdefhijklmn")...
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!!