python+selenium自动化测试-16自动化测试模型

自动化测试模型,即自动化测试框架与工具设计的思想。有四种常用模型,分别是线性模型、模块化驱动模型、数据驱动模型和关键字驱动模型。

在工作实践中,最常用的是模块化驱动和数据驱动两种,联合使用的场景十分广泛。

1、线性模型

概念:线性模型的每个脚本都是相互独立的,且不会产生其他依赖与调用,单纯的来模拟用户完整的操作场景。逻辑清晰,但缺点非常明显:开发成本高,用例之间存在重复的操作。
线性模型用一个简单的登录后退出的操作说明:

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()#推荐使用Firefox

# 加载浏览器驱动
driver.get("http://localhost")

driver.find_element_by_name("username").clear()#清空文本框的用户名
driver.find_element_by_name("username").send_keys("forTest")

driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("123456")

driver.find_element_by_name("Submit").click()# 点击“登录”按钮
sleep(3)#这里使用了显式等待,等待登录后的首页页面元素加载完成。否则下面可能会找不到‘退出’。

driver.find_element_by_link_text("退出").click()
sleep(2)
driver.switch_to_alert().accept()#点击退出后会弹出提示框,要点击确认

driver.quit()# 关闭浏览器及其驱动,单个窗口也可以使用driver.close()

2、模块化驱动模型

概念:将重复的操作封装成公共模块,当用例执行过程中需要用到这一模块操作时直接调用即可。相对与线性模型,最大的优势是最大限度消除了重复,从而提高了开发效率和提高测试用例的可维护性。缺点:虽然操作步骤相同,但是用到的测试数据可能不同。

(1)common.py
import random

class common():
	def type_getRandomNum(self):
		try:
			self.randoma = random.randint(100000, 999999)
            return self.randoma
		except BaseException as msg:
		print(msg)
(2)instance1.py
import common

def instance1(self):
	try:
		randomNum1 = common.type_getRandomNum()
		print("randomNum1 is :%d"%randomNum1)
	except BaseException as msg:
		print(msg)
(3)instance2.py
import common

def instance2(self):
	try:
		randomNum2 = common.type_getRandomNum()
		print("randomNum2 is :%d"%randomNum2)
	except BaseException as msg:
		print(msg)

3、数据驱动模型

概念:将测试中的测试数据和操作分离,数据存放在文件中单独维护(不直接放在代码中)。通过数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变。实际就是将测试数据参数化。

(1)test_login.py
from login import test_login
from testCase.model.getuserInfo import getYaml

class userMgt():
    def test_login(self):
        #调用函数获取账号密码
        data = getYaml('userInfo.yaml')
        username = data['user']['username']
        psd = data['user']['password']
        test_login.login_action(self,username,psd)
        sleep(3)
(2)login.py
from selenium import webdriver
from time import sleep
class test_login():
	def login_action(self,username,psd):#公共模块参数化
		driver = webdriver.Firefox()
		driver.get("http://localhost")
		
		driver.find_element_by_name("username").clear()
		driver.find_element_by_name("username").send_keys(username)
		
		driver.find_element_by_name("password").clear()
		driver.find_element_by_name("password").send_keys(psd)
		
		driver.find_element_by_name("Submit").click()
(3)getuserInfo.py
from ruamel import yaml

def getYaml(yamlName):
    #读取yaml文件
    with open('F:\\phomeNetFront\\test_data\\yaml\\'+yamlName,'r',encoding="utf-8") as file:
        data=yaml.load(file, Loader=yaml.RoundTripLoader)
        return data
(4)userInfo.yaml
user:
  username: forTest
  password: 123456

4、关键字驱动模型

概念:即通过关键字的改变引起测试结果的改变。Selenium IDE也是一种传统的关键字驱动的自动化工具,可以进行录制、回放等操作。对于没多少编程经验的初学者来说,Selenium IDE是一个不错的自动化测试入门工具。这里不具体展开。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章