基于Selenium的自动化测试脚本设计实现 (测试MongoDB的分布式文件服务器)

1.项目简介

基于MongoDB的分布式文件服务器支持所有文件的上传、查看、下载、删除功能。

2.项目需求分析

文件类型:所有类型的文件

自动化测试需求:上传、查看、下载、删除功能。

3.自动化测试用例

用例编号 功能点 完成时间 责任人
upload-001 上传文件 2019-07-30 Anan
upload-002 查看文件 2019-07-30 Anan
upload-003 下载文件 2019-07-30 Anan
upload-004 删除文件 2019-07-30 Anan

4.项目界面

5.python自动化脚本的开发

上传文件的自动化测试实现:

testupload.py

# -*- coding: utf-8 -*-
import unittest
from selenium import webdriver

class Testupload(unittest.TestCase):
    #初始化环境
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)   #等待30s
        self.base_url = "http://localhost:8080/"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_upload(self):
        driver = self.driver;
        driver.get(self.base_url+"/")
        driver.find_element_by_name("filename").clear()
        driver.find_element_by_name("filename").send_keys(u"C:\\Users\\lenovo\\Desktop\\元素\\1.doc")
        driver.find_element_by_css_selector("input[type=\"submit\"]").click();
        driver.implicitly_wait(30)
        if(driver.find_element_by_link_text(u"1.png")).is_displayed():
            print"上传成功"
        else:
            print"上传失败"
        driver.quit()
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([],self.verificationErrors)
    if __name__ == "__main__":
        unittest.main()

查看文件的自动化测试实现:

testview.py

# -*- coding: utf-8 -*-
import unittest
from selenium import webdriver

class Testview(unittest.TestCase):
    #初始化环境
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)   #等待30s
        self.base_url = "http://localhost:8080/"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_view(self):
        driver = self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_xpath(u"(//a[contains(text(),'查看')])[2]").click()
        driver.implicitly_wait(30)
        handles = driver.window_handles
        print(handles)
        driver.switch_to.window(handles[1])
        print (driver.current_window_handle)
        print (driver.title)
        print (driver.current_url)
        driver.quit()
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([],self.verificationErrors)
    if __name__ == "__main__":
        unittest.main()

下载文件的自动化测试实现:

testdown.py

# -*- coding: utf-8 -*-
import os
import unittest
from selenium import webdriver

class Testdown(unittest.TestCase):
    #初始化环境
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)   #等待30s
        self.base_url = "http://localhost:8080/"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_down(self):
        driver = self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_link_text(u"1.png").click()
        driver.implicitly_wait(30)
        if os.path.exists(u"C:\\Users\\lenovo\\Desktop\\元素\\1.doc"):
            print "下载成功"
        else:
            print "下载失败"
        driver.quit()
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([],self.verificationErrors)
    if __name__ == "__main__":
        unittest.main()

删除文件的自动化测试实现:

testdelete.py

# -*- coding: utf-8 -*-
import unittest
from selenium import webdriver

class Testdelete(unittest.TestCase):
    #初始化环境
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)   #等待30s
        self.base_url = "http://localhost:8080/"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_delete(self):
        driver = self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_xpath(u"(//a[contains(text(),'删除')])[2]").click()
        driver.implicitly_wait(30)
        print "删除成功"
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([],self.verificationErrors)
    if __name__ == "__main__":
        unittest.main()

批量执行:

runall.py

# -*- coding: utf-8 -*-
import unittest
from FileServer import testupload, testview,testdown,testdelete

#批量执行
def createsuite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(testupload.Testupload))
    suite.addTest(unittest.makeSuite(testview.Testview))
    suite.addTest(unittest.makeSuite(testdown.Testdown))
    suite.addTest(unittest.makeSuite(testdelete.Testdelete))
    return suite

if __name__=="__main__":
    suite = createsuite()
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)

测试报告的输出:

htmlrunall.py

# -*- coding: utf-8 -*-
#手工添加案例到套件
import HTMLTestRunner
import os
import sys
import time
import unittest

from FileServer import testupload, testview, testdown, testdelete


def createsuite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(testupload.Testupload))
    suite.addTest(unittest.makeSuite(testview.Testview))
    suite.addTest(unittest.makeSuite(testdown.Testdown))
    suite.addTest(unittest.makeSuite(testdelete.Testdelete))
    return suite

if __name__=="__main__":
    curpath=sys.path[0]
    #取当前时间
    now=time.strftime("%Y-%m-%d-%H %M %S",time.localtime(time.time()))
    if not os.path.exists(curpath+'/resultreport'):
        os.makedirs(curpath+'/resultreport')
    filename=curpath+'/resultreport/'+now+'resultreport.html'
    with open(filename,'wb') as fp:
        #出html报告
        runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'测试报告',description=u'用例执行情况',verbosity=2)
        suite=createsuite()
        runner.run(suite)

6.测试报告的生成

发布了159 篇原创文章 · 获赞 65 · 访问量 6万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 终极编程指南 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览