一.页面page类编写

根据PO模式原理,我们需要把页面的常用方法封装到页面page类中,编写测试用例时直接调用对应的方法即可。

我们本次以自动测试百度搜索和登录两个功能为例来讲解。因为搜索功能在首页,所以我们需要完成首页和登录页两个page页面类。
在这里插入图片描述

(1)首页page类的封装

在这里插入图片描述

#! /usr/bin/python
# -*- coding: utf-8 -*-
from pages.base_page import BasePage
from etc.config import LOGGER
from pages.login_page import LoginPage


class IndexPage(BasePage):
    """
    封装首页page类。
    方法:1.search_contact: 首页搜索功能。
          2.goto_login: 首页前往登录功能。
    """
    def search_contact(self, text):
        """
        首页搜索功能
        :param text: 搜索内容
        :return:
        """
        try:
            LOGGER.info(f'Start to search {text}')
            # 输入内容到搜索框
            ele = self._wait_element_to_click('id','kw')
            self._send_keys_element(text=text,ele=ele)
            # 点击百度一下
            ele = self._wait_element_to_click('id','su')
            self._click_element(ele)
            LOGGER.info(f'Search {text} success')
        except Exception as e:
            LOGGER.error(f'Search {text} failed, the reason is {e}')
            raise Exception(f'Search {text} failed')

将页面元素提取都数据文件
上面的代码中,我们将页面元素的信息写在了代码中。这种方式不利于后续维护,因为前端的页面元素如果发生变法,将难以维护代码。我们可以将页面的元素提取到数据文件中,当页面元素发生变化时,可以直接在数据文件中更改,利于我们的维护。
在这里插入图片描述

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os

from pages.base_page import BasePage
from etc.config import LOGGER, BASEDIR
from pages.login_page import LoginPage
from tools.get_data import get_data_from_ini


class IndexPage(BasePage):
    """
    封装首页page类。
    方法:1.search_contact: 首页搜索功能。
          2.goto_login: 首页前往登录功能。
    """
    def search_contact(self, text):
        """
        首页搜索功能
        :param text: 搜索内容
        :return:
        """
        try:
            LOGGER.info(f'Start to search {text}')
            # 输入内容到搜索框
            message = get_data_from_ini(path=os.path.join(BASEDIR,'datas/element.ini'),node='index',key='search_input')
            LOGGER.info(message)
            ele = self._wait_element_to_click(*message)
            self._send_keys_element(text=text,ele=ele)
            # 点击百度一下
            message = get_data_from_ini(path=os.path.join(BASEDIR, 'datas/element.ini'), node='index',key='search_btn')
            ele = self._wait_element_to_click(*message)
            self._click_element(ele)
            LOGGER.info(f'Search {text} success')
        except Exception as e:
            LOGGER.error(f'Search {text} failed, the reason is {e}')
            raise Exception(f'Search {text} failed')

二.测试用例编写

在编写测试用例时,我们一般把用例数据提取到数据文件中。使用数据驱动来驱动测试用例。可以方便我们用例的管理和维护。

数据文件
在这里插入图片描述
测试用例代码

在这里插入图片描述

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import time

import pytest
from etc.config import LOGGER
from pages.index_page import IndexPage
from tools.get_data import get_data_from_yaml
from etc.config import BASEDIR

class Test_case_search:
    """
    搜索功能测试用例
    """
    datas = get_data_from_yaml(os.path.join(BASEDIR,'datas/search_datas.yaml'))
    def setup(self):
        LOGGER.info('START TO EXECUTE TEST CASE')
        self.index = IndexPage()
        self.index._get_url('https://www.baidu.com/')


    def teardown(self):
        self.index._quit_driver()

    @pytest.mark.parametrize("text",datas) # 数据参数化,实现数据驱动
    def test_search(self,text):
        self.index.search_contact(text=text)
        time.sleep(3)
        assert text[0] in self.index._get_title() # 添加断言
        LOGGER.info('EXECUTE TEST CASE SUCCESS')

if __name__ == "__main__":
    pytest.main(["-vs","./test_search_case.py"])

三.增加失败截图及测试报告

我们使用allure来生成测试报告。

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import allure
import pytest
from etc.config import LOGGER
from pages.index_page import IndexPage
from tools.get_data import get_data_from_yaml
from etc.config import BASEDIR
from tools.get_strtime import get_now_time

class Test_case_search:
    """
    搜索功能测试用例
    """
    datas = get_data_from_yaml(os.path.join(BASEDIR,'datas/search_datas.yaml'))
    def setup(self):
        LOGGER.info('START TO EXECUTE TEST CASE')
        self.index = IndexPage()
        self.index._get_url('https://www.baidu.com/')


    def teardown(self):
        self.index._quit_driver()

    @pytest.mark.parametrize("text",datas) # 数据参数化,实现数据驱动
    def test_search(self,text):
        try:
            self.index.search_contact(text=text)
            time.sleep(3)
            assert text[0] in self.index._get_title() # 添加断言
            LOGGER.info('EXECUTE TEST CASE SUCCESS')
        except Exception as e:
            LOGGER.error(f'EXECUTE TEST CASE failed, the reason is {e}')
            img_path = os.path.join(BASEDIR,f'reports/images/{get_now_time()}.png')
            self.index._get_screenshot_as_file(img_path) # 失败截图
            allure.attach.file(source=img_path,name='失败截图',attachment_type=allure.attachment_type.PNG) # 将失败截图上传到测试报告中
            raise e

if __name__ == "__main__":
    json_path = os.path.join(BASEDIR, f'reports/allure_result/{get_now_time()}')  # 指定生成的json报告文件的路径
    html_path = os.path.join(BASEDIR, f'reports/allure_report/{get_now_time()}')  # 指定生成的html报告文件的路径
    pytest.main(['-vs', 'test_search_case.py', f'--alluredir={json_path}']) # 生成json报告
    os.system(f'allure generate {json_path} -o {html_path}') # 使用allure生成HTML报告
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐