robot framework读取excel表格实现数据驱动


1.安装excellibrary,注意python3.7版本,需要安装一下两个库

pip install robotframework-excel
pip install robotframework-excellibrary-xwfintech

注意:

网上有很多关于excel处理的,都是基于python2.7的,还有一些对python3以上都是修改包的方式,其实都不需要,在pip官网上有python3以上的excellibrary,名称是robotframework-excellib 2.0.0,但是该包关键字不够丰富,建议安装robotframework-excellibrary-xwfintech包,这个跟python2.7版本robotframework-excellibrary一样

2.Excel表中单个sheet处理关键字用途

Open Excel 打开Excel文件
Get Row Count 获取行数
Get Column Count 获取列数
Get Row Values 获取某一行的值
Get Column Values 获取某一列的值
Read Cell Data By Coordinates 通过列行编号获取值
Read Cell Data By Name 按名称读取单元格数据

注意:Excel只能支持 xls格式,切记!!!

有一张Excel表格如下:

robot套件进行读取

*** Settings ***
Library   ExcelLibrary

*** Test Cases ***
case03表格读写
    #打开Excel表格
    Open Excel    E:\\woniuboss_webui\\woniuboss.xls
    #读取名为添加资源sheet中每一列的值,最后的数字代表的是第几列,0代表第一列
    #获取最大行数
    ${rowCount}    Get Row Count    添加资源
    FOR   ${row}  IN RANGE  1   ${rowCount}
        ${phonenum}  Read Cell Data By Coordinates  添加资源   0   ${row}
        ${name}  Read Cell Data By Coordinates  添加资源   1   ${row}
        ${status}  Read Cell Data By Coordinates  添加资源   2   ${row}
        ${source}  Read Cell Data By Coordinates  添加资源   3   ${row}
        log to console  ${phonenum}
        log to console  ${name}
        log to console  ${status}
        log to console  ${source}
        log to console  ****************
    END

实战:

 

*** Settings ***
Library  pages.reasourcemanagement.trainingresourcesPage.TrainingResources_business
Library   ExcelLibrary


*** Test Cases ***
#case02新增培训资源
#    ${assert_info}   add_resources   17562244325   张三丰   已上门   360竞价
#    should be equal   ${assert_info}    新增成功.


case03新增培训资源
    #打开Excel表格
    Open Excel    E:\\woniuboss_webui\\woniuboss.xls
    #读取名为添加资源sheet中每一列的值,最后的数字代表的是第几列,0代表第一列
    #获取最大行数
    ${rowCount}    Get Row Count    添加资源
    FOR   ${row}  IN RANGE  1   ${rowCount}
        ${phonenum}  Read Cell Data By Coordinates  添加资源   0   ${row}
        ${name}  Read Cell Data By Coordinates  添加资源   1   ${row}
        ${status}  Read Cell Data By Coordinates  添加资源   2   ${row}
        ${source}  Read Cell Data By Coordinates  添加资源   3   ${row}

        #直接读出来手机号码会转换给浮点型 类似18291579544.0 的格式,所以Convert To Integer转换成整数
        ${phoneInt}   Convert To Integer  ${phonenum}
        #调用新增资源信息接口,进行添加资源
        ${assert_info}   add_resources   ${phoneInt}   ${name}   ${status}   ${source}
        should be equal   ${assert_info}    新增成功.
    END


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM