郑大+元素定位+unittest+邮件发送

2018-06-12  本文已影响0人  往你头上敲三下

如图:点击临床医学系之后,再回到第一个页面点击医学类院系下的更多。

测试类:

from seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

import time

import unittest

class ZhengdaFunc(unittest.TestCase):

    @classmethod

    def setUpClass(cls):

        cls.driver = webdriver.Chrome()

        cls.driver.get('http://www.zzu.edu.cn/')

        cls.driver.maximize_window()

def testcase1(self):

# self.moveHouse()

        self.driver.switch_to.frame('zzu_top_6')

        ele =self.driver.find_element_by_xpath("//*[text()='院系专业']")

         ActionChains(self.driver).move_to_element(ele).perform()

        time.sleep(2)

        self.driver.find_element_by_xpath("//*[text()='临床医学系']").click()

        self.driver.switch_to.window(self.driver.window_handles[2])

        title =self.driver.title

        self.assertEqual(title,'临床医学系')

def testcase2(self):

        self.driver.switch_to.window(self.driver.window_handles[0])

        time.sleep(2)

        # self.moveHouse()

        self.driver.switch_to.frame('zzu_top_6')

        ele =self.driver.find_element_by_xpath("//*[text()='院系专业']")

        ActionChains(self.driver).move_to_element(ele).perform()

        time.sleep(2)

        self.driver.find_element_by_xpath("//*[text()='院系专业']/../div/div/div[3]//*[text()='更多>>']").click()

        self.driver.switch_to.window(self.driver.window_handles[3])

        title =self.driver.title

        self.assertEqual(title,'郑州大学.医学类院系')

执行类(suite):

import unittest

import time

from zhengzhouimport ZhengdaFunc

from HTMLTestRunner_PY3import HTMLTestRunner

if __name__ =='__main__':

    suit = unittest.TestLoader().loadTestsFromTestCase(ZhengdaFunc)

    time = time.strftime('%Y-%m-%d-%H-%M',time.localtime())

    dir ='D:\\zhangcq\\0611\\'+time+'report.html'

    file =open(dir, 'wb')

    runner = HTMLTestRunner(stream=file, title='测试报告', description='郑州大学')

    runner.run(suit)

邮件发送:

from email.mime.textimport MIMEText

import smtplib

from email.headerimport Header

from email.mime.multipartimport MIMEMultipart

def sendmain():

    smtserver ='smtp.yyy.cn'

    sender ='xxx@yyy.cn'

    password ='xxxxxxxx'

    receiver ='mmm@126.com'

    message = MIMEMultipart()

    message.attach(MIMEText('张超群的自动化测试报告','plain','utf-8'))

    message['from'] = sender

    message['to'] = receiver

    message['subject'] = Header('张超群的自动化测试报告','utf-8')

    fujian = MIMEText(open(r'D:\zhangcq\0611\2018-06-12-14-03report.html',encoding='utf-8').read())

    fujian['content-disposition'] ='filename="report.html"'

    message.attach(fujian)

    server = smtplib.SMTP(smtserver, 25)

    server.login(sender,password)

    server.sendmail(sender,receiver,message.as_string())

    server.quit()

sendmain()

测试结果:

测试报告:

第一个测试类中我们写的两个测试用例,可以看到两个用例中的代码重复率还是挺高的。重复的代码我们可以写到函数中,测试方法调用该函数即可。优化后的代码如下:

上一篇下一篇

猜你喜欢

热点阅读