python+selenium+unittese 自动化测试框架

Posted yuxiaofeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+selenium+unittese 自动化测试框架相关的知识,希望对你有一定的参考价值。

为一个协同办公系统中的其中的一个功能板块写的一下段自动化测试脚本,采用unittest单元测试框架,代码如下:

#coding=utf-8
from selenium import webdriver
import unittest
import time
from HTMLTestRunner import HTMLTestRunner
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import smtplib  #发送字符串的邮件
from email.mime.text import MIMEText
from email.mime.image import MIMEImage   #处理图片需要 MIMEImage 类
from email.mime.multipart import MIMEMultipart  #处理多种形态的邮件主体我们需要 MIMEMultipart 类
from email.mime.application import MIMEApplication
import os

class ranzhi(unittest.TestCase):
    def setUp(self):
        #准备工作
        self.driver=webdriver.Chrome()
        # self.driver.implicitly_wait(10)
        self.driver.implicitly_wait(10)
        self.baseurl="http://127.0.0.1/ranzhi/sys/user-login-L3JhbnpoaS9zeXMv.html"

    def test_a1_add_role(self): #1.新增角色之正常功能验证
        # 测试然之
        self.driver.get(self.baseurl)
        self.driver.find_element_by_id(account).send_keys(admin)
        self.driver.find_element_by_id(password).send_keys(123456)
        self.driver.find_element_by_id(submit).click()
        time.sleep(1)
        self.driver.get("http://127.0.0.1/ranzhi/sys/admin/")
        time.sleep(3)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(添加成员).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(角色).click()
        self.driver.switch_to.default_content()
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_class_name(icon-plus).click()
        time.sleep(2)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[3]/td[1]/input[1]").send_keys(assistant)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[3]/td[2]/div[1]/input[1]").send_keys(助理)
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)

    def test_a2_add_null1(self):    #2.当"变量名为空"时,只添加值,保存后,添加角色失败
        self.driver.get(self.baseurl)
        self.driver.find_element_by_id(account).send_keys(admin)
        self.driver.find_element_by_id(password).send_keys(123456)
        self.driver.find_element_by_id(submit).click()
        time.sleep(1)
        self.driver.get("http://127.0.0.1/ranzhi/sys/admin/")
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(添加成员).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(角色).click()
        self.driver.switch_to.default_content()
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        time.sleep(3)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[18]/td[3]/a[1]/i[1]").click()
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[1]/input[1]").send_keys()
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[2]/div[1]/input[1]").send_keys(助理)
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)

    def test_a3_add_null2(self):    #3.当"自定义显示值"为空时,只添加变量名,保存后,添加角色失败
        self.driver.get(self.baseurl)
        self.driver.find_element_by_id(account).send_keys(admin)
        self.driver.find_element_by_id(password).send_keys(123456)
        self.driver.find_element_by_id(submit).click()
        time.sleep(1)
        self.driver.get("http://127.0.0.1/ranzhi/sys/admin/")
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(添加成员).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(角色).click()
        self.driver.switch_to.default_content()
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        time.sleep(3)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[18]/td[3]/a[1]/i[1]").click()
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[1]/input[1]").send_keys(assistant)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[2]/div[1]/input[1]").send_keys()
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)

    def test_a4_editor_role(self):    #4.编辑修改已经存在的角色,验证保存后是否可以修改成功
        self.driver.get(self.baseurl)
        self.driver.find_element_by_id(account).send_keys(admin)
        self.driver.find_element_by_id(password).send_keys(123456)
        self.driver.find_element_by_id(submit).click()
        time.sleep(1)
        self.driver.get("http://127.0.0.1/ranzhi/sys/admin/")
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(添加成员).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(角色).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        time.sleep(1)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[17]/td[2]/div[1]/input[1]").clear()
        time.sleep(1)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[17]/td[2]/div[1]/input[1]").click()
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[17]/td[2]/div[1]/input[1]").send_keys(客户经理)
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)

    def test_a5_delete_role(self):   #5.增加一个新的角色,然后删除该角色,验证保存后是否可以删除成功
        self.driver.get(self.baseurl)
        self.driver.find_element_by_id(account).send_keys(admin)
        self.driver.find_element_by_id(password).send_keys(123456)
        self.driver.find_element_by_id(submit).click()
        time.sleep(1)
        self.driver.get("http://127.0.0.1/ranzhi/sys/admin/")
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(添加成员).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(角色).click()
        self.driver.switch_to.default_content()
        time.sleep(3)
        self.driver.switch_to.frame(iframe-superadmin)
        time.sleep(1)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[18]/td[3]/a[1]").click()
        time.sleep(3)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[1]/input[1]").send_keys(administrator)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[2]/div[1]/input[1]").send_keys(管理员)
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[3]/a[2]/i[1]").click()
        time.sleep(2)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)

    def test_a6_default_Settings(self): #6.已经添加的角色,通过点击恢复默认按钮,验证是否可以还原到初始状态
        self.driver.get(self.baseurl)
        self.driver.find_element_by_id(account).send_keys(admin)
        self.driver.find_element_by_id(password).send_keys(123456)
        self.driver.find_element_by_id(submit).click()
        time.sleep(1)
        self.driver.get("http://127.0.0.1/ranzhi/sys/admin/")
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(添加成员).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        self.driver.find_element_by_link_text(角色).click()
        self.driver.switch_to.default_content()
        time.sleep(1)
        self.driver.switch_to.frame(iframe-superadmin)
        time.sleep(1)
        # 以下为添加角色信息
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[18]/td[3]/a[1]/i[1]").click()
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[1]/input[1]").send_keys(administrator)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[2]/div[1]/input[1]").send_keys(管理员)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[19]/td[3]/a[1]/i[1]").click()
        time.sleep(2)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[20]/td[1]/input[1]").send_keys(assistant)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[20]/td[2]/div[1]/input[1]").send_keys(助理)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[20]/td[3]/a[1]/i[1]").click()
        time.sleep(2)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[21]/td[1]/input[1]").send_keys(sales)
        self.driver.find_element_by_xpath             ("//table[@class=‘table table-condensed‘]/tbody/tr[21]/td[2]/div[1]/input[1]").send_keys(销售)
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(3)
        # 恢复默认功能验证
        self.driver.find_element_by_link_text(恢复默认).click()
        time.sleep(3)
        WebDriverWait(self.driver, 20).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alert.text
        alert.accept()  # 弹窗处理
        time.sleep(1)
        self.driver.find_element_by_id(submit).click()
        time.sleep(5)


    def tearDown(self):
        #释放资源
        self.driver.quit()
        os.system("taskkill -f -im chromedriver.exe")
if __name__==__main__:
    testunit=unittest.TestSuite()
    testunit.addTest(ranzhi(test_a1_add_role))
    testunit.addTest(ranzhi(test_a2_add_null1))
    testunit.addTest(ranzhi(test_a3_add_null2))
    testunit.addTest(ranzhi(test_a4_editor_role))
    testunit.addTest(ranzhi(test_a5_delete_role))
    testunit.addTest(ranzhi(test_a6_default_Settings))
    fp=open(./result.html,wb)
    runner=HTMLTestRunner(stream=fp,title=小余的然之测试报告,description=然之用例执行情况)
    runner.run(testunit)
    fp.close()

if __name__ == __main__:
    fromaddr = ‘‘   #邮件发送方邮箱地址
    password = ‘‘   #发送发邮箱密码
    toaddrs = [‘‘]  #收件人地址

    content = hello, this is email content,Please refer to the attachment.
    textApart = MIMEText(content)
    zipFile = D:\ranzhi.zip   #添加文件路径名
    zipApart = MIMEApplication(open(zipFile, rb).read())
    zipApart.add_header(Content-Disposition, attachment, filename=zipFile)
    m = MIMEMultipart()
    m.attach(textApart)
    m.attach(zipApart)
    m[Subject] = ranzhi测试报告

    try:
        server = smtplib.SMTP(smtp.qq.com)    #这段代码支持QQ邮箱发送邮件
        server.login(fromaddr, password)
        server.sendmail(fromaddr, toaddrs, m.as_string())
        print(success)
        server.quit()
    except smtplib.SMTPException as e:
        print(error:, e)  # 打印错误

 

以上是关于python+selenium+unittese 自动化测试框架的主要内容,如果未能解决你的问题,请参考以下文章

Selenium02篇python+selenium实现Web自动化:鼠标操作和键盘操作!

selenium ide和selenium python的区别

python+selenium环境安装

python+selenium十:selenium的二次封装

Selenium 之 Mac 环境下 Python 安装 selenium 踩坑记录

selenium python怎么封装方法