代碼的目錄結(jié)構(gòu)如下:
目錄結(jié)構(gòu).jpg
測試的對象是本地安裝的禪道系統(tǒng):
禪道.jpg
測試的結(jié)果如下:
郵件.jpg
報(bào)告.jpg
以下是測試代碼:
driver.py
from selenium import webdriver
'''啟動(dòng)不同的瀏覽器啟動(dòng)'''
def browser():
driver=webdriver.Chrome("D:\Google\Chrome\Application\chromedriver.exe")
# driver=webdriver.Firefox()
# driver=webdriver.ie()
# driver.get('http://www.baidu.com')
return driver
if __name__ == '__main__':
browser()
function,py
'''工具方法模塊(截圖猜极,查找最新報(bào)告兄淫、郵件發(fā)送)'''
import os
import time
from selenium import webdriver
import smtplib #發(fā)送郵件模塊
from email.mime.text import MIMEText #定義郵件內(nèi)容
from email.header import Header #定義郵件標(biāo)題
#截圖方法
def insert_img(driver,filename):
# 獲取當(dāng)前模塊所在路徑
print(__file__)
func_path=os.path.dirname(__file__)
print('func_path is %s'%func_path)
#獲取上一級目錄
base_dir=os.path.dirname(func_path)
print("base_dir is %s" %base_dir)
base_dir=str(base_dir)
print(base_dir)
# 獲取項(xiàng)目文件的根目錄路徑
base=base_dir.split('/website')[0]
print(base)
# 指定截圖存放路徑
filepath=base+'/website/test_report/report/'+filename
print(filepath)
driver.get_screenshot_as_file(filepath)
def latest_report(report_dir):
lists=os.listdir(report_dir)
print(lists)
lists.sort()
print('file is :%s'%lists[-1])
file=os.path.join(report_dir,lists[-1])
fi=str(file)
print('latest file is: %s'%fi)
return file
#將測試報(bào)告發(fā)送到郵件
def send_email(latest_report):
print('func_py latest report is %s'%latest_report)
f=open(latest_report,'rb')
mail_content=f.read()
# 發(fā)送郵箱服務(wù)器
smtpserver = 'smtp.163.com
# 發(fā)送郵箱用戶名密碼
user = '*********@163.com'
password = '********'
# 發(fā)送和接收郵箱
sender = '*****@163.com'
receives = [ '****@qq.com']
# 發(fā)送郵件主題和內(nèi)容
subject = '自動(dòng)化測試報(bào)告郵箱發(fā)送'
msg = MIMEText(mail_content,'html','utf-8')
msg['Subject'] = Header(subject,'utf-8')
msg['From'] = sender
msg['To'] = ','.join(receives)
smtp = smtplib.SMTP_SSL(smtpserver, 465)
smtp.helo(smtpserver)
smtp.ehlo(smtpserver)
smtp.login(user, password)
print("Start send email..." )
# smtp.sendmail(sender,receives,msgRoot.as_string())
smtp.sendmail(sender, receives, msg.as_string())
smtp.quit()
print("Send email end!")
if __name__ == '__main__':
# send_multi_email()
driver = webdriver.Chrome("D:\Google\Chrome\Application\chromedriver.exe")
driver.get("http://www.sogou.com")
time.sleep(2)
insert_img(driver, "sogou.png")
driver.close()
myuint.py
import unittest
from driver import driver
'''用例運(yùn)行前后的環(huán)境準(zhǔn)備工作'''
class StartEnd(unittest.TestCase):
def setUp(self):
self.driver=driver.browser()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def tearDown(self):
self.driver.close()
BasePage.py
'''頁面基類'''
from time import sleep
class Page():
def __init__(self,driver):
self.driver=driver
self.base_url='http://127.0.0.1:8080/'
self.timeout=20
def _open(self,url):
url_=self.base_url+url
print('url 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)
login_page.py
from BasePage import *
from selenium import webdriver
from selenium.webdriver.common.by import By
class Login_Page(Page):
'''登錄頁面'''
url='zentao/user-login.html'
# 定位器——對相關(guān)元素進(jìn)行定位
username_loc=(By.ID,'account')
password_loc=(By.NAME,'password')
submit_loc=(By.ID,'submit')
def type_username(self,uname):
self.find_element(*self.username_loc).clear()
self.find_element(*self.username_loc).send_keys(uname)
def type_password(self,pw):
self.find_element(*self.password_loc).clear()
self.find_element(*self.password_loc).send_keys(pw)
def click_submit(self):
self.find_element(*self.submit_loc).click()
def login_action(self,uname,pw):
self.open()
self.type_username(uname)
self.type_password(pw)
self.click_submit()
Login_pass_loc=(By.ID,'companyname')
Login_fail_loc=(By.LINK_TEXT,'忘記密碼')
def type_login_pass_hint(self):
return self.find_element(*self.Login_pass_loc).text
def type_login_fail_hint(self):
return self.find_element(*self.Login_fail_loc).text
# if __name__=='__main__':
# driver=webdriver.Chrome("D:\Google\Chrome\Application\chromedriver.exe")
# po=Login_Page(driver)
# po.login_action('admin','Aa1234')
test_login.py
'''
? 用戶名密碼正確點(diǎn)擊登錄
? 用戶名正確,密碼錯(cuò)誤點(diǎn)擊登錄
? 用戶名和密碼為空點(diǎn)擊登錄
'''
import unittest
from website.testcase.model import function,myuinit
from website.testcase.page_object.login_page import *
from time import sleep
class LoginTest(myuinit.StartEnd):
def test_login1_normal(self):
'''用戶名悬嗓、密碼正確'''
print("test_login1_normal is start run...")
po=Login_Page(self.driver)
po.login_action('admin','Aa1234')
sleep(3)
self.assertEqual(po.type_login_pass_hint(),'易軟天創(chuàng)項(xiàng)目管理系統(tǒng)')
function.insert_img(self.driver,'login_success.png')
print("test_login1_normal is test end!")
@unittest.skip('skp this case')
def test_login2_password_error(self):
'''用戶名正確,密碼錯(cuò)誤'''
print("test_login1_password error is start run...")
po=Login_Page(self.driver)
po.login_action('admin','Aa123456')
sleep(3)
self.assertEqual(po.type_login_pass_hint(),'易軟天創(chuàng)項(xiàng)目管理系統(tǒng)')
function.insert_img(self.driver,'login_success.png')
print("test_login1_normal is test end!")
@unittest.skip('skip this case')
def test_login3_empty(self):
'''用戶名传货、密碼為空'''
print("test_login1_normal is start run...")
po=Login_Page(self.driver)
po.login_action('admin','Aa1234')
sleep(3)
self.assertEqual(po.type_login_pass_hint(),'易軟天創(chuàng)項(xiàng)目管理系統(tǒng)')
function.insert_img(self.driver,'login_success.png')
print("test_login1_normal is test end!")
if __name__=='__main__':
unittest.main()