webUI自动化(七)应用pytest框架工具到webUI自动化----Excel关键字驱动
一、结合pytest框架,整理setup,teardown,testcase部分
setup部分
def setup_class ( self) :
"""前置的 数据初始化内容"""
excel_path = "../data/data.xlsx"
self. excel_obj = openpyxl. load_workbook( excel_path)
config_path = "../config/config.yaml"
config_sheet_values = self. excel_obj[ '配置项' ] . values
config_dict = dict ( )
for config_data in config_sheet_values:
if config_data[ 0 ] != '编号' :
config_dict[ config_data[ 1 ] ] = config_data[ 2 ]
yaml_w_write( config_path, config_dict)
self. b = Base( config_path)
self. cr = CaseRunner( self. excel_obj, self. b)
teardown部分
def teardown_class ( self) :
self. b. wait( { 'txt' : '5' } )
self. excel_obj. close( )
self. b. quit( { } )
二、新建py文件,作为用例运行器,逻辑代码的封装,case_runner.py
class CaseRunner :
def __init__ ( self, excel_obj, base_obj) :
self. excel_obj = excel_obj
self. b = base_obj
def case_runner ( self, sheet_name) :
sheet_obj = self. excel_obj[ sheet_name]
values = sheet_obj. values
for value in values:
data_dict = dict ( )
if value[ 0 ] != "编号" and value[ 1 ] == 1 :
data_dict[ 'no' ] = value[ 0 ]
data_dict[ 'yes_no' ] = value[ 1 ]
data_dict[ 'step_name' ] = value[ 2 ]
data_dict[ 'kw' ] = value[ 3 ]
data_dict[ 'locate_by' ] = value[ 4 ]
data_dict[ 'locate_value' ] = value[ 5 ]
data_dict[ 'txt' ] = value[ 6 ]
data_dict[ 'note' ] = value[ 7 ]
data_dict[ 'except' ] = value[ 8 ]
try :
getattr ( self. b, data_dict[ 'kw' ] ) ( data_dict)
print ( data_dict[ 'step_name' ] , '执行成功!' )
except Exception as e:
print ( data_dict[ 'step_name' ] , '执行失败!' , e)
raise
三、测试用例编写
def test_bilibili ( self) :
self. cr. case_runner( "bilibili" )
四、添加 pytest.ini
[ pytest]
addopts = - v - s
五、完整代码
目录结构 base_07_应用pytest框架工具到webUI自动化_Excel关键字驱动.py
from time import sleep
from selenium import webdriver
from selenium. webdriver. support. wait import WebDriverWait
from base. chrome_options import chrome_options
from base. yaml_tools import yaml_read
from urllib. parse import urljoin
class Base :
def __init__ ( self, config_file_path) :
self. config_dict = yaml_read( config_file_path)
options = chrome_options( )
if self. config_dict[ 'headless' ] == 1 :
options. add_argument( "--headless" )
self. driver = webdriver. Chrome( chrome_options= options)
self. driver. implicitly_wait( 5 )
def open ( self, data_dict) :
"""打开网址"""
base_url = self. config_dict[ data_dict[ 'locate_by' ] ]
self. driver. get( urljoin( base_url, data_dict[ 'txt' ] ) )
def locate ( self, data_dict) :
"""定位,应用显式等待"""
"""
WebDriverWait(driver,timeout,poll_frequency,ignored_exceptions)
driver:浏览器驱动
timeout:最长超时时间,默认以秒为单位。
poll_frequency:检测的间隔步长,默认为0.5s。即每0.5s询问一次,元素是否找到。
ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常。
"""
try :
ele = WebDriverWait( self. driver, 6 , 0.5 ) . until(
lambda s: s. find_element( data_dict[ 'locate_by' ] , data_dict[ 'locate_value' ] ) )
return ele
except :
print ( "定位失败" )
raise
def input ( self, data_dict) :
"""输入"""
self. locate( data_dict) . send_keys( data_dict[ 'txt' ] )
def click ( self, data_dict) :
"""单击"""
self. locate( data_dict) . click( )
def max_window ( self, data_dict) :
"""窗口最大化"""
self. driver. maximize_window( )
def switch_to_iframe ( self, data_dict) :
"""切换到iframe"""
self. driver. switch_to. frame( data_dict[ 'txt' ] )
def switch_to_windows ( self, data_dict) :
"""切换新窗口"""
windows = self. driver. window_handles
self. driver. switch_to. window( windows[ int ( data_dict[ 'txt' ] ) ] )
def assert_locate ( self, data_dict) :
"""断言"""
assert self. locate( data_dict) is not None , '断言失败'
print ( '断言成功!' )
def wait ( self, data_dict) :
"""固定等待"""
sleep( int ( data_dict[ 'txt' ] ) )
def quit ( self, data_dict) :
"""退出浏览器"""
self. driver. quit( )
case_runner.py
class CaseRunner :
def __init__ ( self, excel_obj, base_obj) :
self. excel_obj = excel_obj
self. b = base_obj
def case_runner ( self, sheet_name) :
sheet_obj = self. excel_obj[ sheet_name]
values = sheet_obj. values
for value in values:
data_dict = dict ( )
if value[ 0 ] != "编号" and value[ 1 ] == 1 :
data_dict[ 'no' ] = value[ 0 ]
data_dict[ 'yes_no' ] = value[ 1 ]
data_dict[ 'step_name' ] = value[ 2 ]
data_dict[ 'kw' ] = value[ 3 ]
data_dict[ 'locate_by' ] = value[ 4 ]
data_dict[ 'locate_value' ] = value[ 5 ]
data_dict[ 'txt' ] = value[ 6 ]
data_dict[ 'note' ] = value[ 7 ]
data_dict[ 'except' ] = value[ 8 ]
try :
getattr ( self. b, data_dict[ 'kw' ] ) ( data_dict)
print ( data_dict[ 'step_name' ] , '执行成功!' )
except Exception as e:
print ( data_dict[ 'step_name' ] , '执行失败!' , e)
raise
chrome_options.py
from selenium import webdriver
def chrome_options ( ) :
options = webdriver. ChromeOptions( )
options. add_experimental_option( 'excludeSwitches' , [ 'enable-automation' ] )
options. add_argument( 'start-maximized' )
prefs = { "credentials_enable_service" : False , "pofile.password_manager_enable" : False }
options. add_experimental_option( "prefs" , prefs)
options. add_argument( "--no-sandbox" )
options. add_argument( '--kiosk-printing' )
return options
yaml_tools.py
import yaml
def yaml_read ( file_path) :
"""read"""
with open ( file_path, 'r' , encoding= 'utf-8' ) as f:
x = yaml. load( f, Loader= yaml. FullLoader)
return x
def yaml_a_write ( file_path, data) :
"""追加写入"""
f = open ( file_path, 'a' , encoding= 'utf-8' )
yaml. dump( data, f, allow_unicode= True )
f. close( )
def yaml_w_write ( file_path, data) :
"""覆盖写入"""
f = open ( file_path, 'w' , encoding= 'gbk' )
yaml. dump( data, f, allow_unicode= True )
f. close( )
def yaml_dict_update ( file_path, dict_data) :
"""对yaml(only for dict)进行更新"""
try :
x = yaml_read( file_path)
if not x:
x = dict ( )
x. update( dict_data)
yaml_w_write( file_path, x)
except :
print ( f' { file_path} <<<<< { dict_data} 更新失败!!!' )
raise
if __name__ == '__main__' :
res = yaml_read( r"E:\pythonProject\ZrlogWebUI\config\config.yaml" )
print ( res)
test_bilibili_07.py
import openpyxl
import pytest
from base. base_07_应用pytest框架工具到webUI自动化_Excel关键字驱动 import Base
from base. case_runner import CaseRunner
from base. yaml_tools import yaml_w_write
class Testcase ( ) :
def setup_class ( self) :
"""前置的 数据初始化内容"""
excel_path = "../data/data.xlsx"
self. excel_obj = openpyxl. load_workbook( excel_path)
config_path = "../config/config.yaml"
config_sheet_values = self. excel_obj[ '配置项' ] . values
config_dict = dict ( )
for config_data in config_sheet_values:
if config_data[ 0 ] != '编号' :
config_dict[ config_data[ 1 ] ] = config_data[ 2 ]
yaml_w_write( config_path, config_dict)
self. b = Base( config_path)
self. cr = CaseRunner( self. excel_obj, self. b)
def teardown_class ( self) :
self. b. wait( { 'txt' : '5' } )
self. excel_obj. close( )
self. b. quit( { } )
def test_bilibili ( self) :
self. cr. case_runner( "bilibili" )
if __name__ == '__main__' :
pytest. main( [ "test_bilibili_07.py" ] )
pytest.ini
```python
[ pytest]
addopts = - v - s