1. 程式人生 > >Python+Appium尋找藍牙/wifi匹配

Python+Appium尋找藍牙/wifi匹配

pcl form height 滑動 2個 .info 127.0.0.1 機制 sea

前言:

此篇是介紹怎麽去尋找藍牙,進行匹配。主要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()

Python+Appium尋找藍牙/wifi匹配