Python&Appium實現滑動引導頁進入APP

來源:互聯網
上載者:User

標籤:代碼執行   座標   ase   enter   ret   runner   ext   ppi   drive   

最近在研究安卓APP的自動化測試。首先遇到的問題是,當一個session建立的時候,最先進入的是歡迎頁面和引導頁,引導頁有三張,最後一張上顯示“enter”按鈕,點擊才能進入主介面。

歡迎頁面加引導頁,這兩個頁面是每次進入APP都無法避免的,如何通過代碼執行跳過它們進入主介面的操作呢?

1、建立session

def test_enterApp(self): desired_caps = {} desired_caps[‘platformName‘] = ‘Android‘ desired_caps[‘platformVersion‘] = ‘4.4.4‘ desired_caps[‘app‘] = ‘/Users/a140/Downloads/app.apk‘ desired_caps[‘deviceName‘] = ‘03083025d0250909‘ self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps) self.driver.implicitly_wait(5)

2、運用代碼類比手指從右向左滑動的操作

#擷取螢幕寬度和高度 def getSize(self): x = self.driver.get_window_size()[‘width‘] y = self.driver.get_window_size()[‘height‘] return (x, y) #向左滑動 def swipeLeft(self): l = self.getSize() x1 = int(l[0] * 0.9) y1 = int(l[1] * 0.5) x2 = int(l[0] * 0.1) self.driver.swipe(x1, y1, x2, y1)

因為手機螢幕的尺寸多樣,所以這裡不設定固定的寬度和高度的值,而是封裝了一個擷取螢幕寬高度的方法,通過它計算螢幕滑動的座標和距離。

3、調用swipeLeft()方法向左滑動,最後通過find_element_by_id()找到“enter”按鈕,然後點擊進入主介面

#向左滑動跳過引導頁        x = 0        while x < 3:            self.swipeLeft()            x += 1        enterApp = self.driver.find_element_by_id("com.app.night:id/enter")        enterApp.click()

完整的代碼如下所示:

import unittestfrom appium import webdriverclass HelloWorld(unittest.TestCase):    def test_enterFilter(self):        desired_caps = {}        desired_caps[‘platformName‘] = ‘Android‘        desired_caps[‘platformVersion‘] = ‘4.4.4‘        desired_caps[‘app‘] = ‘/Users/a140/Downloads/app.apk‘        desired_caps[‘deviceName‘] = ‘03083025d0250909‘        self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)        self.driver.implicitly_wait(5)        #向左滑動跳過引導頁        x = 0        while x < 3:            self.swipeLeft()            x += 1        enterApp = self.driver.find_element_by_id("com.app.night:id/enter")        enterApp.click()    #擷取螢幕寬度和高度    def getSize(self):        x = self.driver.get_window_size()[‘width‘]        y = self.driver.get_window_size()[‘height‘]        return (x, y)    #向左滑動    def swipeLeft(self):        l = self.getSize()        x1 = int(l[0] * 0.9)        y1 = int(l[1] * 0.5)        x2 = int(l[0] * 0.1)        self.driver.swipe(x1, y1, x2, y1)if __name__ == ‘__main__‘:    suite = unittest.TestLoader().loadTestsFromTestCase(HelloWorld)    unittest.TextTestRunner(verbosity=2).run(suite)

Python&Appium實現滑動引導頁進入APP

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.