1.封装连接数据库
def mysql_selectab(sql, host, user,passwd , db, port): # 打开数据库连接 db = pymysql.connect(host=str(host), user=str(user),passwd=str(passwd), db=str(db), port=int(port),charset='UTF8') # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行SQL语句 cursor.execute(sql) # 使用 fetchone() 方法获取一条数据 data = cursor.fetchall() #for data in quarydata: #print data return data; # 关闭数据库连接 db.close()
2.读取对应环境的数据库配置信息到基础文件中
def get_db_datasource(temp): if temp == 'test': read_file = open('../../resource/test.robot','r') #读取文件目录 data = read_file.read() #读取文件的内容 wride_file = open('../../resource/basic_file.robot','w') #写文件到basic_file.robot文件中 wride_file.write(data) wride_file.close() if temp == 'citest-exp': read_file = open('../../resource/citest-exp.robot', 'r') # 读取文件目录 data = read_file.read() # 读取文件的内容 wride_file = open('../../resource/basic_file.robot', 'w') # 写文件到basic_file.robot文件中 wride_file.write(data) wride_file.close()
3..py文件查询数据库
#查询 学历管理总条数 def get_education_count_querysql(): education_count_querysql = """ select count(*) from education_info where institution_id = """+str(institution_id)+""" and status_flag=1 """ return education_count_querysql
4.RF调用
pytest.get db datasource ${environment} #读取对应环境(temp环境)的数据库配置信息到 基础文件(basic_file.robot)文件中 ${sql} basic_data.get_education_count_querysql #查询学历管理列表总条数 ${sql_count} pytest.mysql selectab ${sql} ${DBHost} ${DBUser} ${DBPass} ${DBName} ${DBPort}
5.断言
${xpath_count} get matching xpath count ${edulist_xpath}
should be equal as numbers ${sql_count[0][0]} ${xpath_count}
run keyword if ${xpath_count}>0 get list text
6.关键字
*** Keywords *** get list text [Documentation] 获取页面表格中数据元素的文本值 [Arguments] ${listqury} basic_data.get_education_list_querysql ${sql_list} pytest.mysql selectab ${listqury} ${DBHost} ${DBUser} ${DBPass} ${DBName} ${DBPort} ${xpath_count} get matching xpath count ${edulist_xpath} : FOR ${index} IN RANGE 1 ${xpath_count}+1 \ ${eduname_text} get text ${edulist_xpath}[${index}]/td[1] #获取每行学历名称的值 \ should be equal as strings ${eduname_text} ${sql_list[${index}-1][0]} #断言 数据库中的数据和页面获取的数据 相等; \ ${employeeCount_text} get text ${edulist_xpath}[${index}]/td[2] #获取添加了每行学历的员工数 \ should be equal as numbers ${employeeCount_text} ${sql_list[${index}-1][1]} \ ${edustatus_text} get text ${edulist_xpath}[${index}]/td[3] #获取每行状态的值 \ should be equal as strings ${edustatus_text} ${sql_list[${index}-1][2]}