Implementation of python+appium+yaml mobile terminal automated testing framework

Structure introduction

I shared an Android UI test before, but it didn't realize the separation of data and code, and the maintenance cost was high, so I recently optimized it.
If you don’t want to read the article, you can go directly to Github , welcome to make a brick. The
general structure is as follows:

Structure.png

 

  • testyaml management use case, realize the separation of data and code, one folder per module

  • public Store public files, such as reading configuration files, starting appium services, reading Yaml files, defining log formats, etc.

  • page stores the smallest set of test cases, one folder per module

  • results Store test reports and failed screenshots

     

     

    report.png

  • logs store logs

     

     

    logs.png

     

     

    logdetail.png

  • testcase stores test cases
  • runtest.py runs all test cases

yaml format introduction

First look at the format of the yaml file. I also wrote a little article about yaml grammar learning before. The
testcase part is the focus, among which:

  • element_info: Positioning element information

  • find_type:属性,id、xpath、text、ids

  • operate_type: click, sendkeys, back, swipe_up are “back” and “return”. For the time being, there are four

    The above three are required, operate_type is required!!!!!!

  • send_content: used when send_keys

  • index: used when ids

  • times: return times or upslide times

testinfo:
    - id: cm001
      title: 新增终端门店
      execute: 1
testcase:
    -
      element_info: 客户
      find_type: text
      operate_type: click
    -
      element_info: com.fiberhome.waiqin365.client:id/cm_topbar_tv_right
      find_type: id
      operate_type: click
    -
      element_info: com.fiberhome.waiqin365.client:id/custview_id_singletv_inputtext
      find_type: ids
      operate_type: send_keys
      send_content: auto0205
      index: 0
    -
      element_info:
      find_type:
      operate_type: swipe_up
      times: 1
    -
      element_info: 提交
      find_type: text
      operate_type: click
    -
      element_info:
      find_type:
      operate_type: back
      times: 1

Code part

Public section

I personally think that the core is the public part, which is equivalent to building a house. Once the public part is done, just call it later. Build the house and set up the shelf, then add bricks and tiles to the back.

Read the configuration file readconfig.py to
set the log format logs.py to
obtain the device GetDevices.py,
these general ones will not be introduced

  • Read the yaml file GetYaml.py is
    mainly used to read the yaml file
#coding=utf-8
#author='Shichao-Dong'

import sys
reload(sys)
sys.setdefaultencoding('utf8')
import yaml
import codecs

class getyaml:
    def __init__(self,path):
        self.path = path

    def getYaml(self):
        '''
        读取yaml文件
        :param path: 文件路径
        :return:
        '''
        try:
            f = open(self.path)
            data =yaml.load(f)
            f.close()
            return data
        except Exception:
            print(u"未找到yaml文件")

    def alldata(self):
        data =self.getYaml()
        return data

    def caselen(self):
        data = self.alldata()
        length = len(data['testcase'])
        return length

    def get_elementinfo(self,i):
        data = self.alldata()
        # print data['testcase'][i]['element_info']
        return data['testcase'][i]['element_info']

    def get_findtype(self,i):
        data = self.alldata()
        # print data['testcase'][i]['find_type']
        return data['testcase'][i]['find_type']

    def get_operate_type(self,i):
        data = self.alldata()
        # print data['testcase'][i]['operate_type']
        return data['testcase'][i]['operate_type']

    def get_index(self,i):
        data = self.alldata()
        if self.get_findtype(i)=='ids':
                    return data['testcase'][i]['index']
        else:
            pass

    def get_send_content(self,i):
        data = self.alldata()
        # print data['testcase'][i]['send_content']
        if self.get_operate_type(i) == 'send_keys':
            return data['testcase'][i]['send_content']
        else:
            pass

    def get_backtimes(self,i):
        data = self.alldata()
        if self.get_operate_type(i)=='back' or self.get_operate_type(i)=='swipe_up':
                    return data['testcase'][i]['times']
        else:
            pass

    def get_title(self):
        data = self.alldata()
        # print data['testinfo'][0]['title']
        return  data['testinfo'][0]['title']

  • Start the appium service StartAppiumServer.py
    mainly starts appium and returns the port port, which is required in the driver below
#coding=utf-8
#author='Shichao-Dong'

from logs import log
import random,time
import platform
import os
from GetDevices import devices

log = log()
dev = devices().get_deviceName()

class Sp:
    def __init__(self, device):
        self.device = device

    def __start_driver(self, aport, bpport):
        """
        :return:
        """
        if platform.system() == 'Windows':
            import subprocess
            subprocess.Popen("appium -p %s -bp %s -U %s" %
                             (aport, bpport, self.device), shell=True)

    def start_appium(self):
        """
        启动appium
        p:appium port
        bp:bootstrap port
        :return: 返回appium端口参数
        """
        aport = random.randint(4700, 4900)
        bpport = random.randint(4700, 4900)
        self.__start_driver(aport, bpport)

        log.info(
            'start appium :p %s bp %s device:%s' %
            (aport, bpport, self.device))
        time.sleep(10)
        return aport

    def main(self):
        """
        :return: 启动appium
        """
        return self.start_appium()

    def stop_appium(self):
        '''
        停止appium
        :return:
        '''
        if platform.system() == 'Windows':
            os.popen("taskkill /f /im node.exe")

if __name__ == '__main__':
    s = Sp(dev)
    s.main()
  • Get the driver GetDriver.py
    platformName, deviceName, appPackage, appActivity these uninstall configuration files config.ini file, you can directly read through the readconfig.py file.
    appium_port has StartAppiumServer.py file return
s = Sp(deviceName)
appium_port = s.main()

def mydriver():
    desired_caps = {
                'platformName':platformName,'deviceName':deviceName, 'platformVersion':platformVersion,
                'appPackage':appPackage,'appActivity':appActivity,
                'unicodeKeyboard':True,'resetKeyboard':True,'noReset':True
                }
    try:
        driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub'%appium_port,desired_caps)
        time.sleep(4)
        log.info('获取driver成功')
        return driver
    except WebDriverException:
        print 'No driver'

if __name__ == "__main__":
    mydriver()
  • Repackage commands such as find, BaseOperate.py
    mainly contains some basic operations such as slide up, return, and find
#coding=utf-8
#author='Shichao-Dong'

from selenium.webdriver.support.ui import WebDriverWait
from logs import log
import os
import time

'''
一些基础操作:滑动、截图、点击页面元素等
'''

class BaseOperate:
    def __init__(self,driver):
        self.driver = driver

    def back(self):
        '''
        返回键
        :return:
        '''
        os.popen("adb shell input keyevent 4")

    def get_window_size(self):
        '''
        获取屏幕大小
        :return: windowsize
        '''
        global windowSize
        windowSize = self.driver.get_window_size()
        return windowSize

    def swipe_up(self):
        '''
        向上滑动
        :return:
        '''
        windowsSize = self.get_window_size()
        width = windowsSize.get("width")
        height = windowsSize.get("height")
        self.driver.swipe(width/2, height*3/4, width/2, height/4, 1000)

    def screenshot(self):
        now=time.strftime("%y%m%d-%H-%M-%S")
        PATH = lambda p: os.path.abspath(
            os.path.join(os.path.dirname(__file__), p)
        )
        screenshoot_path = PATH('../results/screenshoot/')
        self.driver.get_screenshot_as_file(screenshoot_path+now+'.png')

    def find_id(self,id):
        '''
        寻找元素
        :return:
        '''
        exsit = self.driver.find_element_by_id(id)
        if exsit :
            return True
        else:
            return False

    def find_name(self,name):
        '''
        判断页面是否存在某个元素
        :param name: text
        :return:
        '''
        findname = "//*[@text='%s']"%(name)
        exsit = self.driver.find_element_by_xpath(findname)
        if exsit :
            return True
        else:
            return False

    def get_name(self,name):
        '''
        定位页面text元素
        :param name:
        :return:
        '''
        # element = driver.find_element_by_name(name)
        # return element

        findname = "//*[@text='%s']"%(name)
        try:
            element = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath(findname))
            # element = self.driver.find_element_by_xpath(findname)
            self.driver.implicitly_wait(2)
            return element
        except:
            self.screenshot()
            log.error('未定位到元素:'+'%s')%(name)

    def get_id(self,id):
        '''
        定位页面resouce-id元素
        :param id:
        :return:
        '''
        try:
            element = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_id(id))
            # element = self.driver.find_element_by_id(id)
            self.driver.implicitly_wait(2)
            return element
        except:
            self.screenshot()
            log.error('未定位到元素:'+'%s')%(id)

    def get_xpath(self,xpath):
        '''
        定位页面xpath元素
        :param id:
        :return:
        '''
        try:
            element = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath(xpath))
            # element = self.driver.find_element_by_xpath(xpath)
            self.driver.implicitly_wait(2)
            return element
        except:
            self.screenshot()
            log.error('未定位到元素:'+'%s')%(xpath)

    def get_ids(self,id):
        '''
        定位页面resouce-id元素组
        :param id:
        :return:列表
        '''
        try:
            # elements = self.driver.find_elements_by_id(id)
            elements = WebDriverWait(self.driver, 10).until(lambda x: x.find_elements_by_id(id))
            self.driver.implicitly_wait(2)
            return elements
        except:
            self.screenshot()
            log.error('未定位到元素:'+'%s')%(id)

    def page(self,name):
        '''
        返回至指定页面
        :return:
        '''
        i=0
        while i<10:
            i=i+1
            try:
                findname = "//*[@text='%s']"%(name)
                self.driver.find_element_by_xpath(findname)
                self.driver.implicitly_wait(2)
                break
            except :
                os.popen("adb shell input keyevent 4")
                try:
                    findname = "//*[@text='确定']"
                    self.driver.find_element_by_xpath(findname).click()
                    self.driver.implicitly_wait(2)
                except:
                    os.popen("adb shell input keyevent 4")
                try:
                    self.driver.find_element_by_xpath("//*[@text='工作台']")
                    self.driver.implicitly_wait(2)
                    break
                except:
                    os.popen("adb shell input keyevent 4")
  • Operate.py
    I think is the most critical step. There is no page after that, this file is called for testing, mainly based on the read yaml file, and then the if...else... judgement, and corresponding respectively according to the corresponding opera_type Click, sendkeys and other operations
#coding=utf-8
#author='Shichao-Dong'

from GetYaml import getyaml
from BaseOperate import BaseOperate

class Operate:
    def __init__(self,path,driver):
        self.path = path
        self.driver = driver
        self.yaml = getyaml(self.path)
        self.baseoperate=BaseOperate(driver)

    def check_operate_type(self):
        '''
        读取yaml信息并执行
        element_info:定位元素信息
        find_type:属性,id、xpath、text、ids
        operate_type: click、sendkeys、back、swipe_up 为back就是返回,暂时就三种

        上面三个必填,operate_type必填!!!!!!

        send_content:send_keys 时用到
        index:ids时用到
        times:
        :return:
        '''

        for i in range(self.yaml.caselen()):
            if self.yaml.get_operate_type(i) == 'click':
                if self.yaml.get_findtype(i) == 'text':
                    self.baseoperate.get_name(self.yaml.get_elementinfo(i)).click()
                elif self.yaml.get_findtype(i) == 'id':
                    self.baseoperate.get_id(self.yaml.get_elementinfo(i)).click()
                elif self.yaml.get_findtype(i) == 'xpath':
                    self.baseoperate.get_xpath(self.yaml.get_elementinfo(i)).click()
                elif self.yaml.get_findtype(i) == 'ids':
                    self.baseoperate.get_ids(self.yaml.get_elementinfo(i))[self.yaml.get_index(i)].click()

            elif self.yaml.get_operate_type(i) == 'send_keys':
                if self.yaml.get_findtype(i) == 'text':
                    self.baseoperate.get_name(self.yaml.get_elementinfo(i)).send_keys(self.yaml.get_send_content(i))
                elif self.yaml.get_findtype(i) == 'id':
                    self.baseoperate.get_id(self.yaml.get_elementinfo(i)).send_keys(self.yaml.get_send_content(i))
                elif self.yaml.get_findtype(i) == 'xpath':
                    self.baseoperate.get_xpath(self.yaml.get_elementinfo(i)).send_keys(self.yaml.get_send_content(i))
                elif self.yaml.get_findtype(i) == 'ids':
                    self.baseoperate.get_ids(self.yaml.get_elementinfo(i))[self.yaml.get_index(i)].send_keys(self.yaml.get_send_content(i))

            elif self.yaml.get_operate_type(i) == 'back':
                for n in range(self.yaml.get_backtimes(i)):
                    self.baseoperate.back()

            elif self.yaml.get_operate_type(i) == 'swipe_up':
                for n in range(self.yaml.get_backtimes(i)):
                    self.baseoperate.swipe_up()

    def back_home(self):
        '''
        返回至工作台
        :return:
        '''
        self.baseoperate.page('工作台')

There are so many common parts of the code. When writing this framework, most of the energy is spent on this part, so I personally think it is worth studying

Page section

The page part is the minimum set of use cases, one module and one folder. Take the customer as an example.
At present, two use cases have been written, one for adding and one for sorting. The files are as follows:

 

 

file.png

 

The code is as follows, very concise,

import sys
reload(sys)
sys.setdefaultencoding('utf8')
import codecs,os
from public.Operate import Operate
from public.GetYaml import getyaml

PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)
yamlpath = PATH("../../testyaml/cm/cm-001addcm.yaml")

class AddcmPage:

    def __init__(self,driver):
        self.path = yamlpath
        self.driver = driver
        self.operate = Operate(self.path,self.driver)

    def operateap(self):
        self.operate.check_operate_type()

    def home(self):
        self.operate.back_home()

Run use cases

This part uses unittest to run all test cases and generate reports.
One use case per module, take the customer as an example: CmTest.py

from page.cm.CmAddcmPage import AddcmPage
from page.cm.CmSortcmPage import SortcmPage


from public.GetDriver import mydriver
driver = mydriver()

import unittest,time
class Cm(unittest.TestCase):

    def test_001addcm(self):
        '''
        新增客户
        :return:
        '''
        add = AddcmPage(driver)
        add.operateap()
        add.home()
    def test_002sortcm(self):
        '''
        客户排序
        :return:
        '''
        sort = SortcmPage(driver)
        sort.sortlist()
        sort.home()

    def test_999close(self):
        driver.quit()
        time.sleep(10)

if __name__ == "__main__":
    unittest.main()

First import all the use cases that need to be run from the page layer, and then run it with unittest.
If you want to run all test cases, you need to use runtest.py

import time,os
import unittest
import HTMLTestRunner
from testcase.CmTest import Cm


def testsuit():
    suite = unittest.TestSuite()
    suite.addTests([unittest.defaultTestLoader.loadTestsFromTestCase(Cm),




])

    # runner = unittest.TextTestRunner(verbosity=2)
    # runner.run(suite)

    now=time.strftime("%y-%m-%d-%H-%M-%S")
    PATH = lambda p: os.path.abspath(
        os.path.join(os.path.dirname(__file__), p)
    )
    dirpath = PATH("./results/waiqin365-")

    filename=dirpath + now +'result.html'
    fp=open(filename,'wb')
    runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='waiqin365 6.0.6beta test result',description=u'result:')

    runner.run(suite)
    fp.close()

if __name__ =="__main__":
    testsuit()

The idea here is similar, just import it first and then load the suite.

to sum up

For the time being, the separation of data and use cases has been achieved for the time being, but the writing requirements of yaml are relatively high and cannot be formatted incorrectly.
At the same time, there are some other areas that can be optimized, such as:

  • Judgment on the popup
  • Reconnect after disconnection mechanism
  • Rerun mechanism after failure

Guess you like

Origin blog.csdn.net/grl18840839630/article/details/109987870