一、背景介绍
xx银行智能广告投放管理系统。
在公司开发一套基础的标准化产品,再去客户现场进行产品实施。
二、业务介绍
行方业务人员通过该广告投放管理系统,实现对不同媒体渠道的接入管理、广告计划的创建、广告落地页的自定义、广告投放数据分析等功能。
三、测试方案
1、痛点:
在客户现场实施的过程中,经常面临需求临时变更、定制化需求的新增,导致手动回归测试时间紧压力大、覆盖场景不全面等问题。
2、解决方案:
将公司内开发完的基础版的标准化产品,进行接口、web自动化,主要以接口自动化为主。以此来缓解现场实施过程中的回归测试压力,每当面临新增需求或者需求变更时,可以通过执行自动化测试用例的方式进行基础功能的回归测试,一些优先级高、场景复杂的功能点采用手工测试进行回归。
四、自动化测试实践
1、接口自动化
基于pytest+requests实现接口自动化,通过conftest实现数据共享,将测试环境信息、测试账户等信息存放在conftest文件中进行管理。
部分代码示例:
import pytest
import requests
import yaml
from conftest import global_env
import hashlib
class TestSuit:
def setup_class(self):
path_env = global_env.get("env")
# path_env = os.getenv("interface_env")
env = yaml.safe_load(open(f"{path_env}.yaml", encoding="utf-8"))
self.base_url = env['base_url']
self.username = env['user_name']
self.password = env['password']
input_passwd = hashlib.md5()
input_passwd.update(self.password.encode('utf-8'))
self.password = input_passwd.hexdigest().lower()
def get_token(self):
path = '/auth/login'
url = self.base_url + path
payload = {
'userName': self.username,
'pwd': self.password
}
r = requests.post(url=url, json=payload)
return r.json()['data']['token']
def get_user_info(self, userName, name, token):
path = '/auth/users/pageList'
url = self.base_url + path
body = {
"pageNum": 1,
"pageSize": 30,
"userName": userName,
"name": name
}
header = {"token": token}
r = requests.post(url=url, headers=header, json=body)
return r.json()['data'][0]['id']
def get_media_info(self, mediaName, mediaCode, token):
path = '/media_info/query_info'
url = self.base_url + path
body = {
"pageNum": 1,
"pageSize": 30,
"mediaName": mediaName,
"mediaCode": mediaCode
}
header = {"token": token}
r = requests.post(url=url, headers=header, json=body)
return r.json()['data'][0]['id']
def get_callback_code_info(self, mediaName, mediaCode, token):
path = '/callback/media/pageQuery'
url = self.base_url + path
body = {
"mediaName": mediaName,
"mediaCode": mediaCode,
"pageNum": 1,
"pageSize": 30
}
header = {"token": token}
r = requests.post(url=url, headers=header, json=body)
return r.json()['data'][0]['id']
# 系统账户
def test_login(self):
path = '/auth/login'
url = self.base_url + path
payload = {
'userName': self.username,
'pwd': self.password
}
r = requests.post(url=url, json=payload)
print(r.json())
assert r.json()['code'] == '0000'
@pytest.mark.parametrize('userName, name, phone, email',
[('test_user007',
'测试用户007',
'18252888533',
'test007@163.com')],
ids=['新增测试用户'])
def test_add_uesr(self, userName, name, phone, email, status='true', roleCode=["admin"], roles=[{'id':1, 'roleCode':"admin", 'roleName':"管理员", 'remark':"管理员"}]):
token = self.get_token()
path = '/auth/users/add'
url = self.base_url + path
body = {
'userName': userName,
'name': name,
'phone': phone,
'email': email,
'status': status,
'roleCode': roleCode,
'roles': roles
}
header = {"token": token}
r = requests.post(url=url, headers=header, json=body)
print(r.json())
assert r.json()['code'] == '0000'
2、web自动化
通过selenium 实现web自动化
部分代码示例:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestBaixinWeb:
def setup_class(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(3)
def teardown_class(self):
self.driver.quit()
def test_login(self):
self.driver.get("http://mktadmin.aibankcorp.com/login")
self.driver.find_element(By.CSS_SELECTOR, '[placeholder="请输入用户名"]').send_keys("fengkailun")
time.sleep(1)
self.driver.find_element(By.CSS_SELECTOR, '[placeholder="请输入密码"]').send_keys("zhiniu123")
time.sleep(1)
self.driver.find_element(By.CSS_SELECTOR, '[type="button"]').click()
time.sleep(1)
def test_query_user(self):
self.driver.find_element(By.CSS_SELECTOR, '[alt="滑动块"]').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[text()="系统账户"]').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[text()="用户管理"]').click()
time.sleep(1)
self.driver.find_element(By.CSS_SELECTOR, '[placeholder="登录名"]').send_keys('fengkailun')
time.sleep(1)
self.driver.find_element(By.CSS_SELECTOR, '#newSearchBtn').click()
time.sleep(1)