python web自动化测试框架搭建(功能&接口)——功能测试模块

功能测试使用selenium,模块有:

 

1、futil: 公共方法,如元素高亮显示

# coding=utf-8
"""高亮显示元素"""
def highLight(driver,element):
        js = '''
            element = arguments[0];
            element.setAttribute('style','border: 3px solid red;')
            '''
        driver.execute_script(js,element)

 

2、models: 页面控件和页面操作方法封装

页面基类BasePage.py

# coding=utf-8
from selenium import webdriver

TIME_OUT=5 #超时时间

class BasePage(object):
    def __init__(self, driver):
        """初始化浏览器"""
        self.driver = driver
        '''
        if driver_type == "ie":
            try:
                self.driver = webdriver.Ie()
            except Exception,e:
                print e
        elif driver_type == "chrome":
            try:
                self.driver = webdriver.Chrome()
            except Exception,e:
                print e
        else:
            try:
                self.driver = webdriver.Firefox()   
            except Exception,e:
                print e
        '''

    
    def opendriver(self, url):
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(TIME_OUT)
    
    def find_element(self, by, value):
        """查找元素"""
        try:
            return self.driver.find_element(by=by, value=value)
        except Exception, e:
            print e

    def find_elements(self, by, value):
        """查找元素集合"""
        try:
            return self.driver.find_elements(by=by, value=value)
        except Exception, e:
            print e

    def is_element_isexist(self, By, Value):
        """判断元素是否存在"""
        try:
            self.driver.find_element(by=By, value=Value)
            return True
        except Exception, e:
            print e
            return False
        
    def close(self):
        """当前关闭浏览器tab"""
        try:
            self.driver.close()
        except Exception,e:
            print e
            
    def quit(self):
        """退出浏览器进程"""
        try:
            self.driver.quit()
        except Exception,e:
            print e

 

具体页面封装和操作LoginPage.py

# coding=utf-8
from BasePage import BasePage
from selenium.webdriver.common.by import By

class LoginPage(BasePage):
    '''
            登录页面的登录窗口
    '''
    user_name = (By.NAME, "username")
    pass_word = (By.NAME, "password")
    btn_login = (By.CLASS_NAME, "login-btn")
    
    def __init__(self, driver):
        BasePage.__init__(self, driver)
        self.driver = driver
        self.input_username = self.find_element(*self.user_name)
        self.input_password = self.find_element(*self.pass_word)
        self.btn_login = self.find_element(*self.btn_login)
        
    def login(self, username, password):
        self.input_username.send_keys(username)
        self.input_password.send_keys(password)
        self.btn_login.click()   
# coding=utf-8
from BasePage import BasePage
from selenium.webdriver.common.by import By

class Index(BasePage):
    '''
            首页
    '''
    user_menu = (By.ID, "userSetting")
    user_info = (By.XPATH, "//a[@href='user/userInfo']")
    language_span = (By.ID, "changeLanguage")
    chinese_li = (By.XPATH, "//li[@value='zh_cn']")
    english_li = (By.XPATH, "//li[@value='en']")
    
    def __init__(self, driver):
        BasePage.__init__(self, driver)
        self.driver = driver
        self.usermenu = self.find_element(*self.user_menu)
        self.userinfo = self.find_element(*self.user_info)
        self.languagespan = self.find_element(*self.language_span)
        self.chinase = self.find_element(*self.chinese_li)
        self.english = self.find_element(*self.english_li)
        
    def chengeLanguage(self):
        self.languagespan.click()
        self.english.click()
        self.driver.implicitly_wait(5)
        
        

 

2、Report: 测试报告文件夹

3、testcases: 具体测试用例实现

测试用例基类BaseCase.py,负责执行用例运行前和运行结束后操作

# coding=utf-8

import unittest
import Logger
from selenium import webdriver

log = Logger.Loger()
class BaseCaseClass(unittest.TestCase):

    def setUp(self):
#         options = webdriver.ChromeOptions()
#         options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
#         options.add_argument("--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default");
#         self.driver = webdriver.Chrome(chrome_options=options)
        self.driver = webdriver.Ie()
        self.url = "http://baidu.com"
        
    def tearDown(self):
        self.driver.close()
    
    def loginfo(self, msgStr):
        log.info(msgStr)

 

具体用例:

# coding=utf-8

from BaseCase import BaseCaseClass
from functiontest.models.LoginPage import LoginPage
from functiontest.models.IndexPage import Index
from functiontest.futil import Util

class CheckLogin(BaseCaseClass):
    
    def runTest(self):
        '''
        name:测试标题为:XX系统
        verbosity:0
        '''
        self.driver.get(self.url)
        loginPage = LoginPage(self.driver)
        self.assertEqual(loginPage.driver.title,u"XX系统","标题不正确")
        

class CheckIndex(BaseCaseClass):
    
    def runTest(self):
        '''
        name:测试首页存在个人资料选项
        verbosity:0
        '''
        self.driver.get(self.url)
        #self.driver.switch_to_window(self.driver.window_handles[1])
        loginPage = LoginPage(self.driver)
        loginPage.login("admin", "xxx")
        indexPage = Index(self.driver)
        indexPage.chengeLanguage()
        indexPage = Index(self.driver)
        indexPage.usermenu.click()
        Util.highLight(self.driver, indexPage.userinfo)
        self.assertEqual(indexPage.userinfo.text, "Personal Data","标题不正确")  

 

Python web功能&接口自动化测试框架搭建——接口用例实现

Python web功能&接口自动化测试框架搭建——接口公共方法

Python web功能&接口自动化测试框架搭建——接口测试模块

Python web功能&接口自动化测试框架搭建——功能测试模块

Python web功能&接口自动化测试框架搭建——测试用例执行和结果收集

Python web功能&接口自动化测试框架搭建——通用模块

Python web功能&接口自动化测试框架搭建——unittest介绍

Python web功能&接口自动化测试框架搭建——环境搭建

posted @ 2017-07-19 17:00  爹子王  阅读(2191)  评论(0编辑  收藏  举报