前言:

此篇是介绍怎么去寻找蓝牙,进行匹配。主要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.5
    y1=size['height'] * 0.75
    y2=size['height'] * 0.25
    driver.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 time


desired_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):
    
    @classmethod
    def 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):
        pass


    def 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.5
                y1=self.size['height'] * 0.75
                y2=self.size['height'] * 0.50
                self.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
    
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()