1. 程式人生 > 其它 >UI自動化測試—資料驅動

UI自動化測試—資料驅動

在UI自動化測試中,我們經常會碰到相同的測試步驟,不同的測試場景,會把測試的資料分離到檔案中來統一管理測試數
據,Json檔案也是經常
我們以測試sina郵箱首頁登入場景為例,我們需要測試賬號密碼為空的場景、郵箱格式錯誤的場景、賬號密碼不符的場景
正常我們的程式碼為:
import time
from selenium import webdriver
import unittest

class SinaLogin(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get(
"https://mail.sina.com.cn/") self.driver.implicitly_wait(20) def tearDown(self) -> None: self.driver.quit() def test_sina_null(self): '''sina郵箱驗證,登入賬號祕密為空''' self.driver.find_element_by_id('freename').send_keys('') self.driver.find_element_by_id('freepassword
').send_keys('') self.driver.find_element_by_class_name('loginBtn').click() divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]') self.assertEqual(divText.text,'請輸入郵箱名') def test_sina_email_format(self): '''sina郵箱驗證,登入郵箱格式不正確
''' self.driver.find_element_by_id('freename').send_keys('qwdqh1231') self.driver.find_element_by_id('freepassword').send_keys('12312xs') self.driver.find_element_by_class_name('loginBtn').click() divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]') self.assertEqual(divText.text,'您輸入的郵箱名格式不正確') def test_sina_username_error(self): '''sina郵箱驗證,賬號密碼不匹配''' self.driver.find_element_by_id('freename').send_keys('[email protected]') self.driver.find_element_by_id('freepassword').send_keys('jasbahc12') self.driver.find_element_by_class_name('loginBtn').click() time.sleep(1) divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]') self.assertEqual(divText.text,'登入名或密碼錯誤') if __name__ == '__main__': unittest.main(verbosity=2)
下面以不同的檔案分離為例完成自動化的測試

一.Json

首先我們需要把資料分離出來寫入Json的檔案中,檔案路徑為:UI自動化測試->data->sina.json
{
  "loginNull": {"username": "","password": "","result": "請輸入郵箱名"},
  "emailFormat": {"username": "sadert","password": "asrtr","result": "您輸入的郵箱名格式不正確"},
  "loginError": {"username": "[email protected]","password": "aserty","result": "登入名或密碼錯誤"}
}

呼叫json檔案,檔案路徑為:UI自動化測試->test_sina->utils->operationJon.py

import json
import os

def  base_dir():
    return os.path.dirname(os.path.dirname(os.path.dirname(__file__)))   #當前檔案的上上級路徑,也就是UI自動化測試

def readJson():
    return json.load(open(os.path.join(base_dir(),'data','sina.json'),encoding='utf-8'))   
'''路徑的拼接,找到sina.json檔案,然後將檔案反序列化讀取,並以'utf-8'解碼'''

呼叫json檔案的輸出結果為一個字典:

{'loginNull': {'username': '', 'password': '', 'result': '請輸入郵箱名'}, 'emailFormat': {'username': 'sadert', 'password': 'asrtr', 'result': '您輸入的郵箱名格式不正確'}, 'loginError': {'username': '[email protected]', 'password': 'aserty', 'result': '登入名或密碼錯誤'}}

測試程式碼:

import time
from selenium import webdriver
import unittest
from UI自動化測試.test_sina.utils.operationJson import readJson

class SinaLogin(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get("https://mail.sina.com.cn/")
        self.driver.implicitly_wait(20)

    def tearDown(self) -> None:
        self.driver.quit()

    def test_sina_null(self):
        '''sina郵箱驗證,登入賬號祕密為空'''
        self.driver.find_element_by_id('freename').send_keys(readJson()['loginNull']['username'])
        self.driver.find_element_by_id('freepassword').send_keys(readJson()['loginNull']['password'])
        self.driver.find_element_by_class_name('loginBtn').click()
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        self.assertEqual(divText.text,readJson()['loginNull']['result'])

    def test_sina_email_format(self):
        '''sina郵箱驗證,登入郵箱格式不正確'''
        self.driver.find_element_by_id('freename').send_keys(readJson()['emailFormat']['username'])
        self.driver.find_element_by_id('freepassword').send_keys(readJson()['emailFormat']['password'])
        self.driver.find_element_by_id('freename').send_keys('jasbahc12')
        self.driver.find_element_by_class_name('loginBtn').click()
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        self.assertEqual(divText.text,readJson()['emailFormat']['result'])

    def test_sina_username_error(self):
        '''sina郵箱驗證,賬號密碼不匹配'''
        self.driver.find_element_by_id('freename').send_keys(readJson()['loginError']['username'])
        self.driver.find_element_by_id('freepassword').send_keys(readJson()['loginError']['password'])
        self.driver.find_element_by_class_name('loginBtn').click()
        time.sleep(1)
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        self.assertEqual(divText.text,readJson()['loginError']['result'])

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

二.Yaml

三.Csv

四.Excel

五.MysQl