下面看一下简单的核对数据的脚本
目的
将页面上每次测试都需要核对的数据和数据库中查询的数据核对让代码来解放我们的双手
思路
第一个:通过调用control层的接口,登录,查询数据然后和数据库的数据对比
第二个:selenium抓取页面的数据和数据库中的数据对比(java,Python均可)
下面让我们看一下第二种方法是如何做的
先来看一下怎么连接数据库
def getdatabase(sql):
# 链接数据库
conn = MySQLdb.connect(host='192.168.8.63', user='root', passwd='E4yun.cn123', db='eayuncloud', port=3306)
cur = conn.cursor()
try:
# 执行sql语句
# cur.execute("SELECT mon_money FROM money_account WHERE mon_cusid='"+cusid+"' ")
cur.execute(sql)
results = cur.fetchall()
for row in results:
mon_money = row[0]
except:
print "Error: unable to fecth data"
# 关闭连接
cur.close()
conn.close()
# 返回结果值
return mon_money
那怎么进行业务设计呢????
初始化数据
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window() # 将浏览器最大化
self.driver.implicitly_wait(30)
self.base_url = "http://192.168.8.65:7070/"
self.verificationErrors = []
self.accept_next_alert = True
查询数据库中的数据和页面抓起的数据对比:def test_quit(self):
driver = self.driver
driver.get(self.base_url + "/#/login")
#登录
login.login_ecmc(driver)
driver.find_element_by_link_text(u"收入统计").click()
page_money = driver.find_element_by_xpath("html/body/div[1]/div[3]/div/div/div/table/tbody/tr[1]/td[3]/table/tbody/tr[2]/td[3]/p/span").text
page_money_to = page_money.replace('¥','')
#print("page_money",page_money_to)
time.sleep(10)
sql = "SELECT SUM(mon_money) FROM money_record where income_type = '1' AND oper_type='4'"
pay_count = conn_database.getdatabase(sql)
dec = decimal.Decimal(pay_count)
pay_count_to = str(dec)[:-1]
#print("pay_count",pay_count)
#pay_count_to = conn_database.cut(pay_count, 2)
#print("pay_prepaid_to",pay_count_to)
flag = u"核对总览收入统计中的【ECMC实际充值】失败!!!"
if cmp(str(page_money_to),str(pay_count_to)) == 0:
flag = u"核对总览收入统计中的【ECMC实际充值】成功"
return flag
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as e:
return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally:
self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
名词解释
Setup:初始化一些参数和信息,在测试用例执行前,这个方法中的函数将被先执行,这里将浏览器的调用和URL的访问放到初始化函数中
self.verificationErrors = []:脚本运行时,错误的信息将被打印到这个列表中
self.accept_next_alert = True:是否继续接受下一个警告
implicitly_wait(30)隐式等待:当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找元素,默认的时间是0 7;一旦设置了隐式等待,则它存在整个 WebDriver 对象实例的声明周期中,隐式的等到会让一个正常响应的应用的测试变慢, ;它将会在寻找每个元素的时候都进行等待,这样会增加整个测试执行的时间。
Test_quit:业务处理的方法,里面处理各种业务逻,例如,获取页面的数据,获取数据库的数据,然后核对
getdatabase:链接数据库并返回数据库查询的结果
cursor用来执行命令的方法:
1. callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数
- execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数
3.executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数,例如:使用executemany方法来批量的插入数据 - nextset(self):移动到下一个结果集
cursor用来接收返回值的方法:
1.fetchall(self):接收全部的返回结果行.
2.fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据. - fetchone(self):返回一条结果行.
4.scroll(self, value, mode='relative'):移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果mode='absolute',则表示从结果集的第一 行移动value条.
需要分别的关闭指针对象和连接对象.他们有名字相同的方法 cursor.close() conn.close()
is_element_present:用来查找页面元素是否存在
is_alert_present:判断alert是否存在,对弹框异常的处理
close_alert_and_get_its_text:关闭警告对话框和对文本框的处理
tearDown:此方法在每个测试方法执行后调用,这个地方做所有清理工作,如退出浏览器等
if name == "main":此函数相当于java中的main方法,是整个逻辑的入口,如果没有这个函数所有的方法都不会被执行
Sql:如果有些逻辑可以放在sql中处理,尽量在sql中,这样子简化维护代码的成本,例如:
SELECT mon_money FROM money_account WHERE mon_cusid='ff80808159b61b460159b63be68e0033'此处的cusid需要单独维护
SELECT mon_money FROM money_account m,sys_selfcustomer s where m.mon_cusid=s.cus_id and s.cus_number='ladAdmin'此处就只需要维护用户名,反正每换个环境用户名和密码都会维护一下,这就减少了需要维护的参数
网友评论