基於appium實現的線性代碼引用unittest單元測試框架

在前段時間,分享了幾篇關於appium基礎的博文,再加上期間也有講到unittest測試框架,因此今天就來一個專題,在appium+python實現的線性代碼基礎上,引入unittest框架,使代碼更簡潔。html

以前的博客,能夠見如下連接快速閱讀:python

基於Python的Appium環境搭建合集android

Genymotion模擬器的安裝及腳本製做web

Appium Python API 中文版python3.x

Appium-Server與Appium-Desktop的區別app

單元測試框架Uinttest一文詳解框架

在以上博文中,代碼示例,不少只是個線性腳本,沒有太多的實用之處,用來寫個demo仍是能夠,但實際運用到產品中,就不行了。腳本仍是得引用框架,這樣看起來,代碼就不會那麼亂,更有邏輯性,便於維護。函數

好了,進入正題,對unittest以及appium還不熟悉的,能夠先閱讀如上的博文了解,此篇文中,就不贅述了。post

unittest代碼優化一

優化邏輯:單元測試

①將啓用服務字段放到初始化當中

②將輸入帳號、輸入密碼、獲取當前activity的操做封裝成一個個函數

③在用例中去調用須要執行的函數

示例代碼以下:

import selenium
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest

'''
@author: wenyihuqingjiu
@project: appiumcode
@file: test_login.py
@time: 2019-10-20 22:11
@desc:
'''

print('selenium version = ', selenium.__version__)


def input_account(self):
    account = self.driver.find_element_by_id('id/rel_content')
    account.find_element_by_id('id/edit_acount')
    print("定位輸入框")
    self.driver.press_keycode(61)
    print("換行操做")
    account.send_keys('')
    print("帳號輸入完畢")
    time.sleep(2)


def current(self):
    ac = self.driver.current_activity
    print('當前activity:' + ac)


def input_password(self):
    # genymotion只須要一次換行
    self.driver.press_keycode(61)
    print("換行操做")
    password = self.driver.find_element_by_id('id/rel_content')
    password.find_element_by_id('id/edit_pass')
    print("定位輸入框")
    password.send_keys('')
    print("輸入密碼")
    self.driver.find_element_by_xpath('//android.widget.TextView[@text="登 錄"]').click()
    print("點擊登陸")
    time.sleep(5)
    print('登陸成功')


class case_01(unittest.TestCase):
    @classmethod
    def setUpClass(cls):

        desired_caps = {

            'platformName': 'Android',
            'platformVersion': '4.4.4',
            'deviceName': 'S5',
            'appPackage': '',
            # 非首次登陸的activity
            'appActivity': '.ui.login.view.LoginActivity',
            'udid': '192.168.66.101:5555',
            'noReset': 'true',


        }
        cls.driver = webdriver.Remote('http://192.168.1.101:4723/wd/hub', desired_caps)
        print("服務啓動成功")

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def add_img(self):
        self.imgs.append(self.driver.get_screenshot_as_base64())
        return True

    def setUp(self):
        # 在是python3.x 中,若是在這裏初始化driver ,由於3.x版本 unittestbasic1 運行機制不一樣,會致使用力失敗時截圖失敗
        self.driver.implicitly_wait(30)
        self.imgs = []
        self.addCleanup(self.cleanup)

    def cleanup(self):
        pass

    def test_login01(self):
        input_account(self)
        current(self)
        input_password(self)


if __name__ == "__main__":
    suites = unittest.TestSuite()
    suites.addTest(case_01("test_login01"))
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suites)

unittest代碼優化二

優化邏輯:

①將啓用服務字段放到初始化當中

②將輸入帳號、輸入密碼、獲取當前activity的操做封裝成一個個函數,並將數據參數作成形參

③在用例中去調用須要執行的函數併入參

示例代碼以下:

import selenium
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest

'''
@author: wenyihuqingjiu
@project: appiumcode
@file: test_login.py
@time: 2019-10-20 22:11
@desc:
'''

print('selenium version = ', selenium.__version__)


def input_account(self, username):
    account = self.driver.find_element_by_id('id/rel_content')
    account.find_element_by_id('id/edit_acount')
    print("定位輸入框")
    self.driver.press_keycode(61)
    print("換行操做")
    account.send_keys(username)
    print("帳號輸入完畢")
    time.sleep(2)


def current(self):
    ac = self.driver.current_activity
    print('當前activity:' + ac)


def input_password(self, send_password):
    # genymotion只須要一次換行
    self.driver.press_keycode(61)
    print("換行操做")
    password = self.driver.find_element_by_id('id/rel_content')
    password.find_element_by_id('id/edit_pass')
    print("定位輸入框")
    password.send_keys(send_password)
    print("輸入密碼")
    self.driver.find_element_by_xpath('//android.widget.TextView[@text="登 錄"]').click()
    print("點擊登陸")
    time.sleep(5)
    print('登陸成功')


class case_01(unittest.TestCase):
    @classmethod
    def setUpClass(cls):

        desired_caps = {

            'platformName': 'Android',
            'platformVersion': '4.4.4',
            'deviceName': 'S5',
            'appPackage': '',
            # 非首次登陸的activity
            'appActivity': '.ui.login.view.LoginActivity',
            'udid': '192.168.66.101:5555',
            'noReset': 'true',


        }
        cls.driver = webdriver.Remote('http://192.168.1.101:4723/wd/hub', desired_caps)
        print("服務啓動成功")

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def add_img(self):
        self.imgs.append(self.driver.get_screenshot_as_base64())
        return True

    def setUp(self):
        # 在是python3.x 中,若是在這裏初始化driver ,由於3.x版本 unittestbasic1 運行機制不一樣,會致使用力失敗時截圖失敗
        self.driver.implicitly_wait(30)
        self.imgs = []
        self.addCleanup(self.cleanup)

    def cleanup(self):
        pass

    def test_login01(self):
        # 正常登陸
        input_account(self, "")
        current(self)
        input_password(self, "")
        
    def test_login02(self):
        # 異常登陸
        input_account(self, "")
        current(self)
        input_password(self, "")


if __name__ == "__main__":
    suites = unittest.TestSuite()
    suites.addTest(case_01("test_login01"))
    suites.addTest(case_01("test_login02"))
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suites)

經過上述代碼可看出,使用更方便了,能夠知足正向/反向的用例設計了,是否是很簡單。

代碼可擴展性還有不少,在unittest基礎上,再引入PO模式,那代碼的健壯性就更高了,之後有時間再出相似博文。

 

本文僅表明做者觀點,系做者@溫一壺清酒發表。
歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
文章出處:http://www.cnblogs.com/hong-fithing/
相關文章
相關標籤/搜索