之前介绍了在python中怎么对sqlite3数据库进行操作,今天再详细的介绍,怎么把自动化中使用到的数据存储在sqlite3数据库的文件中,然后在自动化中引用。下面详细的介绍,把页面的元素,输入的数据,以及系统返回的错误信息存储在数据库,然后从数据库中读取,来引入到实际的自动化项目中,就已百度登录为实例,创建表element.db,字段见如下的截图:
存储的测试数据为:
读取这些数据的方法为:
defsqliteData(value1,value2):
rows=[]
try:
conn=sqlite3.connect('D:\sqlite.db')
sql="select * from element;"
cur=conn.cursor()
cur.execute(sql)
for db in cur.fetchall():
rows.append(db)
print rows[value1][value2]
except:
print u'Database operation failed'
finally:
cur.close()
conn.close()
实现的自动化测试脚本见如下的代码:
#!/usr/bin/envpython
#coding:utf-8
fromselenium import webdriver
import selenium.webdriver.common.by as by
import DataDriven as data
import time,unittest
deferrorText(driver):
time.sleep(2)
returndriver.find_element_by_xpath(".//*[@id='TANGRAM__PSP_8__error']").text
deflogin(driver,username,password):
driver.find_element_by_id('u1').find_element_by_class_name('lb').click()
time.sleep(2)
driver.find_element_by_id('TANGRAM__PSP_8__userName').send_keys(username)
time.sleep(2)
driver.find_element_by_id('TANGRAM__PSP_8__password').send_keys(password)
time.sleep(2)
driver.find_element_by_id('TANGRAM__PSP_8__submit').click()
classbaiduTest(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get('http://www.baidu.com')
def test_001(self):
'''验证只输入用户名返回的错误信息'''
login(self.driver,data.sqliteData(1,1),data.sqliteData(1,2))
self.assertEqual(data.sqliteData(1,3),errorText(self.driver))
def test_002(self):
'''验证用户名密码错误'''
login(self.driver,data.sqliteData(2,1),data.sqliteData(2,2))
self.assertEqual(data.sqliteData(2,3),errorText(self.driver))
def test_003(self):
login(self.driver,data.sqliteData(3,1),data.sqliteData(3,2))
self.assertEqual(data.sqliteData(3,3),errorText(self.driver))
def tearDown(self):
self.driver.quit()
if__name__=='__main__':
unittest.main(verbosity=2)