对http://www.51zxw.net/我要自学网进行登录测试
driver.py:
from selenium import webdriver
def browser():
#driver=webdriver.Firefox()
driver=webdriver.Chrome()
#driver=webdriver.Ie()
return driver
run_test.py:
from BSTestRunner import BSTestRunner
import unittest
import time
from function import *
report_dir='./test_report'
test_dir='./test_case'
print("start run testcase...")
discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_login.py')
now=time.strftime("%Y-%m-%d %H_%M_%S")
report_name=report_dir+'/'+now+' resule.html'
print('Start write report...')
#运行前记得把BSTestRunner.py 120行‘unicode'换成’str'
with open(report_name,'wb') as f:
runner=BSTestRunner(stream=f,title='Test Report',description='51zxw Login Test')
runner.run(discover)
f.close()
#查找最新的测试报告
print("find latest report...")
latest_report=latest_report(report_dir)
#邮件发送报告
print("send email report...")
send_email(latest_report)
print("test end!")
test_login.py:
from selenium import webdriver
from model import function,myunit
from page_object.LoginPage import *
from time import sleep
import unittest
"""
@测试用例设计
用户名密码正确登录
用户名正确密码错误登录
用户名和密码为空登录
"""
class LoginTest(myunit.StarEnd):
#@unittest.skip('skip this case)
def test_login1_normal(self):
"""用户名密码正确登录"""
print("test_login1_normal start run...")
po=LoginPage(self.driver)
po.Login_action('1*********@qq.com','c*****')
sleep(3)
#断言与截图
self.assertEqual(po.type_loginPass_hint(), '会员中心')
function.insert_img(self.driver,'51zxw_login1_normal.png')
print('test_login1_normal is test end')
def test_login2_passwdError(self):
"""用户名正确密码错误登录"""
print("test_login2_passwdError start run...")
po=LoginPage(self.driver)
po.Login_action('1*********@qq.com','c***')
sleep(3)
#断言与截图
self.assertEqual(po.type_loginFail_hint(), 'username')
function.insert_img(self.driver,'51zxw_login2_passwdError.png')
print('test_login2_passwdError is test end')
def test_login3_empty(self):
"""用户名和密码为空登录"""
print("test_login3_empty start run...")
po=LoginPage(self.driver)
po.Login_action('','')
sleep(3)
#断言与截图
self.assertEqual(po.type_loginFail_hint(), 'username')
function.insert_img(self.driver,'login3_empty.png')
print('test_login3_empty is test end')
function.py:
from selenium import webdriver
import os
import smtplib #发送邮件模块
from email.mime.text import MIMEText #定义邮件内容
from email.header import Header #定义邮件标题
from email.mime.multipart import MIMEMultipart #用于传送附件
def insert_img(driver,filename):
#将路径转换成相对路径
func_path=os.path.dirname(__file__) #获取当前模块目录的路径
base_dir=os.path.dirname(func_path) #获取当前模块目录的上一级目录的路径
base_dir=str(base_dir)
base_dir=base_dir.replace('\\','/')
base=base_dir.split('/Website')[0]
filepath=base+'/Website/test_report/screenshot/'+filename
driver.get_screenshot_as_file(filepath)
def latest_report(report_dir):
#os.listdir()方法用于返回指定文件夹包含文件或文件夹的名字列表
lists=os.listdir(report_dir)
#按照时间顺序对该目录文件夹下面的文件进行排序
lists.sort(key=lambda fn:os.path.getatime(report_dir+'\\'+fn))
#输出最新的报告路径
file=os.path.join(report_dir,lists[-1])
return file
def send_email(latest_report):
with open(latest_report, 'rb') as e:
mail_content=e.read()
e.close()
#发送邮箱服务器
smtpserver='smtp.qq.com'
#发送邮件用户名和密码
user='1**********@qq.com'
password='v************' #授权码作为密码
#发送和接收的邮箱
sender='1*********@qq.com'
#receiver='9*********@qq.com' #收件人是单个
receiver=['9*********[email protected]','1*********@qq.com'] #收件人是多个
#发送邮件主题和内容
subject='Web Selenium AutoTest Report by Flora.Chen'
#content='Web Selenium AutoTest Report by Flora.Chen
'
msgRoot=MIMEText(mail_content,'html','utf-8')
msgRoot['Subject']=Header(subject,'utf-8')
msgRoot['From']=sender
#msg['To']=receiver #收件人是单个
msgRoot['To']=','.join(receiver) #收件人是多个
#SSL协议端口号使用
smtp=smtplib.SMTP_SSL(smtpserver,465)
#向服务器标识用户身份
smtp.helo(smtpserver)
#服务器返回结果确认
smtp.ehlo(smtpserver)
#登录邮箱服务器用户名和密码
smtp.login(user,password)
print('开始发送邮件...')
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
print('邮件发送结束')
myunit.py:
import unittest
from driver import *
class StarEnd(unittest.TestCase):
def setUp(self):
self.driver=browser()
self.driver.implicitly_wait(10)
#self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
BasePage.py:
from time import sleep
class Page():
"""页面基础类"""
# 初始化
def __init__(self,driver):
self.driver=driver
self.base_url='http://localhost'
self.timeout=10
#打开不同的子页面
def _open(self,url):
url_=self.base_url+url
print("Test page is %s" %url_)
self.driver.maximize_window()
self.driver.get(url_)
sleep(2)
assert self.driver.current_url==url_ ,'Did not land on %s' %url_
def open(self):
self._open(self.url)
#元素定位封装
def find_element(self,*loc):
return self.driver.find_element(*loc)
LoginPage.py:
from BasePage import *
from selenium.webdriver.common.by import By
class LoginPage(Page):
"""首页登录页面"""
url='/'
#定位器
username_loc=(By.NAME,'username')
password_loc=(By.NAME,'password')
submit_loc=(By.CLASS_NAME,'lobtn')
#用户名输入框元素
def type_username(self,username):
self.find_element(*self.username_loc).clear()
self.find_element(*self.username_loc).send_keys(username)
# 密码输入框元素
def type_password(self,password):
self.find_element(*self.password_loc).clear()
self.find_element(*self.password_loc).send_keys(password)
# 登录按钮元素
def type_submit(self):
self.find_element(*self.submit_loc).click()
#登录功能模块封装
def Login_action(self,username, password):
"""测试用户名密码是否可以登录"""
self.open()
self.type_username(username)
self.type_password(password)
self.type_submit()
LoginPass_loc=(By.LINK_TEXT,'会员中心')
LoginFail_loc=(By.NAME,'username')
#登录成功的情况
def type_loginPass_hint(self):
return self.find_element(*self.LoginPass_loc).text
#登录失败的情况
def type_loginFail_hint(self):
return self.find_element(*self.LoginFail_loc).text
可以存放测试数据,我暂时没有存放。