【appium】appium中的元素定位和基本操作

  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.contexts
115         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                 break
122         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