//封装类util
#-*- coding:utf-8 -*-
# 导入appium
from appium import webdriver
# 导入time包
import time
# 导入枚举
from enum import Enum
# 倒包
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 声明类
class Apputil(object):
# 类被实例化的时候init
def __init__(self):
# pep8 规范
# 将配置参数封装进来
self.qiezi = {}
# 打开客户端的方法
def app_start(self):
# 设置属性 设置手机类型
self.qiezi['platformName'] = "Android"
# 设置自动化测试工具
self.qiezi['automationName'] = "Appium"
# 设置手机唯一识别码
self.qiezi['deviceName'] = "127.0.0.1:62001"
# 设置本地app加载的路径
self.qiezi['app'] = "D:/google/appdemo/com.qiezzi.eggplant.apk"
# 是不是需要重写安装,
self.qiezi['noReset'] = False
# 设置启动的包名
self.qiezi['appPackage'] = "com.qiezzi.eggplant"
# 设置需要运行的手机的版本号
self.qiezi['platformVersion'] = "4.4"
# 设置启动页面的activity
self.qiezi['appActivity'] = "com.qiezzi.eggplant.base.WelcomeActivity"
# 设置等待启动的页面的包名
self.qiezi['appWaitPackage'] = "com.qiezzi.eggplant"
# 设置等待启动的页面的activity
self.qiezi['appWaitActivity'] = "com.qiezzi.eggplant.base.WelcomeActivity"
# 将手机启动起来
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",self.qiezi)
#设置休眠五秒
self.TimeSleep(ENUMS.FIVE_TIME)
pass
# 关闭客户端的方法
def app_quit(self):
self.driver.quit()
pass
# 关闭当前窗口
def app_close(self):
self.driver.close()
pass
# 使用休眠方法 强制休眠
def TimeSleep(self,number):
time.sleep(number)
pass
# 隐式休眠
def TimeImplay(self,number):
self.driver.implicitly_wait(number)
# 查找控件
def FindID(self,ID):
# 通过id
ids = (By.ID,ID)
WebDriverWait(self.driver,ENUMS.TWENTY_TIME,ENUMS.ONE_HALF).until(EC.presence_of_element_located(ids))
return self.driver.find_element_by_id(ID)
# 设置点击事件
def ClickID(self,ID):
self.FindID(ID).click()
# 输入内容
def SendkeysID(self,ID,message):
self.FindID(ID).send_keys(message)
# 通过xpath查找
def FindXpath(self,xpath):
xpaths = (By.XPATH,xpath)
WebDriverWait(self.driver,ENUMS.TWENTY_TIME,ENUMS.ONE_HALF).until(EC.presence_of_element_located(xpaths))
return self.driver.find_element_by_xpath(xpath)
# xpath点击事件
def ClickXpath(self,xpath):
self.FindXpath(xpath).click()
# 设置休眠
self.TimeSleep(ENUMS.TWO_TIME)
# 输入内容
def SendkeysXpath(self,xpath,message):
self.FindXpath(xpath).send_keys(message)
# 根据内容查找
def FindLink(self,link):
links = (By.LINK_TEXT,link)
WebDriverWait(self.driver,ENUMS.TWENTY_TIME,ENUMS.ONE_HALF).until(EC.presence_of_element_located(links))
return self.driver.find_element_by_link_text(link)
# 设置点击事件
def ClickLink(self,link):
self.FindLink(link).click()
# 设置休眠
self.TimeSleep(ENUMS.TWO_TIME)
# 输入内容
def SendkesLink(self,link,message):
self.FindLink(link).send_keys(message)
# 设置休眠 以防止在跳转的时候代码运行的速度超过页面跳转的速度
self.TimeSleep(ENUMS.TWO_TIME)
# 切换H5
def SwitchH5(self):
# 设置休眠
self.TimeSleep(ENUMS.FIVE_TIME)
self.driver.switch_to.context("")
# 获取宽度和高度
def getWidth(self):
return self.driver.get_window_size()['width']
# 获取高度
def getHeight(self):
return self.driver.get_window_size()['height']
# 滑动的方法
def swipe(self):
# 设置休眠
self.TimeSleep(ENUMS.TWO_TIME)
# 滑动
self.driver.swipe(self.getWidth()-50,self.getHeight()/2,50,self.getHeight()/2,1000)
# 设置休眠
self.TimeSleep(ENUMS.TWO_TIME)
# 获取当前activity的方法
def getCurrentActivity(self):
return self.driver.current_activity
# 断言页面的方法
def AssertAcitity(self,self1,expect):
# 设置休眠时间
self.TimeSleep(ENUMS.TWO_TIME)
# 进行断言
self1.assertEqual(self.getCurrentActivity(),expect)
pass
def AssertCount(self, self1,count, expect):
# 设置休眠时间
self.TimeSleep(ENUMS.TWO_TIME)
# 进行断言
self1.assertEqual(count, expect)
pass
# 从一个滑动到另外一个
def swith_to_element(self, e1, e2):
# 从 e1 滑动到 e2
self.driver.scroll(e1, e2)
pass
# 查询一组控件
def FindIDs(self, ID):
# 通过id
ids = (By.ID, ID)
# 休眠检查元素
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(EC.presence_of_element_located(ids))
# 开始查找
return self.driver.find_elements_by_id(ID)
# 通过下标点击第一个
def ClickIDS(self, ID, index):
self.FindIDs(ID)[index].click()
# 定义枚举类型
class ENUMS(Enum):
# 10 秒
TEN_TIME = 10
# 20秒
TWENTY_TIME = 20
# 0.5秒
ONE_HALF = 0.5
# 5秒
FIVE_TIME = 5
# 两秒
TWO_TIME = 2
//loginc.clogin.logincontron,账号密码登陆
#-*- coding:utf-8 -*-
# 倒包
from util import apputil
# 声明类
class LoginControl(object):
def __init__(self,app):
# self.app = apputil.Apputil()
#
self.app = app
pass
def login_us_pw(self):
# 输入账号和密码
self.app.SendkeysID("com.qiezzi.eggplant:id/edt_frist_login_accout","18301585363")
self.app.SendkeysID("com.qiezzi.eggplant:id/edt_frist_login_password","123456")
# 点击登录
self.app.ClickID("com.qiezzi.eggplant:id/btn_frist_login_immediately")
pass
//meets。meets。meetcontrol,点击当天的坐标
#-*- coding:utf-8 -*-
from util import apputil
class MeetControl(object):
def __init__(self,app):
# 实例化
# self.app = apputil.Apputil()
#
self.app = app
pass
# 点击我的
def meet_click_tody(self):
# 通过内容查找
self.app.ClickLink("23")
# 因为有网络请求时间所有在这里设休眠
self.app.TimeSleep(apputil.ENUMS.FIVE_TIME)
# 查找listview 控件
items = self.app.driver.find_elements_by_id("com.qiezzi.eggplant:id/ll_appointment_item")
return len(items)
pass
//welcomec.control.welcomecontrol,导航页滑动
#-*- coding:utf-8 -*-
from util import apputil
# 声明类
class WelcomeControl(object):
def __init__(self,app):
# 实例化工具类
# self.app = apputil.Apputil()
self.app = app
pass
# 点击新手引导页
def welcome_next(self):
# 使用for循环实现滑动
for index in range(0,4):
if index< 3:
self.app.swipe()
else:
self.app.ClickXpath("//android.widget.Button[@resource-id='com.qiezzi.eggplant:id/btn_feel_right_now']")
pass
# 点击新手引导页
def welcome_next_hide(self):
# 使用for循环实现滑动
for index in range(0, 4):
if index < 3:
self.app.swipe()
else:
# 使用try 方法
try:
# 查找跳过按钮
self.app.FindID("com.qiezzi.eggplant:id/btn_firstinstall_intent")
pass
except:
self.app.ClickXpath(
"//android.widget.Button[@resource-id='com.qiezzi.eggplant:id/btn_feel_right_now']")
pass
# 滑动到第四个页面,在往回滑动验证按钮能不能使用
# 点击跳过按钮
# index 1 2 3 在这三个页面分别点击跳过按钮,
def welcome_jumnp(self,index):
# 使用for循环的目的就是为了帮助我们滑动页面
for i in range(0,index):
# 滑到我指定的页面去点击按钮
if i == index - 1:
#查找控件,点击跳过按钮
self.app.ClickID("com.qiezzi.eggplant:id/btn_firstinstall_intent")
# 跳出for循环的方法
break
pass
if i <3:
self.app.swipe()
pass
//unit.login,登陆页面
#-*- coding:utf-8 -*-
import unittest
# 倒包
from util import apputil
from welcomec.control import welcomecontrol
from loginc.clogin import logincontron
# 定义类继承
class Login(unittest.TestCase):
@classmethod
def setUpClass(self):
# 实例化工具类 和 控制类
self.app = apputil.Apputil()
# 实例化welcome类
self.welcome = welcomecontrol.WelcomeControl(self.app)
# 实例化登陆
self.login = logincontron.LoginControl(self.app)
pass
# 打开浏览器
def setUp(self):
# 打开浏览器
self.app.app_start()
# 点击跳过
self.welcome.welcome_jumnp(1)
pass
# 关闭浏览器
def tearDown(self):
self.app.app_quit()
pass
def test_login_us_pw(self):
self.login.login_us_pw()
# 进行断言
self.app.AssertAcitity(self,'.main.activity.MainActivity')
pass
if __name__ == '__main__':
unittest.main()
//unit.welcom,导航页的滑动
#-*- coding:utf-8 -*-
# 导入单元测试
import unittest
# 倒包
from util import apputil
# 导入控制类
from welcomec.control import welcomecontrol
# 声明类
class Welcome(unittest.TestCase):
# 实例化对象 只执行一次
@classmethod
def setUpClass(self):
# 实例化工具类
self.app = apputil.Apputil()
# 实例化控制类
self.welcome = welcomecontrol.WelcomeControl(self.app)
pass
def setUp(self):
# 打开客户端
self.app.app_start()
pass
def tearDown(self):
# 关闭客户端
self.app.app_quit()
pass
def test_welcome_next(self):
u"""点击第四个页面的立即体验按钮"""
# 进行滑动,同时点击下一步
self.welcome.welcome_next()
# 进行断言
self.app.AssertAcitity(self,".login.activity.LoginActivity")
pass
def test_welcome_jump_first(self):
u"""点击第一个页面的跳过按钮"""
# 点击跳过按钮的封装,点击第一个页面
self.welcome.welcome_jumnp(1)
# 进行断言
self.app.AssertAcitity(self, ".login.activity.LoginActivity")
pass
def test_welcome_jump_second(self):
u"""点击第二个页面的跳过按钮"""
# 点击跳过按钮的封装,点击第一个页面
self.welcome.welcome_jumnp(2)
# 进行断言
self.app.AssertAcitity(self, ".login.activity.LoginActivity")
pass
def test_welcome_jump_thrid(self):
u"""点击第三个页面的跳过按钮"""
# 点击跳过按钮的封装,点击第一个页面
self.welcome.welcome_jumnp(3)
# 进行断言
self.app.AssertAcitity(self, ".login.activity.LoginActivity")
pass
# 在第四个页面,验证跳过按钮有没有隐藏掉,同时立即体验按钮是不是显示出来了
def test_welcome_jump_hide(self):
u"验证在第四个页面,跳过按钮是不是已经隐藏掉,同时立即体验按钮是不是能够点击"
self.welcome.welcome_next_hide()
# 断言
self.app.AssertAcitity(self, ".login.activity.LoginActivity")
pass
# 滑动到第四个页面,在往会滑动一个页面,验证立即体验按钮又没有隐藏掉
# 跳过按钮又没有显示出来
def test_welcome_four_three(self):
# 断言
self.app.AssertAcitity(self, ".login.activity.LoginActivity")
pass
//unit.meet,创建人员
#-*- coding:utf-8 -*-
#导入单元测试
import unittest
#导包
from util import apputil
from welcomec.control import welcomecontrol
from loginc.clogin import logincontron
from meets.meetc import meetcontrol
class Meet(unittest.TestCase):
@classmethod
def setUpClass(self):
#实例化工具类
self.app=apputil.Apputil()
self.welcome=welcomecontrol.WelcomeControl(self.app)
self.login=logincontron.LoginControl(self.app)
self.meet=meetcontrol.MeetControl(self.app)
pass
def setUp(self):
#打开客户端
self.app.app_start()
self.welcome.welcome_jumnp(1)
self.login.login_us_pw()
#关闭
def tearDown(self):
self.app.app_quit()
def test_new_add_patient(self):
u"""新建患者功能"""
self.app.ClickID("com.qiezzi.eggplant:id/tv_current_mouth")
#设置休眠这里需要网络加载
self.app.TimeSleep(apputil.ENUMS.FIVE_TIME)
#点击第一个医生
self.app.ClickIDS("com.qiezzi.eggplant:id/ll_adapter_group",0)
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
#选中复诊
self.app.ClickID("com.qiezzi.eggplant:id/btn_add_new_appoint_sencod")
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
#点击预约事项
self.app.ClickID("com.qiezzi.eggplant:id/ll_activity_add_new_appoint_times")
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
self.app.ClickIDS("com.qiezzi.eggplant:id/rl_adapter_see_doctor",0)
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
#点击保存
self.app.ClickID("com.qiezzi.eggplant:id/tv_main_title_setting")
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
#点击预约日期
self.app.ClickID("com.qiezzi.eggplant:id/ll_activity_add_new_appoint_data")
self.app.ClickID("android:id/button1")
#点击预约时间
self.app.ClickID("com.qiezzi.eggplant:id/ll_activity_add_new_appoint_time")
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
e1=self.app.FindXpath("//android.widget.EditText[@resource-id='android:id/numberpicker_input' and @text='11']")
e2=self.app.FindXpath("//android.widget.Button[@text='12']")
self.app.swith_to_element(e1,e2)
self.app.ClickID("android:id/button1")
#点击保存按钮
self.app.ClickID("com.qiezzi.eggplant:id/tv_main_title_setting")
self.app.TimeSleep(apputil.ENUMS.TWO_TIME)
count=self.meet.meet_click_tody("//android.widget.FrameLayout[@resource-id='com.qiezzi.eggplant:id/fl_mian_frag']/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.view.View[4]/android.widget.TextView[2]")
#点击今天进行断言
self.app.AssertAcitity(self,count/2,2)
pass
//suit执行
#-*- coding:utf-8 -*-
import unittest
# 导入单元测试
from unit import welcome,login,meet
# 导入单元测试
import HTMLTestRunner
# 导入os
import os
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
# 实例化suit
suit = unittest.TestSuite()
suit.addTest(unittest.makeSuite(welcome.Welcome))
suit.addTest(unittest.makeSuite(login.Login))
suit.addTest(unittest.makeSuite(meet.Meet))
# 事例
files = os.getcwd() +"/qiezi.html"
# 指定读写方式
filename = open(files,"wb")
# 运行自动化测试
runner = HTMLTestRunner.HTMLTestRunner(stream=filename,title=u"茄子",description=u"茄子医生")
runner.run(suit)