本文以开源 bilibili 客户端为例,编写了三个测试用例供参考
开源 bilibili 客户端链接:https://github.com/HotBitmapGG/bilibili-android-client
#coding:utf-8
from appium import webdriver
from time import sleep
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['platformVersion'] = '5.0'
capabilities['deviceName'] = 'Honor'
capabilities['appPackage'] = 'tv.danmaku.bili'
capabilities['appActivity'] = 'tv.danmaku.bili.ui.splash.SplashActivity'
dr = webdriver.Remote('http://localhost:4723/wd/hub', capabilities)
sleep(3)
# 以下模拟用户登录()
#dr.find_element_by_xpath("//*[@text='未登录']").click()
dr.find_element_by_xpath("//*[contains(@text,'未登录')]").click()
#dr.find_element_by_id("tv.danmaku.bili:id/user_nick_text").click() # 如果使用@text,点击不到按键,所以此处使用id
dr.find_element_by_id("user_nick_text").click() # 如果使用@text,点击不到按键,所以此处使用id
dr.find_element_by_xpath("//*[contains(@text,'你的手机号')]").send_keys("1111111")
dr.tap([(580, 826),])
dr.tap([(72,1225),(391,1263)])
#dr.find_element_by_xpath("//*[contains(@text,'登录')]").click() # 该语句无法点击“登录”按钮
dr.tap([(778,972),]) #通过坐标点击“登录”
注:由于密码框 NAF(non-accessiblity friendly) 属性为 true,无法直接通过 Appium 既有方法定位到,故只能通过确定的坐标定位(坐标可以通过开发者选项中的 “打开指针位置” 得到),然后继续通过坐标点击键盘按键模拟用户输入
#coding:utf-8
import unittest
from appium import webdriver
from time import sleep
class bilibiliTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.0'
desired_caps['deviceName'] = 'Samsung'
desired_caps['appPackage'] = 'tv.danmaku.bili'
desired_caps['appActivity'] = 'tv.danmaku.bili.ui.splash.SplashActivity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
sleep(5)
#tearDown 方法做清理工作
def tearDown(self):
self.driver.close_app()
self.driver.quit()
def test_directory(self): #测试侧滑菜单
self.driver.find_element_by_xpath("//*[@text='未登录']").click()
# driver.find_element_by_id("tv.danmaku.bili:id/design_menu_item_text").click()
els = self.driver.find_elements_by_id("tv.danmaku.bili:id/design_menu_item_text") #侧滑菜单各个子项
for el in els:
el.click()
self.driver.back()
print self.driver.current_activity.__str__();
if(self.driver.current_activity.__str__()!='.MainActivityV2'):
self.driver.back()
self.assertEqual(self.driver.current_activity.__str__(), '.MainActivityV2')
sleep(1)
self.driver.find_element_by_xpath("//*[@text='未登录']").click()
#开始测试类中以test开头的测试用例,方法一
suite = unittest.TestLoader().loadTestsFromTestCase(bilibiliTests)
unittest.TextTestRunner(verbosity=2).run(suite)
#方法二
#if __name__ == '__main__':
# unittest.main()
注:该用例使用 Python unittest 单元测试框架
# coding:utf-8
import os
import unittest
from time import sleep
from appium import webdriver
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
class TestBilibiliInput(unittest.TestCase):
def setUp(self):
capabilities={}
capabilities['platformName'] = 'Android'
capabilities['platformVersion'] = '5.0'
capabilities['deviceName'] = 'SAMSUNG'
capabilities['appPackage'] = 'tv.danmaku.bili'
capabilities['appActivity'] = 'tv.danmaku.bili.ui.splash.SplashActivity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub',capabilities)
sleep(5)
def tearDown(self):
self.driver.quit()
def test_bilibili_search(self):
"""搜索测试"""
self.driver.find_element_by_id('tv.danmaku.bili:id/searchable_search').click()
sleep(1)
search_bar = self.driver.find_element_by_id('tv.danmaku.bili:id/search_bar')
search_bar.click()
sleep(1)
search_bar.send_keys("23")
#self.driver.hide_keyboard()
self.driver.find_element_by_id('tv.danmaku.bili:id/search').click()
sleep(1)
self.driver.swipe(600, 1700, 600, 700)
sleep(1)
if __name__ == 'TestUnitTest': # 使用 "__main__"需要命令行运行该文件,否则下面代码不会执行
suite = unittest.TestLoader().loadTestsFromTestCase(TestBilibiliInput)
unittest.TextTestRunner(verbosity=2).run(suite)
注:该用例使用 Python unittest 单元测试框架