前言:
此篇是介紹怎么去尋找藍牙,進行匹配。主要2個問題點:
1.在不同環境下,搜索到的藍牙數量有變
2.在不同環境下,搜索到的藍牙排序會變
簡單思路:
將搜索出來的藍牙名字添加到一個list去,然后在去匹配list里是否有你要匹配的藍牙,找到了就點擊,沒找到,又進行下一次尋找,知道找到為止
簡單代碼:
#coding:utf-8 from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait import time bluetoothName="iPhone" desired_caps = {'platformName': 'Android','deviceName': '9a762346','platformVersion': '6.0.1','noReset': True,'appPackage': 'com.android.settings','appActivity': '.Settings'} driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) size = driver.get_window_size() print ('屏幕的分辨率: %s'% size) print ('啟動成功') WebDriverWait(driver,30,1).until(lambda x:x.find_element_by_xpath('//*[@text="藍牙"]')).click() print('正在搜索藍牙設備,請等待30s') #因為不清楚藍牙停止搜索的機制是什么,這里就讓強制等待30s time.sleep(10) print('已經搜索10s,還剩20s') time.sleep(10) print('已經搜索20s,還剩10s') time.sleep(10) print('已經搜索30s,還剩0s') print('搜索完畢') a=driver.find_elements_by_id('android:id/title') b=[] # 創建一個空的list,用于后面存放打印的文本 for j in range(1,11): #控制滑動次數for i in range(10): #這個10是a的數量。當然也可以直接 len(a) b.append(a[i].text)x1=size['width'] * 0.5y1=size['height'] * 0.75y2=size['height'] * 0.25driver.swipe(x1, y1, x1, y2, 120) #這個 1 20 滑動時間建議不要太多,很容滑過去time.sleep(2) # 這個sleep必須要有,沒有的話就會導致滑太快if "iPhone" in b:WebDriverWait(driver,60,1).until(lambda x:x.find_element_by_xpath('//*[@text="iPhone"]')).click()print('第'+str(j)+'次滑動設備找到藍牙')break #找到了就跳出循環else:print('第'+str(j)+'次滑動設備藍牙未找到,2s后進行下一次尋找')try:WebDriverWait(driver,20,1).until(lambda x:x.find_element_by_xpath('//*[@text="配對"]')).click()print('點擊 配對完成') except:print('配對按鈕沒找到(20s),設備藍牙未找到')
?同理? WiFi也可以用同樣的方法去尋找
#coding:utf-8 import unittest from common.base import BaseApp from appium import webdriver from common.logger import Log from selenium.webdriver.support.ui import WebDriverWait import timedesired_caps = {'platformName': 'Android','deviceName': '9a762346','platformVersion': '6.0.1','noReset': True,'unicodeKeyboard': True,'resetKeyboard': True,'appPackage': 'com.android.settings','appActivity': '.Settings'}u'''測試wifi連接''' class Test(unittest.TestCase):@classmethoddef setUpClass(cls):cls.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)cls.base=BaseApp(cls.driver)cls.log=Log()def setUp(self):passdef testName(self):self.log.info('設備啟動成功')self.size = self.driver.get_window_size()WebDriverWait(self.driver,30,1).until(lambda x:x.find_element_by_xpath('//*[@text="WLAN"]')).click()WebDriverWait(self.driver,30,1).until(lambda x:x.find_element_by_xpath('//*[@text="刷新"]')).click()self.log.info('點擊刷新成功')time.sleep(15)self.log.info('15s搜索完畢')a=self.driver.find_elements_by_id('android:id/title')b=[] for j in range(1,31): for i in range(len(a)):try:b.append(a[i].text)except:self.log.info('添加文本時發生錯誤') if "iPhone羅" in b:WebDriverWait(self.driver,60,1).until(lambda x:x.find_element_by_xpath('//*[@text="iPhone羅"]')).click()self.log.info('第'+str(j)+'次滑動設備找到wifi')self.log.info(b)break else:x1=self.size['width'] * 0.5y1=self.size['height'] * 0.75y2=self.size['height'] * 0.50self.driver.swipe(x1, y1, x1, y2, 200) self.log.info(list(set(b)))time.sleep(2) self.log.info('第'+str(j)+'次滑動設備wifi未找到,2s后進行下一次尋找')try:WebDriverWait(self.driver,10,1).until(lambda x:x.find_element_by_id('com.android.settings:id/password')).send_keys('11111111111111') #輸入密碼self.log.info('輸入密碼完成')time.sleep(2)WebDriverWait(self.driver,10,1).until(lambda x:x.find_element_by_xpath('//*[@text="連接"]')).click()self.log.info('點擊連接成功')except:self.log.info('連接按鈕沒找到(10s),WiFi未找到')def tearDown(self):pass@classmethoddef tearDownClass(cls):cls.driver.quit()if __name__ == "__main__":#import sys;sys.argv = ['', 'Test.testName']unittest.main()
?