python+selenium

基本介绍

测试数据来源于数据库,采用python+selenium的方式执行UI 自动化,执行完成后将结果存进数据库。实现测试数据与代码分离,方便后续维护。

具体思路是按照task -》 case -》 case step的层级展开:

task中包含一个或多个case_id,case_id包含多个step_id,每个step对应一个element,一个action,input(可以是空)和expected value(可以是空)

在demo_case.py文件中直接

代码路径:https://git.garena.com/jingyun.qi/test/

DB 设计

  • element: elementId; module; elementName; type; xpath; css; className; id; url
  • CommonAction: actionTypeId; typeName; description; actionMethod

  • CaseTemplate: caseTemplateId; product; description 

  • TestCase: caseId; caseName; caseTemplateId

  • CaseStep: stepId; stepName; caseId; element; actionTypeId; inputValue; expectedValue; dbConnectionId; dbQuery; description

  • DbConnection: dbConnectionId, userName, password, portNumber    

  • task:  task_id; task_name; test_case_ids;  folder; status; executor;  exec_settings; exec_summary; exec_start_time; exec_end_time; root_paths; description

  • execution: exec_id; task_id; test_case_id; status; result_data; start_time; elapsed_time; remark

蓝色字段为主键,自增, 红色字段为唯一索引

element表存放所有页面元素的locator,支持xpath,css,className,id,url。 module指定element归属于哪个project哪个模块,可以是类似rcmd_queue或者rcmd_bundle这样的,表示这个element是recommendation项目中的queue相关页面元素。

CommonAction表存放定义的一些common action,比如open_url,button_action,input_action,dropdown_list等, 以及一些验证结果的方法,get_method(通过获取api的结果验证),verify_content(通过获取某个元素的内容验证)

CaseStep表存放case的每个步骤,一个case包含多个步骤,只需要指定caseId相同即可。具体来说如何添加一个step呢?

  1. 在element表中找到所需的element name,如果element表没有需要手动插进去。这个就是CaseStep中的element字段
  2. 在CommonAction表中找到对应的动作actionTypeId,如果表中没有需要手动插进去(常规的方法应该都会提前写好存入CommonAction表中)。这个就是CaseStep中的actionTypeId字段
  3. 填充inputValue字段,如果动作无须input,置空
  4. 填充expectedValue字段,倘若该step无需检查结果,置空
  5. dbConnectionId,若这个步骤需要从连接数据库查询,填充该字段,并在DbConnection表中填写对应信息
  6. dbQuery,若这个步骤需要从连接数据库查询,sql语句填充该字段

至此,一个casestep所需的内容基本齐全,可以将一个case拆分成多个step,按照上述方法添加进CaseStep

TestCase表存放case基本信息

CaseTemplate表存放case template基本信息,可以理解为我们有很多模块,比如recommendation,anti fraud等,描述各个module的基本情况

DbConnection表存放db connection信息,用于连接数据库验证某些页面操作的结果

task表存放一次执行case集合的结果,task_name指定此次task执行哪个项目用例,task_name的默认格式是***_UI_Automation,***标示项目,比如RCMD_UI_Automation    antifraud_UI_Automation。这样写的目的是读取_之前的项目名称,据此从elements表中对比module字段读取element信息。


test_case_ids=[1,2,3]表示该次执行1,2,3三个case,exec_summary记录此次task的执行结果,total,fail,elapsed_time

execution表存放每个case在每个task中的具体执行结果,比如case 1在task 2中执行的结果,status记录执行成功(pass)还是失败(fail),result_data记录case每一个step的

actual_result和expected_result。倘若某一步无须验证,在result_data中显示{"expected": "", "actual": ""}。比如:

[{"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "", "actual": ""}, {"expected": "{"feed_queue": {"expire": 1595328790, "has_more": false, "version": "v1", "groups": [{"data": [{"from": "FV2", "data_type": "feed", "country": "ID", "feedid": 939943621416960, "score": 3.457621, "feed_author": 182560116}, {"from": "FV2", "data_type": "feed", "country": "ID", "feedid": 879914263155200, "score": 8.282281, "feed_author": 1290517}], "key": "default"}], "time": 1595325190, "has_new": false}}", "actual": "{"feed_queue": {"version": "v1", "has_more": false, "expire": 1595328790, "groups": [{"data": [{"from": "FV2", "data_type": "feed", "country": "ID", "feedid": 939943621416960, "score": 3.457621, "feed_author": 182560116}, {"from": "FV2", "data_type": "feed", "country": "ID", "feedid": 879914263155200, "score": 8.282281, "feed_author": 1290517}], "key": "default"}], "time": 1595325190, "has_new": false}}"}]


使用方法

step 1: 打开prepare_data.xlsx文件,往对应的sheet页中加需要的测试数据,执行insert.py文件,会自动将数据存放进数据库中对应的表

注意:所有的elements和action都是可以复用的,若没有所需的再新增即可

step 2: 右键执行 testcase.py文件开始执行一个task,会依次执行task中的一个个case

step 3: 执行用例后可去execution表查询对应的case执行结果;去task表查询task执行结果

FAQ

  1. 我想添加一个case,怎么加?
    打开excel文件,一个sheet页对应一张表
    1)往CaseTemplate sheet页和TestCase sheet页加一条数据,添加一些描述信息
    2)将case拆解成step,比如step 1 打开某个url,step 2 点击某个按钮,step 3在某个输入框中输入内容,step 4点击某个确认按钮
    3)针对每个step,在element sheet页和CommonAction sheet页找到对应的元素和操作,没有就自己加进去
    4)准备工作做好后往CaseStep sheet页中插入数据,注意input字段和expected字段
  2. 我想执行用例,怎么做?
    往task sheet页中插入一个task,指定test_case_ids,程序会根据task id中的case id自动去case 表和CaseStep表中搜寻到对应case_id下的step跑起来,并将task结果和具体的case执行结果分别写进task表和execution表
  3. 登陆页面需要输入账号密码以及验证码?
    采用保存有效cookie的方式,只需第一次登录时,手动输入一次账号密码以及验证码,cookie会保存在cookie.txt文件中,以后每次登陆会去该文件中读取cookie避免登陆
  4. 如何验证UI自动化结果?
    可以将验证步骤当做一个step放在case中,case step中的action_type目前支持get_method(通过调用api验证response)和verify_content(通过验证页面元素内容验证)
  5. 如果task中某个case执行过程中失败了,整个task能否继续执行
    可以,不影响其他case执行
  6. 往CaseStep表中插入数据时,需要case id和step id,是否一定要按照顺序插入顺序
    同一个case id下,step的顺序由使用者自己保证。我们需要按照页面操作顺序插入。但是不同的case,比如case 1,step 1;case 2,step 2;case 1 ,step 3;case 2, step 4这种是没有问题的,会按照case id对step进行重组。
  7. 若执行失败了可以在screen文件夹下查看到对应的错误action截图,同时,运行界面console中会打出出错信息,方便定位问题
    打算后续将错误信息一同记录在execution表中。。。
  8. 往数据库中添加数据时,会根据每张表的唯一索引判断,若在表中已经存在,则更新该条记录;若不存在,则新增进表中

to be continued....

原文地址:https://www.cnblogs.com/xingxing666/p/14898895.html