加载中…
个人资料
宁愿模糊的正确也不要精确的错误
宁愿模糊的正确也不要
精确的错误
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,317,785
  • 关注人气:35
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

使用appium+python2编写手机游戏功能自动化

(2017-05-19 22:52:38)
标签:

365

it

在一台benchmark测试手机上编写完测试用例,在其他测试手机上执行测试时只需要换算分辨率相对值就可以正常运行。
配置文件源码:
   #coding:utf-8
#手机和app初始化的配置信息
#key:value
apps={
   'no-reset':True,
   'platformName':'Android',
   'platformVersion':'5.0.1',
   'deviceName':'LGD858HK764aa4d',
   'appPackage':'df.er.ga2',
   'appActivity':'com.unity3d.player.UnityPlayerNativeActivity',
   'base_url':'http://localhost:4723/wd/hub'
}
#desired_caps['platformVersion'] ='4.4.2'
#desired_caps['deviceName'] = 'bc6bd36b
##三星note3
#LG g3手机
#针对不同的测试手机需要修改adb device id值
#desired_caps['app'] = PATH(r'E:\testappium\appiumgame3dgameTest7.apk')
自动化脚本:
# -*- coding: utf-8 -*-
from __future__ import division
import os
import unittest,time,re,datetime
from HTMLTestRunner import HTMLTestRunner
from appium import webdriver
from time import sleep
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import config
#所有配置信息写到config.py里面
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)


class Gametest(unittest.TestCase):
        
    #针对sungxing手机
    @classmethod
    def setUpClass(self):
        print 'setUpClass'
        '''
        desired_caps = {}
        desired_caps['no-reset']=True
        desired_caps['platformName'] = 'Android'
        #desired_caps['platformVersion'] = '4.4.2'
        #desired_caps['deviceName'] = 'bc6bd36b' 
        #三星note3
        desired_caps['platformVersion'] = '5.0.1'
        desired_caps['deviceName'] = 'LGD858HK764aa4d' 
        #LG g3手机
        #针对不同的测试手机需要修改adb device id值
        #desired_caps['app'] = PATH(r'E:\testappium\appiumgame3dgameTest7.apk')
        desired_caps['appPackage'] = 'df.er.ga2'
        desired_caps['appActivity'] = 'com.unity3d.player.UnityPlayerNativeActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)  
        '''
        
        disired_caps={
            'no-reset':config.apps['no-reset'],
            'platformName':config.apps['platformName'],
            'platformVersion':config.apps['platformVersion'],
            'deviceName':config.apps['deviceName'],
            'appPackage':config.apps['appPackage'],
            'appActivity':config.apps['appActivity']
            }
        self.driver=webdriver.Remote(config.apps['base_url'],disired_caps)
                  

    @classmethod
    def tearDownClass(self):
        self.driver.close_app()
        self.driver.quit()
        print 'tearDownClass'

    def setUp(self):
        '''setup'''
        print "setup"
        ###############
        #编写测试用例的手机(lg g3)分辨率,adb shell dumpsys window displays
        testcase_phone_width=2392
        #编写测试用例的手机分辨率宽度,就是app的宽度
        testcase_phone_height=1440
        #编写测试用例的手机分辨率高度,就是app的宽度
        ####################################
        #执行测试用例的手机分辨率:很多台测试手机,分辨率会不一样
        execute_case_phone_width=self.driver.get_window_size()['width']
        #执行测试用例的手机分辨率,app的宽度
        execute_case_phone_height=self.driver.get_window_size()['height']
        #执行测试用例的手机分辨率,app的高度
        print '执行测试用例的手机app分辨率-宽度',execute_case_phone_width
        print '\n'
        print '执行测试用例的手机app分辨率-高度',execute_case_phone_height 
        self.base_x=execute_case_phone_width/testcase_phone_width
        #测试手机分辨率宽度和编写测试用例手机的分辨率的宽度的相对值
        self.base_y=execute_case_phone_height/testcase_phone_height
        #测试手机分辨率高度和编写测试用例手机的分辨率的高度的相对值       
        print 'X轴坐标相对系数:',self.base_x
        print '\n'
        print 'Y轴坐标相对系数:',self.base_y   
                        
    def tearDown(self):
        '''teardown'''
        print 'teardown'
   
    tap_x=0.0
    tap_y=0.0
    def tap_coordination(self,tap_x,tap_y):
        self.x=self.tap_x*self.base_x
        self.y=self.tap_y*self.base_y
        self.driver.swipe(self.x,self.y,self.x,self.y,duration=5)
     #此函数封装不同手机分辨率下点击坐标时调用swipe方法        
    def test_gotomain(self):
        '''进入主界面 testcase1'''
        #point=self.driver.get_window_size()
        #self.driver.swipe(point[x]*0.8, point[y]*0.8, point[x]*0.1, point[y]*0.8, duration=6)
        '''
        x0=1197*self.base_x
        y0=727*self.base_y
        sleep(3)
        self.driver.swipe(x0, y0, x0, y0)
        sleep(15)
        '''
        self.tap_coordination(1197, 727)
        
        sleep(6)
    def test_clickplus(self):
        '''点击"-"按钮,数值应该变大 testcase2'''
        x1=158*self.base_x
        y1=723*self.base_y
        for i in range(10):
            self.driver.swipe(x1, y1, x1, y1)
            sleep(2)
          
            
    def test_clickadd(self):
        '''点击“+”按钮滑动后再退出 testcase3'''
        #点击“+”按钮
        x2=153*self.base_x
        y2=1193*self.base_y
        self.driver.swipe(x2,y2,x2,y2)
        sleep(2)
        x3=1737*self.base_x
        y3=1286*self.base_y
        self.driver.swipe(x3,y3,x3,y3)
        sleep(3)
        #取消"add"
        x4=153*self.base_x
        y4=1193*self.base_y
        self.driver.swipe(x4,y4,x4,y4)
        sleep(3)
        #开始在屏幕上移动
        self.driver.tap([(100,200),(500,400),(1400,700)],2000)
        sleep(3)
        #按手机上的返回键
        self.driver.press_keycode('4')
        sleep(3)        
        #点击退出按钮
        self.driver.swipe(1113,780,1113,780)
        print '点击退出按钮'
       
    def test_load(self):
        '''返回后再点击“载入按钮 testcase4'''
    
        sleep(3)
        self.driver.swipe(1214,975,1214,975)
        sleep(3)
        self.driver.press_keycode('4')
        sleep(3)        
        #点击退出按钮        
     
if __name__ == '__main__':
    timestr = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
    filename = "E:\\testappium\\appiumgame\\result_" + timestr + ".html"
    print (filename)
    fp = open(filename, 'wb')
    runner = HTMLTestRunner(
                    stream=fp,
                    title='testreport',
                    description='testresult'
                    )     
    suitecmd = unittest.TestLoader().loadTestsFromTestCase(Gametest)
 
    unittest.TextTestRunner(verbosity=4).run(suitecmd)
    #测试用例名称打印到cmd,在cmd下显示乱码,需要注意cmd编码格式    
    fp.close()
认真学习,必有收获,版权归属本人。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有