Python Appium 元素定位方法簡單介紹
阿新 • • 發佈:2018-01-31
用例 ios mage andro ont self 測試 pytho col 註意:SDK自帶的uiautomatorviewer.bat無法獲取xpath,需要替換一個jar包方可獲取xpath。此jar包放到了QQ群:193056556中,有需要的可以進群自取。
Python Appium 元素定位
常用的八種定位方法(與selenium通用)
# id定位 driver.find_element_by_id() # name定位 driver.find_element_by_name() # xpath定位 driver.find_element_by_xpath() # class_name定位 driver.find_element_by_class_name() # css 定位 driver.find_element_by_css_selector() # link定位 driver.find_element_by_link_text()# tag定位 driver.find_element_by_tag_name() # partial_link定位 driver.find_element_by_partial_link_text()
# 移動端 新增方法 # accessibility_id 定位 driver.find_element_by_accessibility_id() # Android driver.find_element_by_android_uiautomator() # iosdriver.find_element_by_ios_uiautomation()
id定位
# resrouce-id屬性是id: driver.find_element_by_id(‘username‘)
name定位
# text屬性是name driver.find_element_by_name(u‘請輸入用戶名‘)
Xpath定位
如下圖,使用 uiautomatorviewer.bat 獲取頁面元素
註意:SDK自帶的uiautomatorviewer.bat無法獲取xpath,需要替換一個jar包方可獲取xpath。此jar包放到了QQ群:193056556中,有需要的可以進群自取。
self.driver.find_element_by_xpath("//android.widget.TextView[@text=‘ ‘]").click()
class_name定位
self.driver.find_element_by_class_name(‘android.view.View‘).click()
(註:一般一個頁面上的class屬性不唯一,元素不唯一的話定位會報錯了)
accessibility_id 定位
content-desc即是對應的 accessibility_id
driver.find_element_by_accessibility_id(u"我的").click()
android_uiautomator 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear()
以下是代碼實例
#測試異常登錄-用戶不存在 def test_aNoUserLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登錄/註冊").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer1‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘111111‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登錄").click() time.sleep(18) get_alertMessage = driver.find_element_by_id(‘android:id/message‘).text time.sleep(2) if get_alertMessage == u‘用戶不存在‘: print (u"用戶不存在,用例執行成功!") else: print (u"用例執行失敗!") def test_bNoPassLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登錄/註冊").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘1111112‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登錄").click() time.sleep(18) get_alertMessage = driver.find_element_by_id(‘android:id/message‘).text time.sleep(2) print (get_alertMessage[0:5]) # if get_alertMessage == u‘用戶不存在‘: # print u"用戶不存在,用例執行成功!" # else: # print u"用例執行失敗!" # # 測試正常登錄 def test_cLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登錄/註冊").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘111111‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登錄").click() time.sleep(2) # 判斷是否登錄成功 print (driver.find_element_by_accessibility_id(u"西藏醫藥銷售公司(測試買家)").get_attribute(‘name‘)) nickName = driver.find_element_by_accessibility_id(u"西藏醫藥銷售公司(測試買家)").get_attribute(‘name‘) temp = nickName[0:2] print (temp) if nickName == u‘西藏醫藥銷售公司(測試買家)‘: print (‘登錄成功!‘) else: print (‘登錄失敗!‘) def tearDown(self): self.driver.close_app() self.driver.quit()
Python Appium 元素定位方法簡單介紹