zoukankan      html  css  js  c++  java
  • jpress 用unnitest重构

    一、重构要注意以下几点:

    1. 同步用例
    2. 继承测试用例基础 unitest.TestCase
    3. 将__init__修改为setup
    4. assert断言,修改为unitest断言
    5. 关掉浏览器放到teardown
    6. 注:注意清空数据,顺序测试要加序号,例test1、test2

    二、用户注册

    from selenium import  webdriver
    from selenium.webdriver.support import  expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from Utils import Utils
    from time import sleep
    import unittest
    from Lib.HTMLTestRunner import HTMLTestRunner
    
    # 用户注册
    class TestUserRigster(unittest.TestCase):
        @classmethod
        def setUpClass(cls) -> None:
            super().setUpClass()
            cls.driver = webdriver.Chrome()
            cls.driver.get("xx/jpress/user/register")
            cls.driver.maximize_window()
        # def __init__(self):
        #     self.driver = webdriver.Chrome()
        #     self.driver.get("xxx/jpress/user/register")
        #     self.driver.maximize_window()
        def test1_userRegister_code_err(self):
            username = 'test01'
            email = 'test@163.com'
            pwd = '123456'
            confirmPwd = '123456'
            captcha = '666'
            expected ='验证码不正确'
            self.driver.find_element_by_name('username').send_keys(username)
            self.driver.find_element_by_name('email').send_keys(email)
            self.driver.find_element_by_name('pwd').send_keys(pwd)
            self.driver.find_element_by_name('confirmPwd').send_keys(confirmPwd)
            self.driver.find_element_by_name('captcha').send_keys(captcha)
            self.driver.find_element_by_class_name('btn').click()
    
            WebDriverWait(self.driver,5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            assert alert.text == expected
            alert.accept()
            sleep(5)
    
        def test2_userRegister_code_sucess(self):
            username = 'test01'
            email = 'test@163.com'
            pwd = '123456'
            confirmPwd = '123456'
            captcha = ''
            expected = '注册成功,点击确定进行登录。'
            self.driver.find_element_by_name('username').clear()
            self.driver.find_element_by_name('username').send_keys(username)
            self.driver.find_element_by_name('email').clear()
            self.driver.find_element_by_name('email').send_keys(email)
            self.driver.find_element_by_name('pwd').clear()
            self.driver.find_element_by_name('pwd').send_keys(pwd)
            self.driver.find_element_by_name('confirmPwd').clear()
            self.driver.find_element_by_name('confirmPwd').send_keys(confirmPwd)
            captcha = Utils.get_code(self.driver,'captchaimg')
            self.driver.find_element_by_name('captcha').clear()
            self.driver.find_element_by_name('captcha').send_keys(captcha)
            self.driver.find_element_by_class_name('btn').click()
            WebDriverWait(self.driver,5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            assert alert.text == expected
            alert.accept()
            sleep(5)
    
    
    if __name__ == '__main__':
    
        suite = unittest.TestSuite()
        # 方法一: 通过测试用例类进行加载
        suite.addTest(TestUserRigster("test2_userRegister_code_sucess"))
    
        fp = open(r"./result.html", "wb")
        runner = HTMLTestRunner(stream=fp, title="test register", description="用例执行情况")
        runner.run(suite)
        runner.generateReport()
        fp.close()
        # unittest.main()
    

    三、用户登录

    from selenium import  webdriver
    from selenium.webdriver.support import  expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from Utils import Utils
    from time import sleep
    import unittest
    
    # 用户注册
    class TestUserLogin(unittest.TestCase):
        @classmethod
        def setUpClass(cls) -> None:
            super().setUpClass()
            cls.driver = webdriver.Chrome()
            cls.driver.get("http://xx/jpress/admin/login")
            cls.driver.maximize_window()
        # def __init__(self):
        #     self.driver = webdriver.Chrome()
        #     self.driver.get("http://xx/jpress/admin/login")
        #     self.driver.maximize_window()
        # 测试用户登录,用户名错误
        def test1_user_login_error(self):
            # 用户名为空
            username = ''
            pwd = '123456'
            expected = '账号不能为空'
    
            # 输入用户名
            self.driver.find_element_by_name('user').send_keys(username)
            # 输入密码
            self.driver.find_element_by_name('pwd').send_keys(pwd)
            # 点击登录
            self.driver.find_element_by_class_name('btn').click()
    
            # 等待提示框
    
            WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
    
            sleep(3)
            # 验证
            # assert alert.text == expected
            self.assertEqual(alert.text, expected)
            alert.accept()
        def test2_userLogin_sucess(self):
            username = 'admin'
            pwd = 'admin'
            captcha = ' '
            expected = '用户中心'
            self.driver.find_element_by_name('username').send_keys(username)
            self.driver.find_element_by_name('pwd').send_keys(pwd)
            # 登录验证码取得是 xapth
            captcha = Utils.get_code(self.driver, '//*[@id="form"]/div[3]/img')
            self.driver.find_element_by_name('captcha').clear()
            self.driver.find_element_by_name('captcha').send_keys(captcha)
            self.driver.find_element_by_class_name('btn').click()
            sleep(1)
            WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            sleep(3)
            assert self.driver.title == expected
    

    四、管理员登录

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from Utils import Utils
    from time import sleep
    import unittest
    
    # 管理员登录
    class TestAdminLogin(unittest.TestCase):
    
    
        @classmethod
        def setUpClass(cls) -> None:
            super().setUpClass()
            cls.driver = webdriver.Chrome()
            cls.driver.get("http://192.168.242.101:8080/jpress/admin/login")
            cls.driver.maximize_window()
        # def __init__(self):
        #     self.driver = webdriver.Chrome()
        #     self.driver.get("http://192.168.242.101:8080/jpress/admin/login")
        #     self.driver.maximize_window()
        def test_admin_login_err(self):
            username = 'admin'
            pwd = '123456'
            captcha = '666'
            excepted = '验证码不正确,请重新输入'
    
            self.driver.find_element_by_name('username').send_keys(username)
            self.driver.find_element_by_name('pwd').send_keys(pwd)
            self.driver.find_element_by_name('captcha').send_keys(captcha)
            self.driver.find_element_by_class_name('btn').click()
    
            WebDriverWait(self.driver,5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            # assert alert.text == excepted
            self.assertEqual(alert.text,excepted)
            alert.accept()
            sleep(3)
    
    
        def test_admin_login_success(self):
            username = 'admin'
            pwd = 'admin'
            excepted = 'JPress后台'
            self.driver.find_element_by_name('username').clear()
            self.driver.find_element_by_name('username').send_keys(username)
            self.driver.find_element_by_name('pwd').clear()
            self.driver.find_element_by_name('pwd').send_keys(pwd)
            #   登录验证码的识别应该修改为Xpath  //*[@id="form"]/div[3]/img
            captcha = Utils.get_code(self.driver, 'captchaimg')
            self.driver.find_element_by_name('captcha').clear()
            self.driver.find_element_by_name('captcha').send_keys(captcha)
            self.driver.find_element_by_class_name('btn').click()
    
            WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            # assert excepted == self.driver.title
            self.assertEqual(self.driver.title,excepted)
            sleep(3)
    
    

    五、分类管理

    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.select import Select
    from time import sleep
    import unittest
    
    
    #  分类管理
    class TestCatefory(unittest.TestCase):
        # @classmethod
        # def setUpClass(cls) -> None:
        #     super().setUpClass()
        #     cls.login = TestAdminLogin()
        #     runner = unittest.TextTestRunner()
        #     runner.run(cls.login)
        # 单点登录
        def __init__(self, method, login):
            unittest.TestCase.__init__(self, method)
            self.login = login
    
        # 测试新增分类失败
        def test_addCatefory_err(self):
            ca_name = ''
            parent = 'selenium'
            slug = 'test'
            ca_content = '测试添加分类'
            ca_summary = '测试摘要'
            ca_flag = '测试flag'
            # 点击菜单栏-文章
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
            sleep(1)
            #  点击文章下的二级菜单-分类
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a').click()
            sleep(1)
            #   分类名称
            self.login.driver.find_element_by_name('category.title').send_keys(ca_name)
            #   分类的父分类
            ca_parent = self.login.driver.find_element_by_name('category.pid')
            Select(ca_parent).select_by_visible_text(parent)
    
            # 输入slug
            self.login.driver.find_element_by_name('category.slug').send_keys(slug)
    
            # 排序
            self.login.driver.find_element_by_name('category.order_number').send_keys(2)
    
            # 内容
            self.login.driver.find_element_by_name('category.content').send_keys(ca_content)
    
            # 摘要
            self.login.driver.find_element_by_name('category.summary').send_keys(ca_summary)
    
            # 图标
    
            # 标识
            self.login.driver.find_element_by_name('category.flag').send_keys(ca_flag)
    
            # 提交
            self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
            # 验证提示
            expected = '分类名称不能为空'
            loc = (By.CLASS_NAME, 'toast-message')
            WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
            msg = self.login.driver.find_element(*loc).text
            assert msg == expected
    
        # 测试新增分类成功
        def test_addCatefory_sucess(self):
            ca_name = 'seTest'
            parent = 'selenium'
            slug = 'test'
            ca_content = '测试添加分类'
            ca_summary = '测试摘要'
            ca_flag = '测试flag'
            # 点击菜单栏-文章
            # self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
            sleep(1)
            #  点击文章下的二级菜单-分类
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a').click()
            sleep(1)
            #   分类名称
            self.login.driver.find_element_by_name('category.title').clear()
            self.login.driver.find_element_by_name('category.title').send_Keys(ca_name)
            #   分类的父分类
    
            ca_parent = self.login.driver.find_element_by_name('category.pid')
            Select(ca_parent).select_by_visible_text(parent)
    
            # 输入slug
            self.login.driver.find_element_by_name('category.slug').clear()
            self.login.driver.find_element_by_name('category.slug').send_Keys(slug)
    
            # 排序
            self.login.driver.find_element_by_name('category.order_number').clear()
            self.login.driver.find_element_by_name('category.order_number').send_Keys(2)
    
            # 内容
            self.login.driver.find_element_by_name('category.content').clear()
            self.login.driver.find_element_by_name('category.content').send_Keys(ca_content)
    
            # 摘要
            self.login.driver.find_element_by_name('category.summary').clear()
            self.login.driver.find_element_by_name('category.summary').send_Keys(ca_summary)
    
            # 图标
    
            # 标识
            self.login.driver.find_element_by_name('category.flag').clear()
            self.login.driver.find_element_by_name('category.flag').send_Keys(ca_flag)
    
            # 提交
            self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
            # 验证提示
            expected = None
            assert 1 == 1
    
        def runTest(self):
            self.test_addCatefory_err()
            self.test_addCatefory_sucess()
    

    六、文章管理

    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from time import sleep
    import unittest
    
    # 文章管理
    class TestArticle(unittest.TestCase):
    
        def __init__(self, method, login):
            unittest.TestCase.__init__(self, method)
            self.login = login
        # 测试添加文章
        def test_addArticle_success(self):
    
            ar_title = '测试新增文章'
            ar_connect = '文章内容'
            expected = '添加成功'
            # 点击菜单栏-文章
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
            sleep(1)
            #  点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
            sleep(1)
    
            # 点击新增文章 /html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i
            self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i').click()
            sleep(1)
    
            # 输入标题
            self.login.driver.find_element_by_id('article.title').send_keys()
    
            frame1 = self.login.driver.find_element_by_xpath('')
            self.login.driver.swich_to.frame(frame1)
            # 输入内容 /html/body
            self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
            self.login.driver.find_element_by_xpath('/html/body').clear()
            sleep(1)
            self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
            # 退出iframe
            self.login.driver.swich_to.default_content()
    
            # 发布
            self.login.driver.find_element_by_xpath('//*[@id="form"]/div/div[2]/div[1]/div/button[1]').click()
            # 验证提示
            loc = (By.CLASS_NAME, 'toast-message')
            WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
            msg = self.login.driver.find_element(*loc).text
            assert msg == expected
    
        # 测试添加文章失败
        def test_addArticle_err(self):
            self.login.driver.refresh()
            sleep(1)
            ar_title = ''
            ar_connect = '文章内容'
            ar_summary = '文章摘要'
            ar_seo_kw = '文章seo'
            expected = '标题不能为空'
            # 点击菜单栏-文章
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
            sleep(1)
            #  点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
            sleep(1)
    
            # 点击新增文章 /html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i
            self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i').click()
            sleep(1)
    
            # 输入标题
            self.login.driver.find_element_by_id('article.title').send_keys()
    
            frame1 = self.login.driver.find_element_by_xpath('')
            self.login.driver.swich_to.frame(frame1)
            # 输入内容 /html/body
            self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
            self.login.driver.find_element_by_xpath('/html/body').clear()
            sleep(1)
            self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
    
            # 添加文章摘要
            self.login.driver.find_element_by_name('article.summary').send_keys(ar_summary)
            # 添加文章seo
            self.login.driver.find_element_by_name('article.meta_keywords').send_keys(ar_seo_kw)
            # 退出iframe
            self.login.driver.swich_to.default_content()
    
            # 发布
            self.login.driver.find_element_by_xpath('//*[@id="form"]/div/div[2]/div[1]/div/button[1]').click()
            # 验证提示
            loc = (By.CLASS_NAME, 'toast-message')
            WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
            msg = self.login.driver.find_element(*loc).text
            assert msg == expected
    
            # 删除文章
        def test_delArticle(self):
            self.login.driver.refresh()
            expected = ' '
            # 点击菜单栏-文章
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
            sleep(1)
            #  点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
            sleep(1)
    
            # 找到此条文章, 鼠标再移动到垃圾箱
            ac_link = self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[2]/td[2]/strong/a')
            ActionChains(self.login.driver).move_to_element(ac_link).perform()
            sleep(1)
    
            # 查看文章列表数目
            ac_sum = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
    
            # 删除文章
            ac_select = self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[2]/td[2]/div/div/a[3]')
            ac_select.click()
            # 测试删除后文章数
            ac_del_num =len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
            assert ac_del_num == ac_sum+1
    
        def test_delAllArticle(self):
            expected = ' '
            # 点击菜单栏-文章
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
            sleep(1)
            #  点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
            self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
            sleep(1)
            # 勾选全部
            ac_link = self.login.driver.
                find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[1]/th[1]/input')
            ac_link.click()
            # 点击批量删除 //*[@id="batchDel"]
            self.login.driver.find_element_by_xpath('//*[@id="batchDel"]').click()
    
            WebDriverWait(self.login.driver, 5).until(EC.alert_is_present())
            alert = self.login.driver.swich_to.alert
            alert.accept()
    
            sleep(1)
            ac_del_num = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
            assert ac_del_num == 0
    
        def runTest(self):
            self.test_addArticle_err()
            self.test_addArticle_success()
            self.test_delArticle()
            self.test_delAllArticle()
    
    if __name__ == '__main__':
        testArticle = TestArticle()
        testArticle.test_addArticle_err()
    
    

    七、测试运行

    from Cases.Basic.test_userRigster import TestUserRigster
    from Cases.Basic.test_userLogin import TestUserLogin
    from Cases.Basic.test_admin_login import TestAdminLogin
    from Cases.Basic.test_catefory import TestCatefory
    from Cases.Basic.test_article import TestArticle
    import unittest
    import os
    from Lib.HTMLTestRunner import HTMLTestRunner
    
    if __name__ == '__main__':
        report_title = 'Jpress用例执行报告'
        desc = '用于展示修改样式后的HTMLTestRunner'
        report_file = 'ExampleReport.html'
    
        path = os.path.dirname(os.path.dirname(__file__)) + '/reports/ExampleReport.html'
        print(path)
        testsuite = unittest.TestSuite()
        testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestUserRigster))
        testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestUserLogin))
        testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestAdminLogin))
        testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestCatefory))
        testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestArticle))
    
        with open(path, 'wb') as report:
            runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
            runner.run(testsuite)
    
    
    
  • 相关阅读:
    代码块;继承;this与super关系;重载与重写对比;类的继承特点;final关键字 (Java Day08)
    变量访问;this关键字;静态;工具类;帮助文档;Math使用;Arrays使用(Java Day07)
    面向对象;类和对象;访问对象;创建对象在内存中的理解;匿名对象;封装和this (Java Day06)
    如何保证 RocketMQ 不丢失消息
    Java-String类型的参数传递问题
    图解前中后序遍历
    一文彻底理解ReentrantLock可重入锁的使用
    聊聊MySQL、HBase、ES的特点和区别
    MySQL vs Java 数据类型
    Multi-Tenancy多租户模式
  • 原文地址:https://www.cnblogs.com/c-jw/p/13948669.html
Copyright © 2011-2022 走看看