class MakeLvCase(Login): @classmethod def setUpClass(cls): super(MakeLvCase, cls).setUpClass() @classmethod def teardownclass(cls): super(MakeLvCase, cls).tearDownClass() # stop_app("com.gameholic.drawsomethingbyspider") def setUp(self): stop_app("com.gameholic.drawsomethingbyspider") clear_app("com.gameholic.drawsomethingbyspider") wake() home() start_app("com.gameholic.drawsomethingbyspider") sleep(7) self.poco = UnityPoco() print("make level test start") def enterMakelevel(self): # self.poco = UnityPoco() self.poco("MakeLV").click() sleep(1) if self.poco("Up").child("Text").exists(): print("enter make level successfully") def lookHelp(self): # self.poco = UnityPoco() self.poco("Explain").click() sleep(1) if self.poco("txt").exists(): print("look help interface successfully") self.poco(text="OK").click() if not self.poco("txt").exists(): print("exit help interface successfully") def exitMylevel(self): # self.poco = UnityPoco() self.poco("Return").click() sleep(2) if not self.poco(text="My level").exists(): print("exit my level interface successfully") def swipeMylevel(self): # self.poco = UnityPoco() if len(self.poco("Tree").child().nodes) >= 5: self.poco("Tree").child()[0].child("idtext").get_position() beforeSwipe = self.poco("Tree").child()[0].child( "idtext").get_position() self.poco.swipe([100.0 / 1920, 100.0 / 1080], [100.0 / 1920, -200.0 / 1080]) sleep(2) afterSwipe = self.poco("Tree").child()[0].child( "idtext").get_position() if beforeSwipe != afterSwipe: print("swipe successfully") else: print("swipe failed") def addLevel(self): # self.poco = UnityPoco() self.poco("PlayerMakeLevel").click() if self.poco(text="Disclaimer").exists(): print("open disclaimer interface successfully") def closeDisclaimer(self): # self.poco = UnityPoco() self.poco(text="Cancel").click() sleep(1) if not self.poco(text="Disclaimer").exists(): print("close disclaimer successfully") def agreeDisclaimer(self): # self.poco = UnityPoco() self.poco(text="I Agree").click() sleep(1) if self.poco(text="Choose from album").exists(): print("enter make level interface successfully") def switchLebel(self): # self.poco = UnityPoco() self.poco(text="Monster").click() sleep(1) if self.poco("SetMonster_D").exists(): print("switch to monster interface successfully") self.poco(text="Publish").click() sleep(1) if self.poco("Save_Releaseit").exists(): print("switch to publish interface successfully") self.poco(text="Background").click() sleep(1) if self.poco(text="Choose from album").exists(): print("switch to background interface successfully") def closeMakeLevel(self): # self.poco = UnityPoco() self.poco("Return").click() sleep(2) if not self.poco(text="Background").exists(): print("close make level interface successfully") def chooseAlbum(self): self.poco(text="Choose from album").click() sleep(5) self.poco1 = AndroidUiautomationPoco(force_restart=False) if self.poco1("android.widget.TextView").exists(): print("open album successfully") shell("input keyevent 4") #关闭选择图片 if not self.poco1("android.widget.TextView").exists(): print("close album successfully") def takePhotos(self): self.poco(text="Take a photo").click() sleep(5) self.poco1 = AndroidUiautomationPoco(force_restart=False) if self.poco1( "com.google.android.GoogleCamera:id/shutter_button").exists(): print("open camera successfully") shell("input keyevent 4") if not self.poco1( "com.google.android.GoogleCamera:id/shutter_button").exists(): print("close camera successfully") def addSuccessfully(self): #红米note2 self.poco(text="Take a photo").click() sleep(5) self.poco1 = AndroidUiautomationPoco(force_restart=False) if self.poco1( "com.android.camera:id/v6_shutter_button_internal").exists(): self.poco1( "com.android.camera:id/v6_shutter_button_internal").click() sleep(1) self.poco1( "com.android.camera:id/v6_btn_done").wait_for_appearance() if self.poco1("com.android.camera:id/v6_btn_done").exists(): self.poco1("com.android.camera:id/v6_btn_done").click() sleep(5) if self.poco1("com.miui.gallery:id/wallpaper_apply").exists(): self.poco1("com.miui.gallery:id/wallpaper_apply").click() sleep(1) def addMonster(self): # self.poco = UnityPoco() self.poco(text="Monster").click() sleep(1) #添加怪物预览 if self.poco("Tree").child().exists(): self.poco("Tree").child()[0].click() sleep(1) if self.poco("usedlingdaoli").get_text() != 0: print("add monster successfully") self.poco(text="Preview").click() sleep(2) if self.poco("Mask").exists(): print("preview successfully") self.poco.click([0.1, 0.1]) sleep(2) if not self.poco("Mask").exists(): print("exit preview successfully") #滑动怪物选择,添加超出统率力情况 if self.poco("Monster_1122").exists(): self.swipeMonster() self.poco(text="Reset monster").click() sleep(1) if self.poco("usedlingdaoli").get_text() == 0: print("reset ok") self.poco("Monster_1001").click() def swipeMonster(self): # self.poco = UnityPoco() self.poco.swipe([1000.0 / 1080, 1660.0 / 1920], [10.0 / 1080, 1660.0 / 1920], duration=0.2) sleep(2) pos = self.poco("Monster_1122").get_position() if min(pos) > 0: for _ in range(3): self.poco("Monster_1122").click() sleep(1) if "Your leadership is not" in self.poco("lable").get_text(): print("leadership not enough ok") self.poco(text="OK").click() sleep(1) if not self.poco("lable").exists(): print("close leadership not enough interface successfully") self.poco(text="Reset monster").click() self.poco.swipe([10.0 / 1080, 1660.0 / 1920], [1000.0 / 1080, 1660.0 / 1920], duration=0.2) def switchPrivacy(self): # self.poco = UnityPoco() self.poco(text="Public").click() sleep(1) assert_exists( Template(r"tpl1526632435537.png", threshold=0.9, target_pos=5, rgb=False, record_pos=(-0.384, -0.207), resolution=(576, 1024)), "switch to Open pass") self.poco("OnlyFriend").click() sleep(1) assert_exists( Template(r"tpl1526632538433.png", threshold=0.9, target_pos=5, rgb=False, record_pos=(-0.295, -0.127), resolution=(576, 1024)), "switch to Just pass") self.poco("OnlyMe").click() sleep(1) assert_exists( exists( Template(r"tpl1526632587864.png", threshold=0.9, target_pos=5, rgb=False, record_pos=(-0.267, -0.042), resolution=(576, 1024))), "switch to Publicity pass") def textLevelInfo(self): # self.poco = UnityPoco() self.poco("LvNameInput").click() sleep(1) now = time.strftime('%H%M%S') global title title = "test" + now text(title) self.poco1 = AndroidUiautomationPoco(force_restart=False) if self.poco1("android.widget.Button").exists(): self.poco1("android.widget.Button").click() self.poco("DesInput").click() des = "make at " + now text(des) if self.poco1("android.widget.Button").exists(): self.poco1("android.widget.Button").click() self.poco("Save_Releaseit").click() def publish(self): # self.poco = UnityPoco() self.poco(text="Publish").click() sleep(1) self.poco("Save_Releaseit").click() sleep(1) popupText = self.poco("lable").get_text() #提交成功 if "Your stage has" in popupText: print("make a level successfully") self.poco(text="OK").click() sleep(2) if self.poco(text="My level").exists(): print("return to my level ok") #缺少怪物 if popupText == "The stage must contain a monster": print("no monster can't make a level ok") self.poco("BG").child("Button").click() sleep(1) self.addMonster() #缺少背景图 if popupText == "The stage must contain a background image": print("no backgroud can't make a level") self.poco("BG").child("Button").click() sleep(1) #切换background标签 self.poco(text="Background").click() sleep(1) #添加背景 #使用相册 self.chooseAlbum() #使用相机 self.takePhotos() #成功添加背景 self.addSuccessfully() #缺少名字 if popupText == "The stage must have a name": print("no name can't make a level pass") self.poco("BG").child("Button").click() #添加名称 self.textLevelInfo() def modifyLevel(self): # self.poco = UnityPoco() if self.poco("Tree").child()[0].exists(): self.poco("Tree").child()[0].child("Lvupdate").child( "Text").click() sleep(1) if self.poco(text="Background").exists(): print("enter modify interface successfully") self.poco(text="Publish").click() sleep(1) self.poco("Save_Releaseit").click() sleep(1) popupText = self.poco("lable").get_text() if "You have successfully modified" in popupText: print("modified level successfully") self.poco(text="OK").click() sleep(1) def updatePrivacy(self): # self.poco = UnityPoco() self.poco("Tree").child()[0].child("Lvmode").child("Label").click() sleep(1) self.poco("Item 1: Friends only ").click() sleep(1) if self.poco("lable").get_text() == "Stage changed successfully": print("update to friends only ok") self.poco(text="OK").click() self.poco(text="Friends only ").click() sleep(1) self.poco("Item 2: Only yourself").click() sleep(1) if self.poco("lable").get_text() == "Stage changed successfully": print("update to only yourself ok") self.poco(text="OK").click() self.poco(text="Only yourself").click() sleep(1) self.poco("Item 0: Public").click() sleep(1) if self.poco("lable").get_text() == "Stage changed successfully": print("update to public ok") self.poco(text="OK").click() def deleteLevel(self): # self.poco = UnityPoco() self.poco("Tree").child()[0].child("Lvdelete").child("Text").click() sleep(1) if self.poco( "lable").get_text() == "Please confirm to delete this stage": print("open delete interface successfully") self.poco(text="Cancel").click() sleep(1) if not self.poco("lable").exists(): print("close delete interface successfully") beforeDelete = len(self.poco("Tree").child().nodes) self.poco("Tree").child()[0].child("Lvdelete").child("Text").click() sleep(1) self.poco("ConfirmBtn").click() sleep(2) afterDelete = len(self.poco("Tree").child().nodes) if (afterDelete + 1) == beforeDelete: sleep(1) print("delete level pass") def testMakeLv(self): self.permissionClick() self.autoUpdate() self.login("wn10001", "z123456") self.waitLogin() sleep(5) #进入制作关卡 self.enterMakelevel() #查看帮助 self.lookHelp() #退出关卡 self.exitMylevel() #再次进入制作关卡 self.enterMakelevel() #内网可以测试滑动我的关卡 self.swipeMylevel() #添加关卡 self.addLevel() #关闭免责声明 self.closeDisclaimer() self.addLevel() #同意免责声明 self.agreeDisclaimer() #测试切换标签 self.switchLebel() #关闭制作关卡 self.closeMakeLevel() self.addLevel() self.agreeDisclaimer() #发布关卡缺少怪物失败,添加怪物 self.publish() #发布关卡缺少背景失败,添加背景 self.publish() #发布关卡缺少名称失败,添加名称 self.publish() #发布成功 self.publish() #测试关卡修改 self.modifyLevel() self.updatePrivacy() self.swipeMylevel() self.deleteLevel() #结束测试 stop_app("com.gameholic.drawsomethingbyspider") sleep(2.0) snapshot(msg="app stopped") print("finish test")
#登入模式可能有引导,不管是否出现引导都关闭重开(过引导) DHM_login_facebook() #购买金币 #购买需要用到Android原生POCO from poco.drivers.android.uiautomation import AndroidUiautomationPoco androidPoco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False) if poco(name="Coin").exists(): poco("Coin").click() sleep(5) if exists( Template(r"tpl1572415016673.png", record_pos=(0.006, -0.199), resolution=(2560, 1440))): poco.click([0.42, 0.85]) sleep(5) androidPoco("一键购买").wait(5).click() sleep(15) poco.click([0.5, 0.8]) sleep(5) poco.click([0.5, 0.8]) stop_app(app_id) sleep(10) clear_app(app_id) ############################################################################################ from airtest.core.api import * from airtest_selenium.proxy import WebChrome game_id = '600164'
class Login(unittest.TestCase): @classmethod def setUpClass(cls): connect_device("android:///") cls.poco1 = AndroidUiautomationPoco(force_restart=False) @classmethod def tearDownClass(cls): stop_app("com.gameholic.drawsomethingbyspider") def setUp(self): stop_app("com.gameholic.drawsomethingbyspider") clear_app("com.gameholic.drawsomethingbyspider") wake() home() start_app("com.gameholic.drawsomethingbyspider") sleep(7) self.poco = UnityPoco() print("test start") #自动更新函数 def permissionClick(self): self.poco1 = AndroidUiautomationPoco() while self.poco1("com.android.packageinstaller:id/permission_message").exists(): self.poco1("com.android.packageinstaller:id/permission_allow_button").click() sleep(1) def autoUpdate(self): # self.poco = UnityPoco() sleep(2) loadingTime = 0 try: while self.poco("tip1").exists(): sleep(5) loadingTime += 5 if loadingTime >= 300: if "url" in self.poco("error").get_text(): print("更新失败") stop_app("com.gameholic.drawsomethingbyspider") else: sleep(5) except RuntimeError as e: raise e #登录函数 def login(self, username, password): self.poco1 = AndroidUiautomationPoco() if self.poco1(text="Sign in").exists(): self.poco1(text="Sign in").click() sleep(1.0) self.poco1("android.widget.LinearLayout").offspring("android.widget.RelativeLayout").child("android.widget.LinearLayout").child("android.widget.LinearLayout").child("android.widget.LinearLayout").child("android.widget.ImageView").click() sleep(1) self.poco1(text="Username").click() sleep(1) text(username) sleep(1) self.poco1("android.widget.LinearLayout").offspring("android.widget.RelativeLayout").child("android.widget.LinearLayout").child("android.widget.LinearLayout")[2].child("android.widget.EditText").click() sleep(1) #有的手机text()方法不起作用 shell("input keyboard text %s"%password) shell("input keyevent 4") sleep(1) self.poco1("android.widget.LinearLayout").offspring("android.widget.RelativeLayout").child("android.widget.LinearLayout").child("android.widget.LinearLayout")[3].child("android.widget.TextView").click() sleep(7) else: print("登录弹框未出现") start_app("com.gameholic.drawsomethingbyspider") def waitLogin(self): # self.poco = UnityPoco() while self.poco("Text_Percent").exists(): sleep(5) if self.poco("FBIWorning(Clone)").exists(): self.poco("FBIWorning(Clone)").click() #检查是否存在离线金币,存在关闭 if self.poco("CoinNum").exists(): print("offline coin existed") self.poco.click([540.0/1080, 1210.0/1920]) if self.poco("StartGame").child().get_text() == "基础关卡": self.poco("Setting").click() sleep(1) if self.poco("SurprisePopup").child("Image").exists(): self.poco("SurprisePopup").child("Image").child("Cancel").click() sleep(1) self.poco("Arrow").click() sleep(1) self.poco("Item 0: English").child("Item Label").click() sleep(1) self.poco("option_top_x").click() sleep(1) if self.poco("StartGame").child().get_text() == "Basic Stage": print("login successfully!") def testLogin(self): self.permissionClick() self.autoUpdate() self.login("wn10001", "z123456") self.waitLogin()
class OkcUiTest(object): def __init__(self): self.app_name = config.okc_app_name self.default_wait_time = 1.0 self.default_save_path = "G:/TestData/okc_test/new_teach/" self.ui_pos_path = util.get_ini_data(ini_path=config.conf_path, section="path", section_item="ui_pos") # util.get_ini_data(config.conf_path, section="path", section_item="okc_test_image_dir") self.phone = PhoneManager() self.ui_node = UiNodeData() self.android_object = None self.poco: UnityPoco = None self.click_wait_time = 0.3 self.target_node_pos = tuple() self.ui_pos = util.read_json_file(file_path=self.ui_pos_path) self.start_game() def __init_poco(self): self.poco = UnityPoco() def __init_devices(self): if self.android_object is not None: helper.G.add_device(dev=self.android_object) def update_ui_node(self): self.ui_node.update_node_data( node_data=self.poco.agent.hierarchy.dump()) def start_game(self): android_object = self.phone.app_start(self.app_name) try_count = 0 while try_count < 10: try_count += 1 logging.error("第 %s 次尝试更新UI节点信息失败" % try_count) try: self.__init_poco() self.update_ui_node() logging.info("更新UI节点信息成功") break except (ConnectionResetError, ConnectionAbortedError): time.sleep(self.default_wait_time) continue return android_object def find_ui_node(self, ui_node_name): find_result = self.ui_node.find_node(target_key=ui_node_name) self.update_ui_node() return find_result def get_pos(self, node_name): try: node_pos = self.poco(node_name).get_position() node_size = self.poco(node_name).get_size() node_pos_x = node_pos[0] + node_size[0] / 2 node_pos_y = node_pos[1] + node_size[1] / 2 if node_pos_x >= 1: node_pos_x = 0.99 if node_pos_x <= 0: node_pos_x = 0.01 if node_pos_y >= 1: node_pos_y = 0.99 if node_pos_y <= 0: node_pos_y = 0.01 self.target_node_pos = node_pos_x, node_pos_y logging.info("已找到 %s 节点" % node_name) except PocoNoSuchNodeException: logging.info("没找到 %s 节点" % node_name) return self.target_node_pos def get_pos_by_node(self, node_name) -> tuple: self.target_node_pos = tuple() try_count = 0 while try_count < 5: if self.find_ui_node(node_name): node_pos = self.poco(node_name).get_position() node_size = self.poco(node_name).get_size() node_pos_x = node_pos[0] + node_size[0] / 2 node_pos_y = node_pos[1] + node_size[1] / 2 if node_pos_x >= 1: node_pos_x = 0.99 if node_pos_x <= 0: node_pos_x = 0.01 if node_pos_y >= 1: node_pos_y = 0.99 if node_pos_y <= 0: node_pos_y = 0.01 self.target_node_pos = node_pos_x, node_pos_y logging.info("已找到名为 %s 节点" % node_name) logging.info(f"节点坐标为 : {self.target_node_pos} ") break else: self.update_ui_node() try_count += 1 logging.warning("尝试第 %d 次寻找" % try_count) logging.warning("没有找到名为 %s 节点" % node_name) api.sleep(self.default_wait_time) continue if try_count == 5: logging.error("find ui node failed, node name : %s" % node_name) else: logging.info("node name : %s ; node position : %s" % (node_name, self.target_node_pos)) return self.target_node_pos def click_by_node_name(self, view, node_name, can_change=False, result_view="") -> bool: if can_change: node_pos = self.get_pos(node_name) elif view in self.ui_pos.keys(): if node_name in self.ui_pos[view].keys(): node_pos = tuple(self.ui_pos[view][node_name]) else: node_pos = self.get_pos(node_name) self.ui_pos[view][node_name] = list(node_pos) util.write_json_file(file_path=self.ui_pos_path, data=self.ui_pos) else: node_pos = self.get_pos(node_name) self.ui_pos[view] = {node_name: list(node_pos)} util.write_json_file(file_path=self.ui_pos_path, data=self.ui_pos) logging.info("%s 的坐标为 : %s" % (node_name, node_pos)) if node_pos != (): self.poco.click(pos=node_pos) api.sleep(self.click_wait_time) self.update_ui_node() if result_view == "": return True if self.find_ui_node(ui_node_name=result_view): return True else: logging.error("点击%s后,没有打开%s" % (node_name, result_view)) return False else: self.phone.get_screen_shot(node_name, self.default_save_path) logging.error("点击失败,没找到UI坐标") return False def get_screenshots(self, image_name, save_path=""): if save_path == "": save_path = "%s%s.png" % (self.default_save_path, image_name) else: save_path = "%s/%s.png" % (save_path, image_name) logging.debug("save path : %s" % save_path) self.phone.get_screen_shot(image_name=image_name, save_path=save_path) def login(self, is_new_account=False, uid=10340): if config.okc_environment == config.OkcEnvironment.Dev.value: environment_node_name = "dev_btn" elif config.okc_environment == config.OkcEnvironment.OnlineTest.value: environment_node_name = "online_test_btn" else: environment_node_name = "test_btn" if is_new_account: node_name_fake_sdk = "tab_2" node_name_new = "new_btn" self.click_by_node_name(view="ViewEnvironmentSelect", node_name=node_name_fake_sdk) self.click_by_node_name(view="ViewEnvironmentSelect", node_name=node_name_new) else: uid_node_name = "text" uid_tab_node_name = "tab_1" self.click_by_node_name(view="ViewEnvironmentSelect", node_name=uid_tab_node_name) old_uid = str(self.poco(uid_node_name).get_text()) if not old_uid == str(uid): uid_input_node_name = "uid_input" if self.click_by_node_name(view="ViewEnvironmentSelect", node_name=uid_input_node_name): old_uid_size = len(old_uid) while old_uid_size > 0: api.keyevent("KEYCODE_DEL") old_uid_size -= 1 api.sleep(self.default_wait_time) api.text(str(uid)) self.click_by_node_name(view="ViewEnvironmentSelect", node_name=environment_node_name) if self.click_by_node_name(view="ViewEnvironmentSelect", node_name=environment_node_name): loading_time = 0 loading_start_time = time.time() while True: if loading_time >= 120: logging.error("loading 时间超过两分钟,异常....") self.get_screenshots(image_name="Loading_Error_%s" % int(loading_start_time)) break if self.find_ui_node(ui_node_name="ViewLoading"): logging.info("还在loading中...") time.sleep(self.default_wait_time) loading_time = time.time() - loading_start_time self.update_ui_node() else: logging.info("loading 完毕") logging.info("此次loading耗时小于 %s 秒" % loading_time) break else: logging.error("环境选择按钮点击失败....")
class PdjsMainPage: main_package = "shangyoo.noahmobile.com" main_phone = "Android:///" package_path = r'C:\Users\majiexiong\Desktop\shangyoo.noahmobile.com_15.2.291.apk' def __init__(self): '''初始化设备''' self.device = connect_device(self.main_phone) auto_setup(__file__, logdir=False, devices=self.main_phone) self.poco = UnityPoco() self.setting() self.start_app() # def authorization(self): # '''第一次授权''' # f = yaml_load.yaml_load(r'../quanxian.yml') # f = list(set(f['quanxian'])) # for i in f: # print('adb shell pm grant {} {}'.format(self.main_package,i)) # os.popen('adb shell pm grant {} {}'.format(self.main_package,i)) def check_app(self): '''检查app''' list_app = self.device.list_app() if self.main_package not in list_app: print('安装包体中') self.device.install_app(self.package_path, self.main_package) start_app(self.main_package) time.sleep(10) self.poco = UnityPoco() else: print('包体已安装') start_app(self.main_package) time.sleep(10) self.poco = UnityPoco() def setting(self): '''全局设置''' ST.FIND_TIMEOUT = 30 # 隐式等待 ST.SNAPSHOT_QUALITY = 70 # 图片精度 def report(self): '''报告生成''' simple_report( __file__, logpath= r'C:\Users\majiexiong\PycharmProjects\airtest_demo\center\log', output= r'C:\Users\majiexiong\PycharmProjects\airtest_demo\center\log\log.html' ) def find(self, *element): '''基本方法查找''' if len(element) == 1: self.poco("{}".format(element[0])).wait_for_appearance() return self.poco("{}".format(element[0])) elif len(element) == 2: self.poco("{}".format(element[0])).child("{}".format( element[1])).wait_for_appearance() return self.poco("{}".format(element[0])).child("{}".format( element[1])) elif len(element) == 3: self.poco("{}".format(element[0])).child("{}".format( element[1])).child("{}".format( element[2])).wait_for_appearance() return self.poco("{}".format(element[0])).child("{}".format( element[1])).child("{}".format(element[2])) def find_click(self, *element): '''基本点击''' return self.find(*element).click() def find_long_click(self, *element): '''基本长点击''' return self.find(*element).long_click() def find_chirden(self, *element): '''基本查找子节点所有子节点''' return self.find(*element).children() def find_text(self, *element): '''查找当前ui下文字''' return self.find(*element).get_text() def up_swipe(self): '''上滑''' self.width, self.height = self.device.get_current_resolution() start_pt = (self.width * 0.7, self.height * 0.7) end_pt = (self.width * 0.7, self.height * 0.3) swipe(start_pt, end_pt) def down_swipe(self): '''下滑''' self.width, self.height = self.device.get_current_resolution() start_pt = (self.width * 0.7, self.height * 0.3) end_pt = (self.width * 0.7, self.height * 0.7) swipe(start_pt, end_pt) def left_swipe(self): '''左滑''' self.width, self.height = self.device.get_current_resolution() start_pt = (self.width * 0.3, self.height / 2) end_pt = (self.width * 0.7, self.height / 2) swipe(start_pt, end_pt) def right_swipe(self): '''右滑''' self.width, self.height = self.device.get_current_resolution() start_pt = (self.width * 0.7, self.height / 2) end_pt = (self.width * 0.3, self.height / 2) swipe(start_pt, end_pt) def click_center(self): '''点击屏幕中央''' self.width, self.height = self.device.get_current_resolution() self.poco.click([0.1, 0.5]) def start_app(self): ''' 启动app :return: ''' start_app(self.main_package) def stop_app(self): '''停止app''' stop_app(self.main_package) def assert_type(self, testname, authname, testtitle): '''判断字体是否存在''' try: assert_equal(testname, authname, testtitle) except AssertionError: print('文字判定不对,错误字体{}'.format(testname)) def assert_not_view(self, locator, other_locator): '''判断不存在元素,并创造测试分支''' try: assert_not_exists(locator, '元素{},寻找不到,忽略此步骤'.format(locator)) except: self.poco.click(other_locator) def assert_view(self, locator): '''判断元素是否存在''' try: assert_exists(locator, '该元素{},寻找不到'.format(locator)) except: print('没有找到元素{}'.format(locator)) def screeen_size(self): '''获取当前屏幕尺寸''' self.width, self.height = self.device.get_current_resolution() return self.width, self.height
class UnityPoco(): def __init__(self): self.poco = UnityPoco() def login_unity(self, game_server): """ 到点击登录按钮的login """ self.poco.click([0.05, 0.163]) time.sleep(1) self.poco.click([0.1, 0.13]) os.system(r'adb shell input text ' + game_server) self.poco.click([0.934, 0.867]) self.poco.click([0.03, 0.13]) self.poco("ObjLoginBtns").child("1").child('Image').click() try: self.poco("horizontalBtns").wait_for_appearance(timeout=5) self.poco("BtnOk").click() except Exception: print('已通过权限检测') def login_touch(self): self.poco("ObjLoginBtns").child("1").child('Image').click() def skip_anime(self): try: self.poco("UIPopupPlayVideo").wait_for_appearance(timeout=8) print('已进入到播放动画界面') try: self.poco("txtSkip").click() self.poco("BtnOk").click() except Exception: self.poco("UIPopupPlayVideo").click() self.poco("txtSkip").click() self.poco("BtnOk").click() except Exception: print('本次客户端没有播放动画') def board(self): try: self.poco("UIPopupLoginNotice").wait_for_appearance(timeout=5) for _ in range(3): try: fp = self.poco.freeze() try: fp("CloseBtn").click() except: fp("ObjBtns").child("BtnOk").click() except: break except Exception: print('无游戏公告') def get_ready(self): """ 到大厅准备好,可以直接调用aico的run的方法,这个写完,前面的连起来就可以用了!! """ def _sign(): """ 日常签到的关闭 """ if self.poco("BtnSignIn").exists(): self.poco("BtnSignIn").click() self.poco("btns").child("btnOk").click() self.poco("topBG").child("Back").click() def _bfi_close(): """ 最烦人的拍脸图关闭 """ for _ in range(30): if self.poco("btnClose").exists(): self.poco("btnClose").click() elif self.poco("UIHallSeasonStart").offspring( "BtnOk").exists(): self.poco("UIHallSeasonStart").offspring( "BtnOk").click() elif self.poco("Close").exists() != False: self.poco("Close").click() elif self.poco("nextText").exists(): self.poco("nextText").click() elif self.poco("Close").exists(): self.poco("Close").click() elif self.poco("CloseBtn").exists(): self.poco("CloseBtn").click() elif self.poco("point").offspring("BtnClose").exists(): self.poco("point").offspring("BtnClose").click() elif self.poco("X").exists() != False: self.poco("X").click() elif self.poco("EnContent").child("Back").exists(): self.poco("EnContent").child("Back").click() elif self.poco("empty").exists(): try: self.poco("empty").click() except Exception: pass else: break time.sleep(0.5) # 如果是新角色 需要创建角色 if self.poco("btnCreatPlayer").exists(): self.poco("btnCreatPlayer").click() self.poco("inputName").set_text("萝卜头") self.poco("BtnConfirm").click() self.poco("Button_Leave").click() else: print('不是新用户 直接进入到主界面') # 如果存在向导 if self.poco("Panel").child("Guider").exists(): self.poco.click([0.057, 0.96]) else: print('直接进入到了游戏 又不是新角色 也过了引导') # 这里就可以选择模式了 _bfi_close() try: _sign() except Exception: pass now_mode = self.poco("txtModeName").child("txt_main").get_text() if '单人' not in now_mode: self.poco("CityMode").click() time.sleep(1) self.poco.swipe([0.1, 0.5], [0.55, 0.5]) for i in range(7): mode_name = \ self.poco("UIHallSelectMode").offspring("Mask").child("Content").child("UIModeItem(Clone)")[ i].offspring( "PnlCountent1").children()[2].get_text() if '单人休闲模式' in mode_name: self.poco("UIHallSelectMode").offspring("Mask").child( "Content").child("UIModeItem(Clone)")[0].offspring( "PnlCountent1").click() break _bfi_close() _sign() def battle_remind(self): """ 处理破势的方法 """ def battle_start(): time.sleep(1) self.poco(texture="ui_zy_msxz_ks").click() self.poco("Panel").child("BtnOk").wait_for_appearance() self.poco("Panel").child("BtnOk").click() battle_start() self.poco("0").wait_for_appearance() time.sleep(4) # 卖棋子 self.poco("0").click() Adb_shell.tap([0.26, 0.89]) time.sleep(0.5) Adb_shell.swipe([0.26, 0.89], [0.055, 0.7]) time.sleep(1) try: self.poco("Checkmark").click() self.poco("Button_Ok").click() except Exception: pass # 刷棋盘 self.poco("DebugCanvas").child("Button").click() Adb_shell.swipe([0.74, 0.4], [0.74, 0.33]) time.sleep(2) self.poco("加满金币").child("Button").click() self.poco(text="关闭").click() try: self.poco("Button_Fresh").click() except Exception: self.poco("Button_Store").click() try: self.poco("Button_Fresh").click() except Exception: self.poco("Button_Store").click() self.poco("Button_Fresh").click() try: self.poco("Checkmark").click() self.poco("Button_Ok").click() except Exception: pass # 升人口 self.poco("Button_ReadChessBook").click() if self.poco("Checkmark").exists() == False: self.poco("Button_ReadChessBook").click() try: self.poco("Checkmark").click() self.poco("Button_Ok").click() except Exception: pass def battle_finish(self): self.poco("DebugCanvas").child("Button").click() self.poco("结束战斗").child("Button").click() self.poco("Close").child("Text").click() time.sleep(2) self.poco("Button_Leave").click() try: self.poco("btns").child("btnOk").click() except Exception: pass time.sleep(2) try: self.poco("btnContinue").click() except Exception: pass try: self.poco("Panle").child("textCloseDes").click() except Exception: pass try: self.poco("BtnClose").click() self.poco.click([0.02, 0.025]) except Exception: self.poco.click([0.02, 0.025]) try: self.poco("btnContinue").click() if self.poco("BtnClose").exists(): self.poco.click([0.02, 0.025]) except Exception: pass try: self.poco.click([0.02, 0.025]) self.poco("btns").child("btnOk").click() except Exception: pass
class RewardLevelCase(Login): @classmethod def setUpClass(cls): super(RewardLevelCase, cls).setUpClass() @classmethod def tearDownClass(cls): super(RewardLevelCase, cls).tearDownClass() def setUp(self): stop_app("com.gameholic.drawsomethingbyspider") clear_app("com.gameholic.drawsomethingbyspider") wake() home() start_app("com.gameholic.drawsomethingbyspider") sleep(7) self.poco = UnityPoco() assert ("reward level test start") #进入悬赏关卡 def enterReward(self): self.poco("RewardLV").click() sleep(2) if self.poco("Upup").child("Text").get_text() == "Reward Stage": assert ("进入悬赏关卡成功") #悬赏关卡帮助 def rewardHelp(self): self.poco("Explain").click() sleep(1) if "You can find" in self.poco("txt").get_text(): assert ("打开悬赏关卡成功") self.poco("confirm").click() sleep(1) if not self.poco("txt").exists(): assert ("关闭帮助界面成功") def sorting(self, whatTabel): items = self.poco("Tree").child() listReward = [] listRate = [] if whatTabel == "Overall": for item in items: listReward.append(item.child("reward").get_text()) if listReward[0] == max(listReward): assert ("人气排序正确") if whatTabel == "Popularity": for item in items: listReward.append(item.child("AllPlayerCount").get_text()) if listReward[0] == max(listReward): assert ("流行度排序正确") if whatTabel == "Difficulty": for item in items: challenge = item.child("AllPlayerCount").get_text() passStage = item.child("PassPlayerCount").get_text() if int(challenge) == 0: listRate.append(1) else: rate = float(passStage) / float(challenge) listRate.append(rate) if listRate[0] == min(listRate): assert ("难度排序正确") #切换标签 def changeLabel(self): self.poco(text="Popularity").click() self.poco("Tree").child().child("LvName").wait_for_appearance() self.sorting("Popularity") self.poco(text="Difficulty").click() self.poco("Tree").child().child("LvName").wait_for_appearance() self.sorting("Difficulty") self.poco(text="Overall").click() self.poco("Tree").child().child("LvName").wait_for_appearance() self.sorting("Overall") #上下滑动列表 def swipeList(self): if len(self.poco("Tree").child().nodes) > 5: beforeSwipe = self.poco("Tree").child()[0].child( "idtext").get_position() self.poco.swipe([500.0 / 1080, 1500.0 / 1920], [500.0 / 1080, 200.0 / 1920]) sleep(3) afterSwipe = self.poco("Tree").child()[0].child( "idtext").get_position() if beforeSwipe != afterSwipe: assert ("swipe list successfully") else: assert ("swipe list failed") #筛选弹框开启关闭 def filterOpen(self): self.poco(text="Filters").click() sleep(1) if self.poco(text="mine").exists(): assert ("筛选界面成功打开") self.poco("closed").click() sleep(1) #存在关闭弹框,但是未销毁弹框对象的bug,所以用get_position方式实现功能 pos = self.poco("My").get_position() if pos == [0.5, 0.5]: assert ("关闭筛选界面成功") self.poco("Return").click() sleep(1) self.poco("RewardLV").click() sleep(1) #进行筛选,去除所有人 def switchFliters(self): self.poco(text="Filters").click() sleep(1) while exists( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.188, -0.208), resolution=(1440, 2560))): touch( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.188, -0.208), resolution=(1440, 2560))) sleep(1) self.poco("My").click() sleep(1) assert_exists( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(0.095, -0.21), resolution=(1440, 2560)), "去除mine勾选成功") self.poco("My").click() sleep(1) self.poco("other").click() sleep(1) assert_exists( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(0.095, -0.21), resolution=(1440, 2560)), "other可以除去勾选框") self.poco("other").click() sleep(1) self.poco("Played").click() sleep(1) assert_exists( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(0.095, -0.21), resolution=(1440, 2560)), "not hide played today可以除去勾选框") def cancelAll(self): #去除所有勾选 while exists( Template(r"tpl1524898630127.png", threshold=0.9, target_pos=5, rgb=False, record_pos=(0.096, -0.211), resolution=(720, 1280))): touch( Template(r"tpl1524898630127.png", threshold=0.9, target_pos=5, rgb=False, record_pos=(0.096, -0.211), resolution=(720, 1280))) sleep(0.5) self.poco.click([360.0 / 720, 700.0 / 1280]) sleep(1) assert ("去掉所有人的关卡") if not self.poco("Tree").child().exists(): assert ("去掉所有人的关卡成功") #查看自己关卡 def onlyMine(self, player): self.poco(text="Filters").click() sleep(1) while exists( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.188, -0.208), resolution=(1440, 2560))): touch( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.188, -0.208), resolution=(1440, 2560))) sleep(0.5) self.poco("other").click() sleep(1) self.poco("Played").click() sleep(1) self.poco.click([360.0 / 720, 700.0 / 1280]) sleep(1) self.poco(text="Popularity").click() sleep(1) myLevels = self.poco("Tree").child() marklist = [] # player = self.poco("ToUserInfo").child("Text").get_text() for i in myLevels: if str(player) in i.child("LvMaker").get_text(): marklist.append(1) else: marklist.append(0) assert (marklist) if max(marklist) == min(marklist) and max(marklist) == 1: assert ("只保留自己的关卡成功") # 恢复关卡 def recoveryFilters(self): self.poco(text="Filters").click() while exists( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.188, -0.208), resolution=(1440, 2560))): touch( Template(r"tpl1522138307590.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.188, -0.208), resolution=(1440, 2560))) sleep(1) self.poco.click([360.0 / 720, 700.0 / 1280]) sleep(2) #进入关卡、退出关卡 def enterOneReward(self): self.poco("Tree").child()[0].child("PlayThisLv").click() sleep(1) self.poco("lable").wait_for_appearance() popText = self.poco("Popup").child("BG").child("lable").get_text() print(popText) #not enough coins if "Not enough coins!" == popText: assert ("金币不足无法进入成功") self.poco("Popup").child("BG").child("Button").click() if not self.poco("lable").exists(): sleep(10) assert ("金币不足弹框关闭成功") #coin enough if "You need to spend coins" in self.poco("lable").get_text(): print("打开消耗金币弹框成功") self.poco(text="Cancel").click() sleep(1) if self.poco("lable").get_position() == [0.5, 0.5]: assert ("取消关闭弹框成功") self.poco("Tree").child()[0].child("PlayThisLv").click() self.poco(text="OK").click() sleep(2) if self.poco("Hp").exists(): assert ("进入悬赏关卡成功") self.poco("Return2").click() sleep(1) if self.poco("lable").get_text( ) == "Are you sure you want to exit the game?": assert ("打开退出关卡弹框成功") self.poco(text="Cancel").click() sleep(1) if not self.poco("lable").exists(): assert ("取消退出关卡成功") self.poco("Return2").click() sleep(1) self.poco(text="OK").click() sleep(1) if self.poco("Upup").exists(): assert ("退出悬赏关卡成功") #点赞 def like(self): for _ in range(2): self.poco.swipe([360.0 / 720, 93.0 / 1280], [360.0 / 720, 870.0 / 1280], duration=0.2) sleep(2) self.poco("Center").offspring("Tree").child()[0].offspring( "Btn_DisAgree").click() sleep(1) beforeAgree = self.poco("Center").offspring("Tree").child( )[0].offspring("Btn_Agree").child("Text_AgreeNum").get_text() self.poco("Center").offspring("Tree").child()[0].offspring( "Btn_Agree").click() sleep(1) afterAgree = self.poco("Center").offspring("Tree").child( )[0].offspring("Btn_Agree").child("Text_AgreeNum").get_text() if int(beforeAgree) + 1 == int(afterAgree): assert ("点赞成功") #点踩 def hate(self): self.poco("Center").offspring("Tree").child()[0].offspring( "Btn_Agree").click() sleep(1) beforeAgree = self.poco("Center").offspring("Tree").child( )[0].offspring("Btn_DisAgree").child("Text_DisAgreeNum").get_text() self.poco("Center").offspring("Tree").child()[0].offspring( "Btn_DisAgree").click() sleep(1) afterAgree = self.poco("Center").offspring("Tree").child( )[0].offspring("Btn_DisAgree").child("Text_DisAgreeNum").get_text() if int(beforeAgree) + 1 == int(afterAgree): assert ("点踩成功") #评论 def comment(self): self.poco("Btn_Evaluate").click() sleep(1) popText = self.poco("lable").get_text() if popText == "Not enough coins !": assert ("金币不足无法评论成功") self.poco(text="OK").click() sleep(1) if popText == "You need to give a score!": assert ("评论星级不能为空成功") self.poco(text="OK").click() sleep(1) if popText == "Empty content!": assert ("评论不能为空成功") self.poco(text="OK").click() sleep(1) if "1000 coins" in popText: assert ("弹出花费金币成功") self.poco(text="Cancel").click() if not self.poco("lable").exists(): assert ("关闭花费金币弹框成功") self.poco("Btn_Evaluate").click() sleep(1) self.poco(text="Confirm").click() sleep(1) if popText == "Comment sent!": assert ("评论成功") self.poco(text="OK").click() sleep(1) if "Failed to send comment." in self.poco("lable").get_text(): assert ("没玩过关卡无法评论成功") self.poco(text="OK").click() sleep(1) #有没费次数时 if popText == "Comment sent!": assert ("评论成功") self.poco(text="OK").click() sleep(1) #评星 def startRating(self): self.poco("Bottom").child("Stars").child("Star3").click() sleep(1) assert_exists( Template(r"tpl1522211570186.png", record_pos=(-0.039, 0.443), resolution=(1440, 2560)), "评级成功") #输入评论文字 def enterCommnet(self): self.poco("Placeholder").click() sleep(1) text("good!") self.poco1("android.widget.Button").click() sleep(1) #举报 def reportLevel(self): self.poco(text="Report Stage").click() sleep(1) if self.poco("Toogle_EroticismOrPolitics").exists(): assert ("弹出举报弹框成功") self.poco(text="Cancel").click() sleep(1) if not self.poco("Toogle_EroticismOrPolitics").exists(): assert ("关闭举报弹框成功") self.poco(text="Report Stage").click() sleep(1) self.poco(text="Confirm").click() sleep(1) if self.poco("lable").get_text() == "You must choose an option!": assert ("不选择理由无法举报成功") self.poco(text="OK").click() sleep(1) self.poco("Toogle_EroticismOrPolitics").click() sleep(1) assert_exists( Template(r"tpl1522225879391.png", record_pos=(-0.313, -0.301), resolution=(1440, 2560)), "选择色情/政治选项成功") self.poco("Toogle_Advertisement").click() sleep(1) assert_exists( Template(r"tpl1522225998289.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.138, -0.19), resolution=(1440, 2560)), "选择广告选项成功") self.poco("Toogle_Tort").click() sleep(1) assert_exists( Template(r"tpl1522226039045.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.159, -0.082), resolution=(1440, 2560)), "选择侵权选项成功") self.poco("Toogle_Other").click() sleep(1) assert_exists( Template(r"tpl1524907944782.png", threshold=0.85, target_pos=5, rgb=False, record_pos=(-0.147, 0.032), resolution=(720, 1280)), "选择其它选项成") self.poco(text="Confirm").click() sleep(1) if self.poco("lable").get_text() == "Reason not specified!": assert ("其他理由无文字不能举报") self.poco(text="OK").click() sleep(1) self.poco("InputField_OtherReason").child("Text").click() sleep(1) text("take") self.poco1("android.widget.Button").click() sleep(1) if self.poco("InputField_OtherReason").child( "Text").get_text() == "take": assert ("其它输入文字成功") self.poco("InputField_Desc").child("Text").click() sleep(1) text("bad") self.poco("InputField_OtherReason").child("Text").click() sleep(1) if self.poco("InputField_Desc").child("Text").get_text() == 'bad': assert ("描述输入文字成功") self.poco(text="Confirm").click() sleep(1) if self.poco("lable").get_text() == "Reported": assert ("举报成功") self.poco(text="OK").click() sleep(1) #长按弹出他人信息弹框 def longPress(self): self.poco("Tree").child()[0].long_click() sleep(1) if self.poco("gotohomepage").exists(): assert ("弹出他人信息框成功") self.poco("share").sibling("Button").click() sleep(1) if not self.poco("gotohomepage").exists(): assert ("关闭他人信息框成功") self.poco("Tree").child()[0].child("thumbnail").click() sleep(1) self.poco(text="View author's homepage").click() self.poco("Text_Title").wait_for_appearance() sleep(1) if self.poco("Text_Title").exists(): assert ("查看他人信息成功") self.poco("Btn_Back").click() sleep(1) if not self.poco("Text_Title").exists(): assert ("退出他人信息界面成功") self.poco("Tree").child()[0].child("thumbnail").click() sleep(1) self.poco(text="Share this stage").click() sleep(1) if self.poco(text="Share this stage").get_text() == "Share this stage": assert ("分享关卡弹出成功") #share功能 if self.poco(text="Share").exists(): self.poco(text="Share").click() sleep(1) if self.poco(text="Shared").get_text() == "Shared": assert ("分享成功") #关闭分享 self.poco("sharePopup(Clone)").child("BG").child("Button").click() sleep(1) if not self.poco(text="Share this stage").exists(): assert ("关闭分享关卡成功") else: self.poco.click([355.0 / 720, 924.0 / 1280]) sleep(1) def getUserData(self): player = self.poco("ToUserInfo").child("Text").get_text() # mycoins = self.poco(texture="tongyong_jinbi").child("Text").get_text() return player def testRewardLevel(self): self.permissionClick() self.autoUpdate() self.login("wn10001", "z123456") self.waitLogin() sleep(7) player = self.getUserData() #进入悬赏关卡 self.enterReward() #查看帮助 self.rewardHelp() #切换标签测试 self.changeLabel() #列表滑动测试 self.swipeList() #筛选测试 #由于bug会复制filters,所以退出重进 self.filterOpen() self.switchFliters() self.cancelAll() self.recoveryFilters() #由于bug会复制filters,所以退出重进 self.poco("Return").click() sleep(1) self.poco("RewardLV").click() sleep(1) self.onlyMine(player) self.recoveryFilters() #具体关卡进入和退出 self.enterOneReward() #进入评论 self.poco("Tree").child()[0].child("Evaluate").click() sleep(1) #直接评论弹出星级不能为空 self.comment() # #输入星级再评论弹出文字不能为空 self.startRating() self.comment() # #输入文字再评论弹出评论成功 self.enterCommnet() self.comment() #点赞和踩 self.like() self.hate() #举报测试 self.reportLevel() self.poco("Btn_Back").click() sleep(1) #长按测试 self.longPress() stop_app("com.gameholic.drawsomethingbyspider") self.permissionClick() self.autoUpdate() self.login("wn10008", "z123456") self.waitLogin() sleep(5) self.enterReward() self.poco("Tree").child()[0].child("Evaluate").click() sleep(1) self.startRating() self.enterCommnet() self.comment() self.poco("Return").click() sleep(1) self.poco("Return").click() sleep(1) #结束测试 stop_app("com.gameholic.drawsomethingbyspider") sleep(2.0) snapshot(msg="app stopped") print("finish test")