框架构建(4):基于UnitTest测试框架,编写测试用例

下面正式编写测试用例。先看一下基于UnitTest测试用例的结构:

import unittest
class TestViewPage(unittest.TestCase):
    def setUp(self):
        #运行测试用例前的初始化操作

    def test_xx(self):
        #测试用例

    def tearDown(self):
        #测试执行后,清空测试环境

分析:

在执行web测试时,我们都需要使用浏览器打开一个url,如果需要登陆的话,还要登陆相应角色的用户后,才能正常开始执行测试。因此,setUp部分,我们需要填写打开浏览器,打开指定url,以及登陆用户名密码。而tearDown部分一般则是关闭浏览器操作。

那么现在我们可以基于上一节写的login_page.py 写一个setup.py, 将打开一个url和登陆操作整合在setup.py中。

//src/common/setup.py

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import sys
sys.path.append("../..")
from src.pages.login_page import LoginPage
from config.globalparameter import web_url
from time import sleep

def login(username, password):
    driver = webdriver.Firefox()
    url = web_url
    login_page = LoginPage(driver, url)
    try:
        login_page.open_page()
    except:
        raise e
        
    if EC.title_contains("Sign in")(driver):
        print "Now start to login the web."
  
    login_page.input_username(username)
    login_page.input_password(password)
    login_page.click_login()
    
    ret = login_page.check_username_shown_correct(username)
    if ret == True:
        log.info("Login successfully!")   
    else:
        raise Exception("Failed to login.")
    return driver

if __name__ == '__main__':
    driver = login("username_wang", "passwd_wang") 
    # open the url defined in config/globalparameter.py and use the given username and passwd to login

 

这样就可以把setup.py中的login方法应用到测试用例中了。现在假设我们需要写一个点击菜单(测试管理-》浏览测试报告)并打开相应页面的测试用例, 首先我们创建一个\src\pages\menu.py 来定义所有页面元素和页面操作

//src/pages/menu.py

from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import sys
sys.path.append("../..")
from src.common.BasePage import BasePage

class Menu(BasePage):
    #locator
    # The first level menu TestManagement
    menu_Test_Management_loc = (By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a')

    # The second level menu View Report
    menu_View_Report_loc = (By.LINK_TEXT, 'View Report')  

    # The ul tag is used to confirm if the report tree is loaded
    ul_report_tree_loc = (By.XPATH, '//ul[@id="report_tree"]')

    # The header text is used to confirm if the report tree is loaded correctly
    header_test_report_loc = (By.XPATH, '/html/body/div[2]/div[1]/div[1]')
    
    def __init__(self, selenium_driver):
        self.driver = selenium_driver

    def move_on_menu_Test_Management(self):
        '''
        move the mouse on menu
        '''
        element = self.find_element(*self.menu_Test_Management_loc)
        ActionChains(self.driver).move_to_element(element).perform()

    def click_menu_View_Report(self):
        self.find_element(*self.menu_View_Report_loc).click()


    def check_report_page_loaded(self, text):
        """
        check if the View Report page is loaded.
        if the given text is included in the given area, return True, otherwise return False
        """
        self.find_element(*self.ul_report_tree_loc)
        return self.check_page_loaded_correct(text, *self.header_test_report_loc)

下一步编写测试用例 /src/test_case/test_viewpage.py

//src/test_case/test_viewpage.py

import unittest
from selenium import webdriver
import sys
sys.path.append("../..")
from src.pages.menu import Menu
from src.common.setup import login
from time import sleep


class TestViewPage(unittest.TestCase):
    def setUp(self):
        self.driver = login("username","passwd")        

    def test_viewpage_report(self):
        '''View Page test: View Report'''
        self.menu = Menu(self.driver)
        menu = self.menu
        menu.move_on_menu_Test_Management()
        Print "Click Menu: View Report."
        menu.click_menu_View_Report()
        
        print "Check if the test report page is loaded correctly."
        ret = menu.check_report_page_loaded("Test Report")
        self.assertTrue(ret, msg="Failed to load TestReport page")


    def tearDown(self):
        self.driver.close()

if __name__ == '__main__':
    unittest.main()

编写完测试用例后,现在要开始运行测试用例,如上述代码所示, 通过unittest.main()来启动当前测试用例,直接F5运行测试用例即可

if __name__ == '__main__':
    unittest.main()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值