基于Selenium自动化测试框架的设计与实现

测试项目:基于MongoDB的文件服务器

功能介绍:

基于MongoDB的文件服务器支技所有文件的上传、下载、查看、删除。

项目技术:

  • Servlet
  • Freemarker模板引擎
  • Mongodb数据库

项目界面:

基于Selenium自动化测试框架的设计与实现_第1张图片

 

Selenium框架自动化脚本

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re,os

class Test(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "http://192.168.24.131:9999/"
        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("C:\\Users\\10320\\Pictures\\006.jpg")
        driver.find_element_by_css_selector("input[type=\"submit\"]").click()
        driver.implicitly_wait(20)
        if driver.find_element_by_link_text("006.jpg").is_displayed():
            print("上传成功")
        else:
            print("上传失败")
        driver.quit()
    #查看文件
    def test_view(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_xpath(u"(//a[contains(text(),'查看')])[3]").click()
        driver.implicitly_wait(20)
        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 test_download(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_link_text(u"007.jpg").click()
        driver.implicitly_wait(20);
        if os.path.exists(u"C:\\Users\\10320\\Pictures\\008.jpg"):
            print"下载成功"
        else:
            print"下载失败"
        driver.quit()
    #删除文件
    def test_delete(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_xpath(u"(//a[contains(text(),'删除')])[3]").click()
        driver.implicitly_wait(20);
        print"删除成功"
        driver.quit();
    #判断element是否存在,可删除
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    #判断alert是否存在,可删除
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    #关闭alert,可删除
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    #清除环境
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
#执行用例
if __name__ == "__main__":
    unittest.main()

 输出测试报告脚本

# -*- coding: utf-8 -*-
import unittest,csv
import os,sys
import time
import HTMLTestRunner
#手工添加案例到套件,
def createsuite():
    discover=unittest.defaultTestLoader.discover('../Test',pattern='TestPython.py',top_level_dir=None)
    print discover
    return discover
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:
    #出测试报告
 runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'测试报告',description=u'用例执行情况',verbosity=2)
 suite=createsuite()
 runner.run(suite)

生成的测试报告 

 基于Selenium自动化测试框架的设计与实现_第2张图片

你可能感兴趣的:(软件测试)