#coding=utf-8
import csv #导入scv库,可以读取csv文件
from selenium import webdriver
import unittest
from time import sleep
import time
import os
import sys
import MySQLdb
my_file=''
dr=webdriver.Chrome()
class test(unittest.TestCase):
def test_login(self):
data=csv.reader(open(my_file,'r'))
for user in data:
dr.get('https://passport.cnblogs.com/user/signin')
dr.find_element_by_id('input1').clear()
dr.find_element_by_id('input1').send_keys(user[0])
dr.find_element_by_id('input2').clear()
dr.find_element_by_id('input2').send_keys(user[1])
dr.find_element_by_id('signin').click()
sleep(1)
print ('\n'+'测试项:'+user[2])
dr.get_screenshot_as_file('D:/report/'+user[3]+".jpg")
try:
assert dr.find_element_by_id(user[4]).text
try:
error_message = dr.find_element_by_id(user[4]).text
self.assertEqual(error_message,user[5])
print('提示信息正确!预期值与实际值一致:')
print('预期值:'+user[5])
print('实际值:'+error_message)
except:
print ('提示信息错误!预期值与实际值不符:')
print ('预期值:'+user[5])
print('实际值:'+error_message)
except:
print ('提示信息类型错误,请确认元素名称是否正确!')
def tearDown(self):
dr.quit()
def run():
from HTMLTestRunner import HTMLTestRunner
import time
flag=True
report_title = u'登陆模块测试报告'
desc = u'登陆模块测试报告详情:'
date=time.strftime("%Y%m%d")
time=time.strftime("%Y%m%d%H%M%S")
path= 'D:/report/'+time+"/"
report_path = path+"report.html"
if not os.path.exists(path):
os.makedirs(path)
else:
pass
#定义一个测试容器
testsuite = unittest.TestSuite()
#将测试用例添加到容器
testsuite.addTest(test("test_login"))
fp=open(report_path,'wb')
runner = HTMLTestRunner(stream=fp, title=report_title, description=desc)
try:
runner.run(testsuite)
except Exception:
flag==False
return flag,report_path
report.close()
dr.quit()
if __name__=='__main__':
conn=MySQLdb.connect(host="x.x.x.x",user="xxxx",passwd="xxxx",db="test",charset="utf8")
cursor=conn.cursor()
cursor.execute("select attachment from datapool_manage where id="+sys.argv[-1])
result=cursor.fetchone()
cursor.execute("insert into table sit_script values(26,"yanzhengjiaoben","ceshifuwuqi","logpath","ceshi","ceshi",1,2,0,)")
my_file=result[0]
print(run())