Python+Andrid+Unittest自动化实现QQ登录
框架简介
import unittest
class RunCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('所有case执行之前的前置')
@classmethod
def tearDownClass(cls):
print('所有case执行之后的后置')
def setUp(self):
print('每条case之前执行')
def tearDown(self):
print('每条case之后执行')
@unittest.skip('') # 无条件跳过此条case
def test_case01(self):
"""第一条case打开网页"""
print('我是case01')
def test_case02(self):
"""第二条case输入账号"""
print('我是case02')
if __name__ == '__main__':
# 构造测试集
suite = unittest.TestSuite()
suite.addTest(RunCase('test_case01'))
suite.addTest(RunCase('test_case02'))
# 运行测试集
runner = unittest.TextTestRunner()
runner.run(suite)
例子
from appium import webdriver
import time,unittest
from Scripts.HTMLTestRunner_cn import HTMLTestRunner
class Testlogin(unittest.TestCase):
def setUp(self):#初始化
self.desired_caps={}
self.desired_caps['platformName'] = r'Android'
self.desired_caps['deviceName']=r"836f304c43583057"
self.desired_caps['preformVersion']=r'7.0'
self.desired_caps['appPackage'] ='com.sec.android.app.launcher'
self.desired_caps['appActivity']='.activities.LauncherActivity'
#self.desired_caps['appPackage'] ='com.tencent.mobileqq'
#self.desired_caps['appActivity']='.activity.SplashActivity'
self.driver=webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)#启动 app
time.sleep(15)
def tearDown(self):#还原测试环境
self.driver.quit()
def test_Login1(self):#测试用例
#self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"" + QQ + "\")")
self.driver.find_element_by_accessibility_id('QQ').click()
#WebDriverWait(self.driver, seconds).until(lambda x: x.find_element_by_accessibility_id('请输入QQ号码或手机或邮箱'))
me=self.driver.find_element_by_accessibility_id('请输入QQ号码或手机或邮箱')
me.clear()
me.send_keys('1471976302')
password=self.driver.find_element_by_id('com.tencent.mobileqq:id/password')
password.clear()
password.send_keys('45667')
self.driver.find_element_by_id('com.tencent.mobileqq:id/login').click()
time.sleep(5)
self.driver.find_element_by_id('com.tencent.mobileqq:id/obm').click()
self.driver.find_element_by_accessibility_id('退出当前帐号').click()
self.driver.find_element_by_id('com.tencent.mobileqq:id/dialogRightBtn').click()
def test_Login2(self):#测试用例
self.driver.find_element_by_accessibility_id('QQ').click()
me=self.driver.find_element_by_accessibility_id('请输入QQ号码或手机或邮箱')
me.clear()
me.send_keys('1471976302')
password=self.driver.find_element_by_id('com.tencent.mobileqq:id/password')
password.clear()
password.send_keys('700')
self.driver.find_element_by_id('com.tencent.mobileqq:id/login').click()
time.sleep(5)
if self.driver.find_element_by_id('com.tencent.mobileqq:id/dialogTitle') is None:
print('login is sucess')
else:
print('login is Flase')
print(self.driver.find_element_by_id('com.tencent.mobileqq:id/dialogText').text)
time.sleep(5)
self.driver.find_element_by_accessibility_id('确定').click()
if __name__ == '__main__':
suiteTest = unittest.TestSuite()
suiteTest.addTest(Testlogin("test_Login1"))
suiteTest.addTest(Testlogin("test_Login2"))
now=time.strftime('%Y-%m%d',time.localtime(time.time()))
report_dir= r'%s.html'%n
owre_open= open(report_dir,'wb')
runner=HTMLTestRunner.HTMLTestRunner(stream=re_open,title='QQ测试',description='测试结果')
runner.run(suiteTest)
如何实现Appium每次运行可不用卸载手机上的Appium Settings 和Unlock
详见此文章
亲测有效