import unittest
import time
from appium import webdriver
from public import config
import os
#类继承unittest.TestCase 类,从TestCase类继承是告诉unittest模块的方式,这是一个测试案例。
class ContactsAndroidTests(unittest.TestCase):
#setUp 用于设置初始化的部分,在测试用例执行前,这个方法中的函数将先被调用。
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.3'
desired_caps['deviceName'] = '127.0.0.1:5554'
# 版本小于4.2.2(api17)都需要指定selendroid
# desired_caps['automationName'] = 'selendroid'
# 安装APP
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))
desired_caps['app'] = PATH('..\\apk\\test.apk')
#包名
desired_caps['appPackage'] = 'com.android.calculator2'
#activity
desired_caps['appActivity'] = '.Calculator'
#支持中文输入法
desired_caps['unicodeKeyboard'] = True
#运行结果后还原输入法
desired_caps['resetKeyboard'] = True
#跳过重签名
desired_caps['noSign'] = True
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
#启动activity
# self.driver.start_activity('com.android.calculator2','.Calculator')
#earDown 方法在每个测试方法执行后调用,这个地方做所有清理工作,如退出
def tearDown(self):
now = time.strftime('%H_%M_%S',time.localtime(time.time()))
self.driver.save_screenshot(""+config.screenshotpath+"test_demo"+now+".png")
self.driver.close_app()
self.driver.quit()
#放置的就是我们的测试脚本了,这部分我们并不陌生;因为我们执行的脚本就在这里。
def test_add_contacts(self):
self.driver.find_element_by_id("com.android.calculator2:id/digit7").click()
self.driver.find_element_by_id("com.android.calculator2:id/mul").click()
self.driver.find_element_by_id("com.android.calculator2:id/digit9").click()
self.driver.find_element_by_id("com.android.calculator2:id/equal").click()
text = self.driver.find_element_by_class_name("android.widget.EditText").text
self.assertEqual(text,'63','结果有误!')
#unitest.main()函数用来测试 类中以test开头的测试用例
if __name__ == '__main__':
pass