Exemplo n.º 1
0
 def test_login(self, item):
     # 第一步:准备用例数据
     # 接口地址
     url = conf.get("env", "base_url") + item["url"]
     # 请求头
     headers = eval(conf.get("env", "headers"))
     # 请求参数
     item["data"] = replace_data(item["data"], TestLogin)
     params = eval(item["data"])
     # 预取结果
     expected = eval(item["expected"])
     # 请求方法
     method = item["method"]
     # 第二步:调用接口,获取实际结果
     response = requests.request(method,
                                 url=url,
                                 json=params,
                                 headers=headers)
     res = response.json()
     print("预期结果:", expected)
     print("实际结果:", response.text)
     # 第三步:断言
     try:
         self.assertEqual(expected["code"], res["code"])
         self.assertEqual(expected["msg"], res["msg"])
     except AssertionError as e:
         log.error("用例{},执行未通过".format(item["title"]))
         log.exception(e)
         raise e
     else:
         log.info("用例{},执行通过".format(item["title"]))
Exemplo n.º 2
0
    def test_login(self, item):
        # 1. 准备测试数据
        url = conf.get("env", "base_url") + item["url"]
        method = item["method"]
        params = eval(item["data"])
        expected = eval(item["expected"])

        # 2. 请求接口,获取实际结果
        res = requests.request(method, url, json=params).json()

        print("-------当前执行的用例是:{}----------".format(item["title"]))
        print("请求参数:", params)
        print("预期结果:", expected)
        print("实际结果:", res)
        print("===============执行结束===============\n")

        # 3. 断言

        try:
            assert_dict_item(expected, res)
        except AssertionError as e:
            log.exception(e)
            log.error("--测试用例:{}--执行失败".format(item["title"]))
            raise e
        else:
            log.info("--测试用例:{}--执行通过".format(item["title"]))
Exemplo n.º 3
0
 def wait_element_presence(self,
                           locator,
                           img_info,
                           timeout=15,
                           poll_frequency=0.5):
     start_time = time.time()
     try:
         # 等待元素被加载
         ele = WebDriverWait(self.driver, timeout, poll_frequency).until(
             EC.presence_of_element_located(locator))
     except Exception as e:
         # 输出日志
         log.error("元素--{}--等待可见超时".format(locator))
         log.exception(e)
         # 对当前页面进行截图,格式:时间_页面_操作.png
         file_name = "{}_{}.png".format(start_time, img_info)
         file_path = os.path.join(ERROR_IMAGE, file_name)
         self.driver.save_screenshot(file_path)
         log.info("错误页面截图成功,图片保存的路径:{}".format(file_path))
         raise e
     else:
         # 元素等待之后获取时间
         end_time = time.time()
         log.info("元素--{}--等待成功,等待时间是{}秒".format(locator,
                                                 end_time - start_time))
         return ele
Exemplo n.º 4
0
 def wait_ele_be_clickable(self,
                           loc,
                           loc_desc='',
                           timeout=20,
                           poll_time=0.5):
     """
     等待元素可点击
     :param loc: 元素定位器 ——> :(BY.xxx,'表达式')
     :param loc_desc: 元素文本的描述(记录日志时使用)
     :param timeout: 最大等待时间
     :param poll_time: 等待的轮询间隔
     :return: 定位到的element
     """
     try:
         ele = WebDriverWait(self.driver,
                             timeout=timeout,
                             poll_frequency=poll_time).until(
                                 EC.element_to_be_clickable(loc))
     except Exception as e:
         log.error('等待元素-【{}】-可点击超时'.format(loc_desc))
         log.exception(e)
         self.page_save_screenshot(loc_desc)
         raise e
     else:
         log.info('等待元素-【{}】-可点击成功'.format(loc_desc))
         return ele
Exemplo n.º 5
0
 def test_audit(self, item):
     # 第一步:准备用例数据
     url = conf.get("env", "base_url") + item["url"]
     headers = eval(conf.get("env", "headers"))
     headers["Authorization"] = self.admin_token
     # 替换用例参数
     item["data"] = replace_data(item["data"], TestAudit)
     params = eval(item["data"])
     # 请求方法
     method = item["method"]
     # 预期结果
     expected = eval(item["expected"])
     # 第二步:请求接口,获取实际返回的结果
     response = requests.request(url=url, method=method, json=params, headers=headers)
     res = response.json()
     # 第三步:断言
     try:
         self.assertEqual(expected["code"], res["code"])
         self.assertEqual(expected["msg"], res["msg"])
         if item["title"] == "审核通过":
             TestAudit.pass_loan_id = params["loan_id"]
     except AssertionError as e:
         log.error("用例{},执行未通过".format(item["title"]))
         log.exception(e)
         self.excel.write_excel(row=item['case_id'] + 1, column=8, value='未通过')
         raise e
     else:
         log.info("用例{},执行通过".format(item["title"]))
         self.excel.write_excel(row=item['case_id'] + 1, column=8, value='通过')
Exemplo n.º 6
0
    def test_info(self, item):
        # 请求接口
        url = conf.get('env', 'base_url') + replace_data(item['url'], TestInfo)
        # 请求方法
        method = item['method']
        # 请求头
        headers = eval(conf.get('env', 'headers'))
        headers['Authorization'] = self.token
        # 预期结果
        expected = eval(item['expected'])
        # 请求接口获得实际结果
        response = requests.request(method=method, url=url, headers=headers)
        res = response.json()

        print('预计结果:', expected)
        print('实际结果:', res)

        # 断言
        try:
            self.assertEqual(res['code'], expected['code'])
            self.assertEqual(res['code'], expected['code'])
        except AssertionError as e:
            log.exception(e)
            log.error('用例{},执行未通过'.format(item['title']))
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='未通过')
            raise e
        else:
            log.info('用例{},执行通过'.format(item['title']))
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='通过')
Exemplo n.º 7
0
    def test_invest(self, case):
        # 1.请求行,url,method
        invest_url = conf.get("env", "base_url") + case["url"]
        invest_method = case["method"]

        # 2.请求头,headers
        invest_headers = eval(conf.get("env", "headers_v2"))
        if case["interface"] != "login":
            invest_headers["Authorization"] = getattr(TestData, "token_value")

        # 3.请求体,data
        invest_data = eval(replace_data(case["data"]))

        # 4.预期结果和回写数据的行数
        invest_expected = eval(case["expected"])
        row_num = case["case_id"] + 1

        # 5.发起请求之前,现在invest表当中查询当前投资用户对当前标的的投资数量start_num
        if case["check_sql"]:
            query_sql = replace_data(case["check_sql"])
            start_num = self.mysql.get_count(query_sql)

        # 6.发起请求,返回数据
        response = self.http.send_requests_v2(url=invest_url,
                                              method=invest_method,
                                              headers=invest_headers,
                                              json=invest_data)
        json_data = response.json()

        # 7.根据接口的类型来判断是否提取token,member_id,loan_id
        if case["interface"] == "login":
            member_id = jsonpath.jsonpath(json_data, "$..id")[0]
            setattr(TestData, "member_id", str(member_id))
            token_type = jsonpath.jsonpath(json_data, "$..token_type")[0]
            token = jsonpath.jsonpath(json_data, "$..token")[0]
            token_value = token_type + " " + token
            setattr(TestData, "token_value", token_value)
        elif case["interface"] == "add":
            loan_id = jsonpath.jsonpath(json_data, "$..id")[0]
            setattr(TestData, "loan_id", str(loan_id))

        # 8.断言
        try:
            self.assertEqual(invest_expected["code"], json_data["code"])
            self.assertEqual(invest_expected["msg"], json_data["msg"])
            # 发起请求之后,再次在invest表当中查询当前投资用户对当前标的的投资数量end_num
            if json_data["msg"] == "OK" and case["check_sql"]:
                query_sql = replace_data(case["check_sql"])
                end_num = self.mysql.get_count(query_sql)
                self.assertEqual(end_num - start_num, 1)
        except AssertionError as e:
            self.excle.write_data(row=row_num, column=8, value="未通过")
            log.error("用例:{}未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excle.write_data(row=row_num, column=8, value="通过")
            log.info("用例:{}通过".format(case["title"]))
Exemplo n.º 8
0
    def test_add(self, case):
        # 1.请求行 url,method
        add_url = conf.get("env", "base_url") + case["url"]
        add_method = case["method"]

        # 2.请求头 headers
        add_headers = eval(conf.get("env", "headers_v2"))
        if case["interface"] != "login":
            add_headers["Authorization"] = getattr(TestData, "token_value")

        # 3.请求体 data
        add_data = eval(replace_data(case["data"]))

        # 4.预期结果和回写数据的行数
        add_expected = eval(case["expected"])
        row_num = case["case_id"] + 1

        # 5.在发起请求之前,先在数据库查询当前管理员标的数量为start_num
        if case["check_sql"]:
            sql = replace_data(case["check_sql"])
            start_num = self.mysql.get_count(sql)

        # 6.发起请求,获取实际返回结果
        response = self.http.send_requests_v2(url=add_url,
                                              method=add_method,
                                              headers=add_headers,
                                              json=add_data)
        json_data = response.json()

        # 7.登录接口获取返回数据当中的member_id和token
        if case["interface"] == "login":
            # admin_member_id
            admin_member_id = jsonpath.jsonpath(json_data, "$..id")[0]
            setattr(TestData, "admin_member_id", str(admin_member_id))
            # token
            token_type = jsonpath.jsonpath(json_data, "$..token_type")[0]
            token = jsonpath.jsonpath(json_data, "$..token")[0]
            token_value = token_type + " " + token
            setattr(TestData, "token_value", token_value)
        # 8.断言
        try:
            self.assertEqual(add_expected["code"], json_data["code"])
            self.assertEqual(add_expected["msg"], json_data["msg"])
            if case["check_sql"]:
                sql = replace_data(case["check_sql"])
                end_num = self.mysql.get_count(sql)
                self.assertEqual(end_num - start_num, 1)
        except AssertionError as e:
            self.excle.write_data(row=row_num, column=8, value="未通过")
            log.error("用例:{}未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excle.write_data(row=row_num, column=8, value="通过")
            log.info("用例:{}通过".format(case["title"]))
Exemplo n.º 9
0
    def test_audit(self, case):
        # 1.请求行 url,method
        audit_url = conf.get("env", "base_url") + case["url"]
        audit_method = case["method"]

        # 2.请求头 headers
        audit_headers = eval(conf.get("env", "headers_v2"))
        if case["interface"] != "login":
            audit_headers["Authorization"] = getattr(TestData, "token_value")

        # 3.请求体 data
        audit_data = eval(replace_data(case["data"]))

        # 4.预期结果和回写数据的行数
        audit_expected = eval(case["expected"])
        row_num = case["case_id"] + 1

        # 5.发起请求,返回数据
        response = self.http.send_requests_v2(url=audit_url,
                                              method=audit_method,
                                              headers=audit_headers,
                                              json=audit_data)
        json_data = response.json()

        # 6.根据接口的类型来判断是否提取token,member_id,loan_id
        if case["interface"] == "login":
            admin_member_id = jsonpath.jsonpath(json_data, "$..id")[0]
            setattr(TestData, "admin_member_id", str(admin_member_id))
            token_type = jsonpath.jsonpath(json_data, "$..token_type")[0]
            token = jsonpath.jsonpath(json_data, "$..token")[0]
            token_value = token_type + " " + token
            setattr(TestData, "token_value", token_value)
        elif case["interface"] == "add":
            loan_id = jsonpath.jsonpath(json_data, "$..id")[0]
            setattr(TestData, "loan_id", str(loan_id))
        elif json_data["msg"] == "OK" and case["title"] == "审核通过":
            pass_loan_id = getattr(TestData, "loan_id")
            setattr(TestData, "pass_loan_id", pass_loan_id)

        # 7.断言
        try:
            self.assertEqual(audit_expected["code"], json_data["code"])
            self.assertEqual(audit_expected["msg"], json_data["msg"])
            if case["check_sql"]:
                sql = replace_data(case["check_sql"])
                status = self.mysql.get_one(sql)["status"]
                self.assertEqual(status, audit_expected["status"])
        except AssertionError as e:
            self.excle.write_data(row=row_num, column=8, value="未通过")
            log.error("用例:{}:未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excle.write_data(row=row_num, column=8, value="通过")
            log.info("用例:{}:通过".format(case["title"]))
Exemplo n.º 10
0
    def test_register(self, item):
        """测试注册的测试用例方法"""
        # 请求数据
        """参数化开始"""
        # 判断请求参数中是否有手机号,需要替换
        if "*phone*" in item['data']:
            phone = self.random_phone()
            # 将参数中的*phone*替换为随机生成的手机号
            item['data'] = item['data'].replace('*phone*', phone)
        """参数化结束"""

        params = eval(item['data'])
        # 请求头
        headers = eval(conf.get('env', 'headers'))
        # 请求接口
        register_url = conf.get('env', 'base_url') + item['url']
        # 预期结果
        expected = eval(item['expected'])
        # 请求方式
        method = item['method']
        # 实际结果
        response = requests.request(method=method,
                                    json=params,
                                    url=register_url,
                                    headers=headers)
        res = response.json()

        print('预期结果:', expected)
        print('请求参数:', params)
        print('实际结果:', res)
        # 断言判断
        try:
            self.assertEqual(expected['code'], res['code'])
            self.assertEqual(expected['msg'], res['msg'])

            # 获取测试数据中的 check_sql 判断注册的信息在数据库中是否存在
            check_sql = item['check_sql']
            if check_sql:
                # params['mobile_phone'] 请求参数中的 mobile_phone 的值
                res = db.find_data(check_sql.format(params['mobile_phone']))
                # 判断res结果是否为空,为空则断言失败,用例执行不通过
                self.assertTrue(res)

        except AssertionError as e:
            log.exception(e)
            log.error('用例{},测试未通过'.format(item['title']))
            self.excel.write_excel(row=(item['case_id'] + 1),
                                   column=8,
                                   value='未通过')
            raise e
        else:
            log.info('用例{},测试通过'.format(item['title']))
            self.excel.write_excel(row=(item['case_id'] + 1),
                                   column=8,
                                   value='通过')
Exemplo n.º 11
0
 def test_login_invalid_user(self, test_info, init_web):
     driver = init_web
     expected = test_info['expected']
     LoginPage(driver).login(test_info["mobile"], test_info["pwd"])
     invalid_msg = LoginPage(driver).get_invalid_msg()
     try:
         assert invalid_msg == expected
     except AssertionError as e:
         log.error("断言失败")
         log.exception(e)
         raise e
     else:
         log.info("用例执行通过")
Exemplo n.º 12
0
    def test_add(self, item):
        # 请求接口的url
        url = conf.get('env', 'base_url') + item['url']
        # 请求参数
        params = eval(replace_data(item['data'], TestAddV3))

        # 请求参数中添加 timestamp 和sign
        crypt_info = HandleSign.generate_sign(self.token_value)
        params['timestamp'] = crypt_info['timestamp']
        params['sign'] = crypt_info['sign']

        # 请求头
        # headers = eval(conf.get('env', 'headers'))

        # 请求头 V3
        headers = eval(conf.get('env', 'headersV3'))

        headers['Authorization'] = self.token
        # 请求方法
        method = item['method']
        # 预期结果
        expected = eval(item['expected'])
        # 请求接口获得请求结果
        response = requests.request(method=method,
                                    url=url,
                                    json=params,
                                    headers=headers)
        res = response.json()
        print('预期结果:', expected)
        print('实际结果:', res)
        # 断言
        try:
            self.assertEqual(res['code'], expected['code'])
            self.assertEqual(res['code'], expected['code'])
            if item['check_sql']:
                result = db.find_data(
                    (item['check_sql']).format(jsonpath(res, '$..id')[0]))
                self.assertTrue(result)
        except AssertionError as e:
            log.error('用例{},执行失败'.format(item['title']))
            log.exception(e)
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='未通过')
            raise e
        else:
            log.info('用例{},执行成功'.format(item['title']))
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='通过')
Exemplo n.º 13
0
 def test_login_success_user(self, test_info, init_web):
     driver = init_web
     expected = test_info['expected']
     LoginPage(driver).login(test_info["mobile"], test_info["pwd"])
     time.sleep(0.5)  # 此处发生页面跳转,建议强制等待0.5秒
     account_msg = HomePage(driver).get_user_info()
     try:
         assert account_msg == expected
     except AssertionError as e:
         log.error("断言失败")
         log.exception(e)
         raise e
     else:
         log.info("用例执行通过")
Exemplo n.º 14
0
    def test_audit(self, item):
        # 第一步:准备用例数据
        url = conf.get("env", "base_url") + item["url"]
        # 请求头V2
        # headers = eval(conf.get("env", "headers"))

        # 请求头v3
        headers = eval(conf.get('env', 'headersV3'))
        headers["Authorization"] = self.admin_token

        # 替换用例参数
        item["data"] = replace_data(item["data"], TestAuditV3)
        params = eval(item["data"])

        # 请求参数添加timestamp和sign
        crypt_info = HandleSign.generate_sign(self.admin_token_value)
        params['timestamp'] = crypt_info['timestamp']
        params['sign'] = crypt_info['sign']

        # 请求方法
        method = item["method"]
        # 预期结果
        expected = eval(item["expected"])
        # 第二步:请求接口,获取实际返回的结果
        response = requests.request(url=url,
                                    method=method,
                                    json=params,
                                    headers=headers)
        res = response.json()
        print('预期结果:', expected)
        print('实际结果:', res)
        # 第三步:断言
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            if item["title"] == "审核通过":
                TestAuditV3.pass_loan_id = params["loan_id"]
        except AssertionError as e:
            log.error("用例{},执行未通过".format(item["title"]))
            log.exception(e)
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='未通过')
            raise e
        else:
            log.info("用例{},执行通过".format(item["title"]))
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='通过')
Exemplo n.º 15
0
 def click_element(self, locator, img_info):
     try:
         self.driver.find_element(*locator).click()
     except Exception as e:
         # 输出日志
         log.error("元素--{}--点击元素失败".format(locator))
         log.exception(e)
         # 对当前页面进行截图,格式:时间_页面_操作.png
         file_name = "{}_{}.png".format(img_info)
         file_path = os.path.join(ERROR_IMAGE, file_name)
         self.driver.save_screenshot(file_path)
         log.info("错误属性截图成功,图片保存的路径:{}".format(file_path))
         raise e
     else:
         log.info("元素--{}--元素点击成功,".format(locator))
Exemplo n.º 16
0
 def click_element(self, loc, loc_desc=''):
     """
     点击元素
     :param loc:元素定位器 ——> :(BY.xxx,'表达式')
     :param loc_desc:元素文本的描述(记录日志时使用)
     """
     try:
         self.driver.find_element(*loc).click()
     except Exception as e:
         log.error('点击元素-【{}】-失败'.format(loc_desc))
         log.exception(e)
         self.page_save_screenshot(loc_desc)
         raise e
     else:
         log.info('点击元素-【{}】-成功'.format(loc_desc))
Exemplo n.º 17
0
    def test_update(self, item):
        # 获取请求接口
        url = conf.get('env', 'base_url') + item['url']
        # 获取请求数据
        params = eval(item['data'])

        # 请求参数添加timestamp和sign
        crypt_info = HandleSign.generate_sign(self.token_value)
        params['timestamp'] = crypt_info['timestamp']
        params['sign'] = crypt_info['sign']

        # 获取请求方法
        method = item['method']
        # 获取请求头v2
        # headers = eval(conf.get('env', 'headers'))

        # 请求头 v3
        headers = eval(conf.get('env', 'headers'))
        headers['Authorization'] = self.token

        # 预计结果
        expected = eval(item['expected'])
        # 请求接口获得实际结果
        response = requests.request(method=method,
                                    url=url,
                                    json=params,
                                    headers=headers)
        res = response.json()

        print('预计结果:', expected)
        print('实际结果:', res)

        # 断言判断
        try:
            self.assertEqual(res['code'], expected['code'])
            self.assertEqual(res['msg'], expected['msg'])
        except AssertionError as e:
            log.error("用例{},执行失败".format(item['title']))
            log.exception(e)
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='未通过')
            raise e
        else:
            log.info("用例{},执行成功".format(item['title']))
            self.excel.write_excel(row=item['case_id'] + 1,
                                   column=8,
                                   value='通过')
Exemplo n.º 18
0
 def test_add_course_pass(self,course_setup,case):
     index_page = course_setup[1]
     # 1.加入课程--输入验证码--点击确认
     index_page.add_course(case['verificate_code'])
     expected = case["expected"]
     # 2.获取元素的属性
     time.sleep(2)
     res = index_page.get_add_toast()
     try:
         assert res == expected
     except AssertionError as e:
         log.error("用例--{}--执行失败".format(case['title']))
         log.exception(e)
         raise e
     else:
         log.info("用例--{}--执行成功".format(case['title']))
Exemplo n.º 19
0
class TestInvest:
    # 投资的前置条件-登录成功(默认账号可用金额足够大)-选择项目(默认选择第一个项目)点击抢投标
    # 投资失败,弹窗上出现提示信息的用例
    @pytest.mark.parametrize('case', invest_data.invest_data_is_wrong)
    def test_invest_error(self, case, invest_setup):
        print(case)
        invest_page = invest_setup[0]
        user_page = invest_setup[1]
        # 1.投资
        invest_page.invest(case["amount"])
        time.sleep(1)
        invest_page.maxmize_window()
        # 2.点击投资
        invest_page.click_invest()
        expected = case['expected']
        # 3.获取弹窗提示
        res = invest_page.get_toast_wrong()
        time.sleep(1)
        invest_page.close_wrong_toast()
        # 4 断言
        try:
            assert res == expected
        except AssertionError as e:
            log.error("用例--{}--执行失败".format(case['title']))
            log.exception(e)
            raise e
        else:
            log.info("用例--{}--执行通过".format(case['title']))
Exemplo n.º 20
0
 def input_text(self, locator, text_value, img_info):
     # 文本内容输入
     try:
         self.driver.find_element(*locator).send_keys(text_value)
     except Exception as e:
         # 输出日志
         log.error("输入文本-{}--失败".format(locator))
         log.exception(e)
         # 对当前页面进行截图,格式:时间_页面_操作.png
         file_name = "{}_{}.png".format(img_info)
         file_path = os.path.join(ERROR_IMAGE, file_name)
         self.driver.save_screenshot(file_path)
         log.info("错误属性截图成功,图片保存的路径:{}".format(file_path))
         raise e
     else:
         log.info("输入文本内容--{}--成功,".format(locator))
Exemplo n.º 21
0
 def test_login_username_is_empty(self, test_info, init_web):
     driver = init_web
     # 预期结果
     expected = test_info["expected"]
     # 实际结果
     LoginPage(driver).login(test_info["mobile"], test_info["pwd"])
     # 断言
     error_msg = LoginPage(driver).get_error_msg()
     try:
         assert error_msg == expected
     except AssertionError as e:
         log.error("断言失败")
         log.exception(e)
         raise e
     else:
         log.info("用例执行通过")
Exemplo n.º 22
0
class TestWork:
    """作业测试类"""
    @pytest.mark.work_update
    @pytest.mark.parametrize("case", Work_data.update_work_success_case)
    def test_update_work_success(self, case, work_fixture):
        """上传作业文件成功测试用例"""
        class_page, work_page = work_fixture
        #   点击进入作业页面
        class_page.click_work_title_href()
        #   第一次点击更新提交页面
        work_page.submit_file_01()
        #   点击确定编辑作业按钮
        work_page.click_dix_edit_work()
        #   点击添加作业文件
        work_page.add_file(case["data"])
        #   第二次点击更新提交页面
        work_page.submit_file_02()
        #   获取提交成功弹框信息
        res = work_page.get_success_alert_info()
        #   点击关闭提交成功弹框
        work_page.close_success_alert()
        #   预期结果
        expected = case["expected"]
        try:
            assert expected == res
        except AssertionError as e:
            log.error("用例--{}---执行未通过".format(case['title']))
            log.exception(e)
            raise e
        else:
            log.info("用例--{}---执行通过".format(case['title']))
        #   返回课程-作业页面
        work_page.click_return_work()
Exemplo n.º 23
0
class TestCourse:
    """课程测试类"""

    # @pytest.mark.skip
    @pytest.mark.course_join
    @pytest.mark.parametrize("case", Course_data.join_course_success_case)
    def test_join_courese_success(self, case, course_fixture):
        """加入班级成功测试用例"""
        index_page = course_fixture[0]
        #   点击加入课程
        index_page.click_join_course()
        #   输入课程验证码
        index_page.input_course_code(case["data"])
        #   点击加入按钮
        index_page.add()
        #   获取成功信息框信息
        res = index_page.get_success_alert_info()
        #   预取结果
        expected = case["expected"]
        try:
            assert expected == res
        except AssertionError as e:
            log.error("用例--{}---执行未通过".format(case['title']))
            log.exception(e)
            raise e
        else:
            log.info("用例--{}---执行通过".format(case['title']))
        #   刷新页面
        index_page.ref_page()
Exemplo n.º 24
0
 def test_login_pass(self, login_setup, case):
     # 登录成功--输入账号--输入密码--点击登录--断言是否登录成功
     login_page, index_page = login_setup
     login_page.reset_login_page()
     login_page.login(case['phone'], case['pwd'])
     # 断言是否登录成功
     res = index_page.is_login()
     try:
         assert res
     except AssertionError as e:
         log.error("用例--{}--执行失败".format(case['title']))
         log.exception(e)
         raise e
     else:
         log.info("用例--{}--执行成功".format(case['title']))
         index_page.click_quit_login()
Exemplo n.º 25
0
class TestHomework:
    # 作业首次提交
    @pytest.mark.parametrize('case', homework_data.homework_data_success)
    def test_homework_pass(self, case, homework_setup):
        index_page = homework_setup[0]
        homework_page = homework_setup[1]
        start_status = homework_setup[2]
        if start_status == "未完成":
            # 1.点击作业上传
            homework_page.add_load_file()
            time.sleep(10)
            homework_page.handup_homework()
            expected = case["expected"]
            # 2.获取弹窗的文本
            res = homework_page.get_homework_toast()
            time.sleep(2)
            # 关闭弹窗
            homework_page.close_homework_toast()
            time.sleep(2)
            # 3.获取作业的状态
            # end_status = homework_page.get_homework_status()
            # 4.断言
            try:
                assert res == expected
            except AssertionError as e:
                log.error("用例--{}--执行失败".format(case['title']))
                log.exception(e)
                raise e
            else:
                log.info("用例--{}--执行成功".format(case['title']))
        else:
            pass
Exemplo n.º 26
0
    def test_add(self, item):
        # 第一步:获取参数
        case_id = item["case_id"]
        # 请求的url
        url = conf.get("env", "base_url") + item["url"]
        # 请求的方法
        method = item["method"]
        # 请求的headers
        headers = eval(conf.get("env", "headers"))
        if item["interface"] == "add":
            headers["Authorization"] = self.token
        # 请求的参数
        item["data"] = replace_data(item["data"], TestAdd)
        params = eval(item["data"])
        # 预期的结果
        expected = eval(item["expected"])
        # 判断执行前的数据库状态
        sql = item["check_sql"]
        # 第二步 发送请求
        response = requests.request(url=url,
                                    method=method,
                                    json=params,
                                    headers=headers)
        res = response.json()
        if item["interface"] == "login":
            # 提取token和用户id保存为类属性
            TestAdd.token = "Bearer" + " " + jsonpath(res, "$..token")[0]
            TestAdd.member_id = jsonpath(res, "$..id")[0]

        print("预期结果:", expected)
        print("实际结果:", res)
        try:
            self.assertEqual(res["code"], expected["code"])
            self.assertEqual(res["msg"], expected["msg"])
            if item["interface"] == "add":
                if sql:
                    sql = replace_data(sql, TestAdd)
                    res = db.find_data(sql)
                self.assertTrue(res)
        except AssertionError as e:
            log.error("用例执行失败:{}".format(item["title"]))
            log.exception(e)
            raise e
            self.excel.write_data(row=case_id + 1, column=8, value="失败")
        else:
            log.info("用例执行通过:{}".format(item["title"]))
            self.excel.write_data(row=case_id + 1, column=8, value="通过")
Exemplo n.º 27
0
 def page_save_screenshot(self, loc_desc):
     """
     页面截图
     :param loc_desc: 操作的描述
     """
     try:
         # 获取当前时间,并转换为指定格式的字符串
         date_desc = time.strftime('%Y-%m-%d %H_%M_%S')
         filename = date_desc + loc_desc + '.png'
         file_path = os.path.join(ERROR_IMAGE, filename)
         self.driver.save_screenshot(file_path)
     except Exception as e:
         log.error("对-【{}】--这个操作进行页面截图失败".format(loc_desc))
         log.exception(e)
         raise e
     else:
         log.info("对-【{}】--这个操作进行页面截图成功,图片保存为{}".format(loc_desc, filename))
Exemplo n.º 28
0
 def get_element_text(self, loc, loc_desc=''):
     """
     获取元素的文本
     :param loc: 元素定位器 ——> :(BY.xxx,'表达式')
     :param loc_desc: 元素文本的描述(记录日志时使用)
     :return: 元素的text
     """
     try:
         text = self.driver.find_element(*loc).text
     except Exception as e:
         log.error('获取元素-【{}】-文本失败'.format(loc_desc))
         log.exception(e)
         self.page_save_screenshot(loc_desc)
         raise e
     else:
         log.info('获取元素-【{}】-文本成功'.format(loc_desc))
         return text
Exemplo n.º 29
0
 def test_login_data_is_none(self, login_setup, case):
     login_page, index_page = login_setup
     login_page.reset_login_page()
     # 1登录
     login_page.login(case['phone'], case['pwd'])
     expected = case["expected"]
     # 2获取页面上的错误提示
     res = login_page.get_page_error_info()
     # 3断言是否登录成功
     try:
         assert res == expected
     except AssertionError as e:
         log.error("用例--{}--执行失败".format(case['title']))
         log.exception(e)
         raise e
     else:
         log.info("用例--{}--执行成功".format(case['title']))
Exemplo n.º 30
0
 def test_login_fail(self, login_setup, case):
     login_page, index_page = login_setup
     login_page.reset_login_page()
     # 1.登录
     login_page.login(case['phone'], case['pwd'])
     expected = case['expected']
     # 2.获取页面上的错误提示
     res = login_page.get_page_error_info()
     # 3.断言
     try:
         assert res == expected
     except AssertionError as e:
         log.error("用例--{}--执行失败".format(case['title']))
         log.exception(e)
         raise e
     else:
         log.info("用例--{}--执行成功".format(case['title']))