完整用例
# coding=utf-8
import os
import unittest, sys, time, re, datetime
import HTMLTestRunner
from appium import webdriver
from time import sleep
import sys
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction
reload(sys)
sys.setdefaultencoding('utf-8')
cwd = os.getcwd()
phone_student = 'erwa@qq.com'
class SimpleAndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.0.1'
desired_caps['deviceName'] = '192.168.56.101'
desired_caps['app'] = os.path.abspath(cwd + '/YCMath_Android_V2.7.0_guanghetv.apk')
desired_caps['appPackage'] = 'com.yangcong345.android.phone'
desired_caps['appActivity'] = 'com.yangcong345.android.phone.presentation.activity.SplashActivity'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(60)
def tearDown(self):
# end the session
self.driver.quit()
def input_name_pwd(self, name, pwd):
self.driver.find_element_by_id('com.yangcong345.android.phone:id/etUserName').send_keys(name)
self.driver.find_element_by_id('com.yangcong345.android.phone:id/etPwd').send_keys(pwd)
def login(self):
self.driver.find_element_by_id('com.yangcong345.android.phone:id/btn_login').click()
self.input_name_pwd(phone_student, '123456')
self.driver.find_element_by_id('com.yangcong345.android.phone:id/btnLogin').click()
sleep(3)
def open_chapter(self):
self.driver.find_element_by_id('com.yangcong345.android.phone:id/mask_bottom').click()
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/tabs')
self.assertIsNotNone(el)
els = el.find_elements_by_class_name('android.widget.TextView')
self.driver.scroll(els[len(els) - 1], els[0])
sleep(1)
el = self.driver.find_element_by_name(u'不等式与不等式组')
self.assertIsNotNone(el)
el.click()
sleep(3)
def open_theme(self):
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/exp_lv')
self.assertIsNotNone(el)
el = self.driver.find_element_by_name(u'不等式的基本概念')
self.assertIsNotNone(el)
el.click()
sleep(3)
def open_topic(self, name):
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/recycler_view')
self.assertIsNotNone(el)
el = self.driver.find_element_by_name(name)
self.assertIsNotNone(el)
el.click()
sleep(3)
def close_small_video(self):
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/btn_close')
self.assertIsNotNone(el)
el.click()
def seek_to_end(self):
self.close_small_video()
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/mediacontroller_progress')
self.assertIsNotNone(el)
end = el.size.get('width')
y = el.location.get('y')
action = TouchAction(self.driver)
action.tap(None, end + 95, y).perform()
sleep(3)
self.close_small_video()
def click_rest(self):
el = self.driver.find_element_by_name(u'先休息一下')
self.assertIsNotNone(el)
el.click()
def module_has_video(self):
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/recycler_view')
self.assertIsNotNone(el)
el = el.find_element_by_name(u'视频讲解')
self.assertIsNotNone(el)
def open_module_video(self):
el = self.driver.find_element_by_id('com.yangcong345.android.phone:id/recycler_view')
self.assertIsNotNone(el)
el = el.find_element_by_name(u'视频讲解')
self.assertIsNotNone(el)
el.click()
def is_at_theme(self):
el = self.driver.find_element_by_name(u'完成度')
self.assertIsNotNone(el)
def test_3_1_3(self):
self.login()
self.open_chapter()
self.open_theme()
self.open_topic(u'不等式引入')
self.module_has_video()
self.open_module_video()
self.seek_to_end()
self.click_rest()
self.is_at_theme()
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(
unittest.defaultTestLoader.loadTestsFromTestCase(SimpleAndroidTests)
)
timestr = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
filename = os.path.abspath(cwd + '/result') + "/result_" + timestr + ".html"
print (filename)
fp = open(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='测试结果',
description='测试报告'
)
runner.run(suite)
fp.close()  # 测试报告关闭