您的位置:首页 > 移动开发

MAC 搭建自动化测试环境(基于Appium+python+ios/android+unnitest+HTMLTestRunner)(一)

2017-08-03 13:44 2651 查看
之前一直都是用windows搭建系统进行的测试,现在用的MAC比较多,在搭建的同时顺手记录下

之前已经安装了SDKManager及Python27,以下内容基于已安装上述软件

一:安装brew

1.安装brew


/usr/bin/ruby -e"$(curl -fsSLhttps://raw.githubusercontent.com/Homebrew/install/master/install)"
 


2.查看brew安装是否成功



3.安装软件git实验是否brew安装成功,git也可以管理脚本

二:安装node (appium的支持)

1.安装appium,第一次安装不成功,实验用翻墙安装
npm --registryhttp://registry.cnpmjs.org install -g appium



 
2.appium安装成功

 


 
3.运行appium-doctor



查了下需要安装wd
4.安装wdnpm --registry http://registry.npm.taobao.org install -g wd
5.由于appium-doctor 总报错,就查了下,appium 1.5以后需要单独安装,于是
npm install appium-doctor –g 安装,安装后重新打开命令行,运行appium-doctor ,
提示



三.配置Appium环境变量
从上图可以看到看到5个报错,有四个是跟android_home相关的,先解决它
1.打开配置文件 >touch .bash_profile
                          >open -e .bash_profile
2.编辑配置文件
输入配置的环境变量
ANDROID_HOME=~/Library/Android/sdk
export ANDROID_HOME
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools,保存并退出
3.立即生效,命令行“>source /etc/bashrc”
4.安装Carthage,这是一个ios的依赖管理工具,轻便,简介,低耦合,不用修改xcode配置
重新打开命令行窗口,运行appium-doctor
ok
四.配置HTMLTestRunner
1.下载HTMLTestRunner http://tungwaiyip.info/software/HTMLTestRunner.html 2.在命令行运行python



我的是将文件放到红线的路径下,import HTMLTestRunner,没有报错,表示配置完成

五.脚本试运行
1.部分脚本
#_*_coding=utf-8_*_
import os
from time import sleep
import HTMLTestRunner
import unittest
from appium import webdriver
class Login(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'PBV5T16928003673'
desired_caps['appPackage'] = '自己的包名'
desired_caps['appActivity'] = 'activity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)

self.driver.implicitly_wait(2)
# 跳过展示页
try:
ima1=self.driver.find_element_by_class_name("android.widget.ImageView")
if ima1:
self.driver.swipe(1000,1500,200,1500,1000)
self.driver.implicitly_wait(5)

ima2=self.driver.find_element_by_class_name("android.widget.ImageView")
if ima2:
self.driver.swipe(1000,1500,200,1500,1000)
self.driver.implicitly_wait(5)

ima3=self.driver.find_element_by_class_name("android.widget.ImageView").click()

except Exception,e :
print e

# 设置权限
self.driver.implicitly_wait(10)
self.driver.find_element_by_id("com.xihe.moblie.credit:id/goto_settings").click()
self.driver.implicitly_wait(20)
self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
self.driver.implicitly_wait(20)
self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
self.driver.implicitly_wait(20)
self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
self.driver.implicitly_wait(20)

def test_login(self):

try:
el = self.driver.find_element_by_id("com.xihe.moblie.credit:id/ll_homeme")

if el:
el.click()
except Exception,e :
print 'an element could not find'
print e
sleep(5)
self.driver.find_element_by_id("com.xihe.moblie.credit:id/et_account").send_keys('自己的手机号')
self.driver.implicitly_wait(10)
self.driver.find_element_by_id("com.xihe.moblie.credit:id/et_password").send_keys('密码')
self.driver.implicitly_wait(10)
self.driver.find_element_by_id("com.xihe.moblie.credit:id/btn_send_data").click()
a= self.driver.find_element_by_id("com.xihe.moblie.credit:id/tv_loan_history")
b=self.driver.find_element_by_id("com.xihe.moblie.credit:id/tv_instructions")
self.assertEquals(a.text,u'借款记录')
self.assertIn(u'验证是否是自己的名字',b.text)
sleep(10)
def test_logout(self):
try:
self.test_login()
self.driver.find_element_by_id("com.xihe.moblie.credit:id/ll_homeme").click()
self.driver.implicitly_wait(5)
self.driver.find_element_by_id("com.xihe.moblie.credit:id/ll_seting").click()
self.driver.implicitly_wait(5)
self.driver.find_element_by_id("com.xihe.moblie.credit:id/btn_out_user").click()
self.driver.implicitly_wait(5)
e=self.driver.find_element_by_id("com.xihe.moblie.credit:id/tv_application_amount").text
self.assertEqual('500',e)
except Exception,e:
print e
def tearDown(self):
self.driver.quit()

if __name__ =='__main__':
# suite = unittest.TestLoader().loadTestsFromTestCase(Login)
# unittest.TextTestRunner(verbosity=2).run(suite)
# # suite=unittest.makeSuite(DemoPage)
#构造测试集
def suite():
suite = unittest.TestSuite()
suite.addTest(Login("test_login"))
suite.addTest(Login("test_logout"))
return suite

#定义报告存放路径,支持相对路径
filename = "result.html"
fp = file(filename,'wb')

#定义测试报告
runner = HTMLTestRunner.HTMLTestRunner(
stream = fp,
title = u'薪动钱包测试报告',
description = u' 用例执行情况:'
)

#运行测试用例
runner.run(suite())
fp.close()
2.运行脚本提示no module named appium

解决方案
在命令行运行

cd /usr/local/bin
pipinstall Appium-Python-Clien
3.生成的报告截图



我将在下一篇中介绍如何后续的功能:
1.通过svn获取安装包并安装在手机(安卓)
2.测试报告生成并自动发送至指定邮箱
3.如何用appium同时运行安卓和iOS两个平台的应用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息