selenium2+python3自动化2-csdn写博客(邮件发送附件测试报告)

一、项目结构


二、脚本

1、common.py

class Login():
    def user_login(self,driver,account,password):
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys(account)
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys(password)        
        driver.find_element_by_class_name("logging").click()

2、test_publish_blog.py

from selenium import webdriver
from common.common import Login
from time import sleep,ctime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import os
import unittest


def setUpModule():
    print("publish_blog test module start...")
def tearDownModule():
    print("publish_blog test module end...")


class MyTest(unittest.TestCase):
    '''优快云写博客测试'''
    @classmethod
    def setUpClass(cls):
        print("publish_blog test class start...")
    @classmethod
    def tearDownClass(cls):
        print("publish_blog test class end...")
    
    def setUp(self):   
        print("publish_blog test case start...")     
        #配置文件地址
        profile_directory = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\xva67whn.default'
        #加载配置
        profile = webdriver.FirefoxProfile(profile_directory)
        #启动浏览器配置
        self.driver = webdriver.Firefox(profile) 
        self.driver.maximize_window() 
        self.driver.implicitly_wait(10)
        self.base_url = "http://www.youkuaiyun.com/"   
    #@unittest.skip("直接跳过测试")
    def test_publish_blog(self):
        print('test case test_publish_blog')
        '''写博客'''
        driver = self.driver
        driver.get(self.base_url)
        print(driver.title)
        print(driver.current_url)
        #显式等待 
        login = WebDriverWait(driver,2,0.5).until(EC.presence_of_element_located((By.LINK_TEXT,"登录")))
        login.click()
        print(driver.title)
        print(driver.current_url)
        Login().user_login(driver, "", "")
        sleep(1)
        print(driver.title)
        print(driver.current_url)
        windows_first = driver.current_window_handle 
        print(windows_first)
        above = driver.find_element_by_xpath("html/body/div[2]/div/div[2]/ul/li[5]/div")
        ActionChains(driver).move_to_element(above).perform()
        driver.find_element_by_xpath("html/body/div[2]/div/div[2]/ul/li[5]/div/div/dl/dt[4]/a/span").click()
        sleep(1)
        all_handles = driver.window_handles   
        print(all_handles)
        for handle in all_handles:
            if handle!= windows_first:
                driver.switch_to.window(handle)
                print('写博客页面')
                tishi = driver.find_element_by_xpath("html/body/div[2]/div[6]/p[8]").text
                print(tishi)
                print(ctime())
                driver.find_element_by_id("selType").click()
                driver.find_element_by_xpath("//*[@id='selType']/option[2]").click()
                driver.find_element_by_id("txtTitle").send_keys("title")
                f = driver.find_element_by_id("xhe0_iframe") 
                driver.switch_to.frame(f)
                driver.find_element_by_xpath("/html/body").send_keys("content")
                driver.switch_to.parent_frame()
                f2 = driver.find_element_by_id("frm_img_2")
                driver.switch_to.frame(f2)
                driver.find_element_by_id("file1").send_keys("D:\\20170617170031.png") 
        #         driver.find_element_by_id("file1").click()
        #         os.system("D:\\upfile.exe")     
                driver.switch_to.parent_frame()
                driver.find_element_by_xpath("//*[@id='wrap']/fieldset/table/tbody/tr[2]/td/input[1]").click()
                sleep(1)
                driver.find_element_by_xpath("//*[@id='ol_img']/li/a").click()
                driver.find_element_by_id("chk_tag_2").click()            
                driver.find_element_by_id("btnDraft").click()      
                print(ctime())
        for handle in all_handles:
            if handle==windows_first:
                driver.switch_to.window(handle)
                print("回到首页")
                user = driver.find_element_by_class_name("phrf_name").text
                print(user) 
                sleep(3)
                
    def tearDown(self):
        print("publish_blog test case end...")
        self.driver.quit()    
if __name__ == "__main__":
    unittest.main()


3、test_search.py

from selenium import webdriver
from common.common import Login
from selenium.webdriver.common.keys import Keys
import unittest
from time import sleep


def setUpModule():
    print("search test module start...")
def tearDownModule():
    print("search test module end...")
    
class MyTest(unittest.TestCase):
    '''优快云搜索测试'''
    @classmethod
    def setUpClass(cls):
        print("search test class start...")
    @classmethod
    def tearDownClass(cls):
        print("search test class end...")
        
    def setUp(self):   
        print("search test case start...")
        #配置文件地址
        profile_directory = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\xva67whn.default'
        #加载配置
        profile = webdriver.FirefoxProfile(profile_directory)
        #启动浏览器配置
        self.driver = webdriver.Firefox(profile) 
        self.driver.maximize_window() 
        self.driver.implicitly_wait(10)
        self.base_url = "http://www.youkuaiyun.com/"
        
    def test_search(self):
        print('test case test_publish_blog')
        '''搜索关键字'''
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_link_text("登录").click()
        Login().user_login(driver, "", "")
        #driver.find_element_by_id("searchKey").send_keys("python")
        #driver.find_element_by_id("searchBtn").click()
        search = driver.find_element_by_id("searchKey")
        search.send_keys("python")
        search.send_keys(Keys.ENTER)
        sleep(3)
        
    def tearDown(self):
        print("search test case end...")
        self.driver.quit()
    
if __name__ =="__main__":
    unittest.main()

4、runtest.py

import unittest
from HTMLTestRunner import HTMLTestRunner
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import os
from email.mime.multipart import MIMEMultipart


#查找测试报告目录,找到最新生成的测试报告文件
def new_report(testreport):  
    lists = os.listdir(testreport)
    lists.sort(key=lambda fn: os.path.getatime(testreport+"\\"+fn))
    file_new = os.path.join(testreport, lists[-1])
    print(file_new)
    return file_new


def send_mail(file_new):
    f = open(file_new,"rb")
    mail_body = f.read()
    f.close()   
    
    stmpserver = 'smtp.163.com'
    user = ""
    password = ""   
    subject = '优快云自动化测试报告'    
    
    # 构造MIMEMultipart对象做为根容器  
    msgRoot = MIMEMultipart()
    
    text_msg = MIMEText(mail_body,'html','utf-8')
    msgRoot.attach(text_msg)    
    
    file_msg = MIMEText(mail_body, 'base64', 'utf-8')
    file_msg["Content-Type"] = 'application/octet-stream'
    ## 设置附件头
    basename = os.path.basename(file_new)
    print(basename)   
    file_msg["Content-Disposition"] = 'attachment; filename="'+ basename +'"'
    msgRoot.attach(file_msg)  
    # 设置根容器属性
    msgRoot['Subject'] = Header(subject,'utf-8')
    msgRoot['From'] = ''    
    msgRoot['To']= ''      
   
    #连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(stmpserver)
    smtp.login(user, password)
    smtp.sendmail(msgRoot['From'], msgRoot['To'], msgRoot.as_string())
    smtp.quit() 
    print('email has send out!') 


if __name__ =="__main__":
    test_dir = "./"
    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    filename = './report/' + now + 'result.html'
    fp = open(filename,'wb')
    runner = HTMLTestRunner(stream=fp,title='优快云搜索、写博客测试报告',description='用例执行情况:')
    runner.run(discover)
    fp.close()
    newreport = new_report('./report')#查找新生成的报告
    send_mail(newreport)





 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值