How to use displayRotation method in fMBT

Best Python code snippet using fMBT_python

device.py

Source:device.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2"""3Tencent is pleased to support the open source community by making GAutomator available.4Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.5Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at6http://opensource.org/licenses/MIT7Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.8 un-Thread-safe9 Service use libaray https://github.com/xiaocong/uiautomator10 Mobile feature use uiautomator service,so maybe confict with other process which also use uiautomator serice,such as Monkeyrunner,AccessibilityService11"""12__author__ = 'minhuaxu wukenaihesos@gmail.com'13import re14import traceback15import os16import logging17from wpyscripts.common.wetest_exceptions import (WeTestRuntimeError, UIAutomatorError, WeTestInvaildArg, LoginError)18from wpyscripts.httptools.exceptions import ConnectionException19import wpyscripts.common.platform_helper as platform20from wpyscripts.common.adb_process import *21from wpyscripts.Minitouch import Minitouch22from wpyscripts.common.coordinate_transfer import *23from functools import wraps24from config import Account25logger = logging.getLogger("wetest")26class DisplaySize(object):27 """28 Mobile Screen resolution29 Attributes:30 width: width px31 height:height px32 """33 def __init__(self, width, height):34 self.__width = width35 self.__height = height36 @property37 def width(self):38 return self.__width39 @property40 def height(self):41 return self.__height42 def __str__(self):43 return "width = {0},height = {1}".format(self.width, self.height)44class TopActivity(object):45 """46 Mobile current foreground activity and package name.activity is None,when android version > 5.047 Attributes:48 activity:top Activity49 package_name:forceground package name50 """51 def __init__(self, _activity, _package):52 self.activity = _activity53 self.package_name = _package54 def __str__(self):55 return "package name = {0},activity = {1}".format(self.package_name, self.activity)56def exception_call_super(fn):57 """58 Decorate function59 CloudDevice call function use wetest platorm http service,when raise exception,try to call parent class method60 很多方法请求平台时,有可能出现问题。请求平台出现问题时,可以直接尝试使用本地方法。61 CloudDevice调用失败时,直接调用Device里面相同名称的方法62 :param fn:63 :return:64 """65 @wraps(fn)66 def wrap_function(*args, **kwargs):67 try:68 return fn(*args, **kwargs)69 except (WeTestRuntimeError, ConnectionException):70 logger.warning("call cloud function {0} error".format(fn.__name__))71 return getattr(super(CloudDevice, args[0]), fn.__name__)(*args[1:], **kwargs)72 return wrap_function73class Device(object):74 """"Base Mobile Related operations"""75 def __init__(self, serial, package, ui_device):76 super(Device, self).__init__()77 self.serial = serial78 self.minitoucher=Minitouch.get_minitouch()79 self.package = package80 self.ui_device = ui_device81 self._adb = AdbTool() # serial, os.environ.get("PLATFORM_IP", "127.0.0.1")82 @property83 def adb(self):84 return self._adb85 def get_rotation(self):86 """87 get mobile current rotation88 orienting the devie to left/right or natural.89 left/l: rotation=90 , displayRotation=190 right/r: rotation=270, displayRotation=391 natural/n: rotation=0 , displayRotation=092 upsidedown/u: rotation=180, displayRotation=293 :return: 0,1,2,394 """95 rotation = self.ui_device.info["displayRotation"]96 return rotation97 def get_display_size(self):98 """99 Get mobile screen resolution100 :Usage:101 >>> import wpyscripts.manager as manager102 >>> device=manager.get_device()103 >>> display=device.get_display_size()104 :return:105 a instance of DisplaySize106 :rtype: DisplaySize107 """108 try:109 return DisplaySize(self.ui_device.info["displayWidth"], self.ui_device.info["displayHeight"])110 except:111 stack = traceback.format_exc()112 logger.warning(stack)113 result = self.adb.cmd_wait("shell", "dumpsys", "window", "displays")114 if result:115 pattern = re.compile(r'cur=(\d+)x(\d+)')116 match = pattern.search(result)117 if match:118 dispaly_size = DisplaySize(int(match.group(1)), int(match.group(2)))119 return dispaly_size120 def get_top_package_activity(self):121 """122 get the current foreground activity123 :Usage:124 >>> import wpyscripts.manager as manager125 >>> device=manager.get_device()126 >>> display=device.get_top_package_activity()127 :return:128 a instance of TopActivity129 :rtype: TopActivity130 """131 try:132 return TopActivity("", self.ui_device.info["currentPackageName"])133 except:134 stack = traceback.format_exc()135 logger.warning(stack)136 def _is_package_live(self, package=None):137 logger.warning("_is_package_live unimplement")138 def back(self):139 """140 Click back button141 When your game not force,you call engine api may catch timeout exception.At this time you can call back142 :Usage:143 >>> import wpyscripts.manager as manager144 >>> device=manager.get_device()145 >>> display=device.back()146 :return:147 """148 try:149 self.ui_device.press("back")150 except Exception as e:151 raise UIAutomatorError(e.message)152 def touchDown(self,contactId,x,y,consider_rotation=True):153 """154 send touch down event to device155 :Usage:156 >>> import wpyscripts.manager as manager157 >>> device=manager.get_device()158 >>> device.touchDown(0,300,300,True)159 :param contactId: the touch event id. 160 x,y: touch position161 consider_rotation: if it's True, the position is display position considering device orientation. 162 """163 pt_x,pt_y=x,y164 display_size=self.get_display_size()165 display_size = (display_size.width, display_size.height)166 orientation=self.get_rotation()167 if consider_rotation:168 (pt_x,pt_y)= transfer_display_coordinate_to_image((x,y),display_size,orientation,self.minitoucher.getScreenResolution())169 self.minitoucher.touchDown(pt_x,pt_y, contactId)170 def touchMove(self,contactId,x,y,consider_rotation=True):171 """172 send touch move event to device173 :Usage:174 >>> import wpyscripts.manager as manager175 >>> device=manager.get_device()176 >>> device.touchMove(0,300,300,True)177 :param contactId: the touch event id. 178 x,y: touch position179 consider_rotation: if it's True, the position is display position considering device orientation. 180 """181 pt_x,pt_y=x,y182 display_size=self.get_display_size()183 display_size = (display_size.width, display_size.height)184 orientation=self.get_rotation()185 if consider_rotation:186 (pt_x,pt_y)= transfer_display_coordinate_to_image((x,y),display_size,orientation,self.minitoucher.getScreenResolution())187 self.minitoucher.touchMove(pt_x,pt_y, contactId)188 def touchUp(self,contactId):189 """190 send touch up event to device191 :Usage:192 >>> import wpyscripts.manager as manager193 >>> device=manager.get_device()194 >>> device.touchUp(0)195 :param contactId: the touch event id. 196 """197 self.minitoucher.touchUp(contact=contactId)198 def launch_app(self, package, activity="android.intent.category.LAUNCHER"):199 """200 启动游戏,游戏启动后返回。返回启动后的Pid和拉起时间,device实例会保存Pid的值201 :param package:202 :return:(234,2341)分别代表打起后的pid和拉起时间203 """204 result = self.adb.cmd_wait("shell", "monkey", "-p", package, "-c", activity, "1")205 if result:206 content = result207 logger.debug(content)208 result = excute_adb("shell ps")209 if result:210 pattern_str = r"(?P<user>\S+)\s+(?P<pid>\d+)\s+(?P<ppid>\d+)\s.*{0}\s+".format(package)211 pattern = re.compile(pattern_str)212 line = result.readline()213 while line:214 match = pattern.search(line)215 if match:216 pid = match.groupdict().get("pid")217 pid = int(pid)218 return pid, 0219 line = result.readline()220 def clear_data(self, package=None):221 """222 Adb shell pm clear package,clear app data223 :param package:224 :return:225 """226 if package is None:227 raise WeTestInvaildArg("Error,No app packagename")228 result = self.adb.cmd_wait("shell", "pm", "clear", package)229 if result:230 content = result231 logger.debug(content)232 def _clear_qq_account(self):233 logger.debug("adb shell pm clear com.tencent.mobileqq")234 try:235 result = self.adb.cmd_wait("shell", "pm", "clear", "com.tencent.mobileqq")236 if result:237 content = result238 logger.debug(content)239 except:240 stack = traceback.format_exc()241 logger.warning(stack)242 logger.debug("adb shell pm clear com.tencent.mm")243 try:244 result = self.adb.cmd_wait("shell", "pm", "clear", "com.tencent.mm")245 if result:246 content = result247 logger.debug(content)248 except:249 stack = traceback.format_exc()250 logger.warning(stack)251 def _clear_user_info(self, package):252 if package == None:253 raise WeTestInvaildArg("Package Name can't be none")254 logger.debug("adb shell pm clear {0}".format(package))255 result = excute_adb("shell pm clear {0}".format(package))256 if result:257 content = result.read()258 logger.debug(content)259 def excute_adb(self, cmd):260 """261 Open a pipe to/from a adb command returning a file object,when command is end262 if testcat is run on wetest platorm,you can put your file to UPLOADDIR,and download from reporter263 :Usage:264 >>> import wpyscripts.manager as manager265 >>> device=manager.get_device()266 >>> device.excute_adb_shell("pull /data/local/tmp {0}/perform.txt".format(os.environ.get("UPLOADDIR",".")))267 :param cmd: adb command268 :return:a file object269 """270 return excute_adb(cmd)271 def get_current_package(self):272 return self.ui_device.info["currentPackageName"]273 def login_qq_wechat_wait(self, timeout=180):274 """275 如果是在QQ或者微信登陆界面调用该函数,则会尝试登陆。在本地运行时,请在main.py进行开头或者命令行设置。在wetest平台运行时,每部手机会下发不同的账号,自定义276 账号保存在OTHERNAME和OTHERPWD。277 :usage:278 :param timeout:登陆超时时间,秒279 :return 登陆packagename退出,则认为登陆成功True。超时还没登陆,则返回False280 """281 import wpyscripts.uiautomator.login_tencent as login282 _current_pkgname = self.get_current_package()283 logger.info("Current Pkgname: " + _current_pkgname)284 if _current_pkgname == "com.tencent.mobileqq":285 account = os.environ.get("OTHERNAME") or os.environ.get("QQNAME")286 pwd = os.environ.get("OTHERPWD") or os.environ.get("QQPWD")287 if not account or not pwd:288 raise LoginError("no account or pwd,please check OTHERNAME or QQNAME environment")289 elif _current_pkgname == "com.tencent.mm":290 account = os.environ.get("OTHERNAME") or os.environ.get("WECHATNAME")291 pwd = os.environ.get("OTHERPWD") or os.environ.get("WECHATPWD")292 if not account or not pwd:293 raise LoginError("no account or pwd,please check OTHERNAME or WECHATNAME environment")294 else:295 account = os.environ.get("OTHERNAME")296 pwd = os.environ.get("OTHERPWD")297 if not account or not pwd:298 raise LoginError("no account or pwd,please check OTHERNAME or WECHATNAME environment")299 return login.login_tencent(account, pwd, timeout)300 def qq_account(self):301 """302 Get qq acount and password,when run wetest platorm,each mobile will allocate a account303 :return:tuple QQ account and password304 """305 qq_account = os.environ.get("QQNAME")306 qq_pwd = os.environ.get("QQPWD")307 return qq_account, qq_pwd308 def wx_account(self):309 """310 Get wechat acount and password,when run wetest platorm,each mobile will allocate a account311 :return:tuple of wechat account and password312 """313 weixin_account = os.environ.get("WECHATNAME")314 weixin_pwd = os.environ.get("WECHATPWD")315 return weixin_account, weixin_pwd316 def self_define_account(self):317 """318 Get self define account,your uploaded accounts will allocate to mobile319 :return: tupe of self define account and password320 """321 account = os.environ.get("OTHERNAME")322 pwd = os.environ.get("OTHERPWD")323 return account, pwd324 def start_handle_popbox(self, handle_package_pattern=None, handle_text_pattern=None):325 pass326 def stop_handle_popbox(self):327 pass328class CloudDevice(Device):329 """330 wetest平台运行时会创建该实现类331 """332 from libs.uiauto.uiautomator import AutomatorDevice333 def __init__(self, serial, _package_name, _activity, ui_device=AutomatorDevice()):334 super(CloudDevice, self).__init__(serial, _package_name, ui_device)335 self.package_name = _package_name336 self.launch_activity = _activity337 self.pid = None338 self.timeout = 3000339 self.platform_client = platform.get_platform_client()340 @exception_call_super341 def get_display_size(self):342 """343 返回屏幕长宽344 :Usage:345 device=manage.get_device()346 display=device.get_display_size()347 :return:348 a instance of DisplaySize349 :rtype: DisplaySize350 :raise: WeTestPlatormError351 """352 response = self.platform_client.get_display_size()353 self.width = response["width"]354 self.height = response["height"]355 return DisplaySize(response["width"], response["height"])356 def get_top_package_activity(self):357 """358 获取当前手机的顶层的Activity名称和package名称359 :Usage:360 device=manage.get_device()361 activity=device.get_top_package_activity()362 :return:363 a instance of TopActivity364 :rtype: TopActivity365 :raise: WeTestPlatormError366 """367 return TopActivity("", self.ui_device.info["currentPackageName"])368 def launch_app(self, package=None, activity=None):369 """370 启动游戏,游戏启动后返回。返回启动后的Pid和拉起时间,device实例会保存Pid的值371 :param package:372 None的情况下会拉起,当前测试app373 :Usage:374 >>> import wpyscripts.manager as manager375 >>> device=manager.get_device()376 >>> device.launch_app()377 :return:(234,2341)分别代表打起后的pid和拉起时间378 :rtype tuple,379 """380 if package is not None:381 package_name = package382 elif self.package_name is not None:383 package_name = self.package_name384 else:385 raise WeTestInvaildArg("Error,Unknow app packagename")386 if activity is not None:387 launcher = activity388 elif self.launch_activity is not None:389 launcher = self.launch_activity390 else:391 launcher = "android.intent.category.LAUNCHER"392 if len(package_name) >= 128:393 raise WeTestInvaildArg("Error,Package name {0} length more than 128")394 logger.debug("Launch app {0}".format(package_name))395 response = self.platform_client.launch_app(package_name, launcher)396 return response397 @exception_call_super398 def get_rotation(self):399 """400 get mobile current rotation401 orienting the devie to left/right or natural.402 left/l: rotation=90 , displayRotation=1403 right/r: rotation=270, displayRotation=3404 natural/n: rotation=0 , displayRotation=0405 upsidedown/u: rotation=180, displayRotation=2406 :return: 0,1,2,3407 """408 response = self.platform_client.get_rotation()409 return response["rotation"]410 @exception_call_super411 def clear_data(self, package=None):412 if package is None:413 raise WeTestInvaildArg("Error,No app packagename")414 response = self.platform_client.clear_app_data(package)415 if response:416 return response417 @exception_call_super418 def _clear_qq_account(self):419 self.clear_data("com.tencent.mobileqq")420 self.clear_data("com.tencent.mm")421 @exception_call_super422 def _clear_user_info(self, package=None):423 if package is not None:424 package_name = package425 elif self.package_name is not None:426 package_name = self.package_name427 else:428 raise WeTestInvaildArg("Error,Unknow app packagename")429 if len(package_name) >= 128:430 raise WeTestInvaildArg("Error,Package name {0} length more than 128")431 platform.get_platform_client().procdied_report(False)432 time.sleep(3)433 self.clear_data(package_name)434 time.sleep(3)435 platform.get_platform_client().procdied_report(True)436class NativeDevice(Device):437 """438 在本地运行调试时,会创建439 """440 def __init__(self, serial, ui_device):441 super(NativeDevice, self).__init__(serial, None, ui_device)442 """443 main.py中可以进行全局本地测试时的配置,但是测试的时候不一定会从main运行,这个时候就需要在这个地方配置账号和密码444 """445 if not os.environ.get("QQNAME",None):446 os.environ["QQNAME"] = Account.QQNAME447 os.environ["QQPWD"] = Account.QQPWD448 if not os.environ.get("WECHATNAME",None):449 os.environ["WECHATNAME"] = Account.WECHATNAME450 os.environ["WECHATPWD"] = Account.WECHATPWD451 def start_handle_popbox(self, handle_package_pattern=None, handle_text_pattern=None):452 if not handle_package_pattern:453 handle_package_pattern = u"^(?!(com.tencent.mm|com.tencent.mqq|com.tencent.mobileqq|com.android.contacts|com.android.mms|com.yulong.android.contacts|com.android.dialer|com.android.keyguard|com.tencent.mm.coolassist|com.example.test.wegame.TestMainPanel|cn.uc.gamesdk.account)$).*"454 if not handle_text_pattern:455 handle_text_pattern = u"(^(完成|关闭|关闭应用|好|好的|确定|确认|安装|下次再说|知道了)$|(.*(?<!不)(忽略|允(\s){0,2}许|同意)|继续|清理|稍后|暂不|强制|下一步).*)"456 logger.debug(u"monitor package pattern {0}".format(handle_package_pattern))457 logger.debug(u"monitor click text {0}".format(handle_text_pattern))458 self.ui_device.start_pop_monitor(handle_package_pattern, handle_text_pattern)459 def stop_handle_popbox(self):460 logger.debug("close pop box handler")...

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:

YouTube

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

Run fMBT 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?

Helpful

NotHelpful