最近在研究安卓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 unittest
from appium import webdriver
class 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)
原文地址:https://www.cnblogs.com/liushengchieh/p/9084827.html