博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【appium】appium中的元素定位和基本操作
阅读量:5276 次
发布时间:2019-06-14

本文共 4848 字,大约阅读时间需要 16 分钟。

1 # coding=utf-8  2   3 from appium import webdriver  4 import time  5 from selenium.webdriver.support.ui import WebDriverWait  6 from selenium.webdriver.support import expected_conditions as EC  7   8   9  10 class Base: 11  12     def __init__(self): 13         self.driver = self.get_driver() 14         self.size = self.get_size() 15  16     def get_driver(self): 17         capabilities = { 18             "platformName": "Android", 19             "automationName": "UiAutomator2",  # 测试平台,默认为appium,为了get_tost此处为automator2 20             "deviceName": "127.0.0.1:21513", 21             "app": "E:\\pythonAppium\\autoTest\\apps\\mukewang.apk", 22             "appWaitActivity": "cn.com.open.mooc.user.register.MCPhoneRegisterAty", 23             "noReset": "True",  # 是否重装 24             # "chromeOptions": {"androidProcess": "WEBVIEW_cn.com.open.mooc"} 25  26         } 27         driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities) 28         return driver 29  30     def get_size(self): 31         # 获取屏幕大小 32         size = self.driver.get_window_size() 33         width = size['width'] 34         height = size['height'] 35         return width, height 36  37     def swipe_right(self): 38         """ 39         从左向右滑 40         :return: 41         """ 42         x = self.get_size()[0]/10 43         x1 = self.get_size()[0]/10*9 44         y = self.get_size()[1]/2 45         self.driver.swipe(x, y, x1, y) 46  47     def swipe_left(self): 48         """ 49         从右向左滑 50         :return: 51         """ 52         x = self.get_size()[0]/10*9 53         x1 = self.get_size()[0]/10 54         y = self.get_size()[1]/2 55         self.driver.swipe(x, y, x1, y) 56  57     def swipe_up(self): 58         """ 59         从下往上滑 60         :return: 61         """ 62         x = self.get_size()[0]/2 63         y = self.get_size()[1]/10*9 64         y1 = self.get_size()[1]/10 65         self.driver.swipe(x, y, x, y1) 66  67     def swipe_down(self): 68         """ 69         从上往下滑 70         :return: 71         """ 72         x = self.get_size()[0]/2 73         y = self.get_size()[1]/10 74         y1 = self.get_size()[1]/10*9 75         self.driver.swipe(x, y, x, y1) 76  77     def swipe_on(self, direction): 78         if direction == 'left': 79             self.swipe_left() 80         elif direction == 'right': 81             self.swipe_right() 82         elif direction == 'up': 83             self.swipe_up() 84         else: 85             self.swipe_down() 86  87     def go_to_login(self): 88         """ 89         跳转到登录界面 90         :return: 91         """ 92         self.driver.find_element_by_id('cn.com.open.mooc:id/tv_go_login').click() 93  94     def login_by_id(self): 95         self.driver.find_element_by_id('cn.com.open.mooc:id/account_edit').send_keys('13055211990') 96         # self.drvier.find_element_by_id('cn.com.open.mooc:id/password_edit').send_keys('dianzi1312') 97         self.driver.find_element_by_id('cn.com.open.mooc:id/login').click() 98  99     def login_by_uiautomator(self):100         self.driver.find_element_by_android_uiautomator('new UiSelector().text("13055211990")').clear()101         self.driver.find_element_by_android_uiautomator('new UiSelector().text("手机号/邮箱")').sendkeys('13055211990')102         self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/password_edit")').sendkeys('dianzi1312')103 104     def login_by_xpath(self):105         # 在所有层级查找text包含忘记的元素106         # self.drvier.find_element_by_xpath('//*[cotains(@text,"忘记")]').click()107         # 在class为..中查找text为忘记的元素108         # self.drvier.find_element_by_xpath('//android.widget.TextView[@text="忘记"]').click()109         # /../preceding-sibiling::寻找上级节点110         self.driver.find_element_by_xpath('//android.widget.TextView@resource-id="cn.com.open.mooc:id/login_lable"]/../preceding-sibiling::*[@index="1]')111 112     def get_webview(self):113         time.sleep(20)114         webviews = self.driver.contexts115         print(webviews)116         for view in webviews:117             if 'WEBVIEW_cn.com.open.mooc' in view:118                 print("1")119                 self.driver.switch_to.context(view)120                 print("2")121                 break122         self.driver.find_element_by_link_text('JAVA').click()123 124     def get_tost(self):125         time.sleep(2)126         tost_locator = ("xpath", "//*[contains(@text,'请输入密码')]")127         result = WebDriverWait(self.driver, 10, 0.1).until(EC.presence_of_element_located(tost_locator))128         print(result)129 130 131 if __name__ == '__main__':132     run = Base()133     time.sleep(5)134     # run.login_by_uiautomator()135     run.go_to_login()136     time.sleep(2)137     run.login_by_id()138     run.get_tost()

 

转载于:https://www.cnblogs.com/dhs94/p/10519781.html

你可能感兴趣的文章
运动跟踪建模
查看>>
导入数据到excel
查看>>
淘宝服务端高并发分布式架构的十四次演进之路
查看>>
js时间戳格式化成日期格式
查看>>
3D Object Classification With Point Convolution —— 点云卷积网络
查看>>
【Windows程序设计】Unicode简介
查看>>
斜率优化
查看>>
ES6 new syntax of Default Function Parameters
查看>>
用linux装逼->我的vim配置(不定时更新)
查看>>
vue 设置 input 为不可以编辑
查看>>
PHP二维数组排序函数
查看>>
[ An Ac a Day ^_^ ] CodeForces 468A 24 Game 构造
查看>>
【转】VC6.0附带小工具软件一览
查看>>
tp3.2路由表达式支持的正则定义必须以“/”开头
查看>>
基于CkEditor实现.net在线开发之路(1)
查看>>
Android架构分析之Android消息处理机制(一)
查看>>
函数指针与指针函数
查看>>
非刚性人脸跟踪 —— 实用工具
查看>>
MS-SQL分页not in 方法改进之使用row_number
查看>>
sql2008 查询某个表被那些存储过程用到
查看>>