Interface Automation uses reflection to solve the problem of data dependencies

First, let's look at what is reflected in python?

The method of reflecting it can map to a string or instance variables instance may then be performed to load, modify, and other operations. It has four important ways:

  • getattr Gets the object attribute specifies the name of the string
  • set a target object setattr
  • Determining whether the object has hasattr corresponding object (String)
  • delattr delete the specified property

Here I talk about two commonly used.

Figure:

Defines a GetData () class, there is provided a class attribute, cookie = None, and then set a new value function setattr object GetData inside cookie attributes, i.e. COOKIE = biscuits, inside the final value and then removed getattr function. As can be seen, which is the attribute value of the replacement process.

The above talked about the concept of reflection, Here's what practical use, how to use reflection to resolve data dependencies.

Test target, after a successful login, see my boutique interface. Avatar and assert the upper right corner of the student's name. Specific interface as follows:

To see my first boutique interface, you must be logged. FIG login interface code as follows:

GetData introduced first class, then the login is successful, the login is successful after determining if the cookie to use, setattr (GetData, 'cookie', login_res.cookies), to which the cookie = None GetData class attribute setting new properties.

Use the time to use getattr (GetData, 'cookie'), the inside of the extracted attribute value. I figure below boutique transfer cookie login page.

All code is as follows:

import unittest
from API_AUTO.tools.http_request import HttpRequest
import re
from API_AUTO.tools.get_data import GetData


class TestHttp(unittest.TestCase):
    def setUp(self):
        pass

    def test_Normal_login(self):
        '''正常登录'''

        url = 'https://www.ketangpai.com/UserApi/login'
        data = {
            "email": "[email protected]",
            "password": "A137898",
            "remember": 0
        }
        headers = {
            "User-Agent": " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded",
        }
        login_res = HttpRequest().http_request('post', url, data, headers)
        if login_res.cookies:
            '''如果有cookie就进行替换,if后面的条件语句值不为空,即条件成立'''
            setattr(GetData, 'cookie', login_res.cookies)
        try:
            self.assertEqual(1, login_res.json()['status'], '登录失败')
        except AssertionError as e:
            print('登录错误{}'.format(e))
            raise e

    def test_mooc(self):
        '''我的精品页面'''
        url1 = 'https://www.ketangpai.com/Mooc/Mooc/index.html'
        headers1 = {
            "Referer": "https: // www.ketangpai.com / Main / index.html",
            "User-Agent": " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",

        }
        res1 = HttpRequest().http_request('get', url=url1, headers=headers1, cookies=getattr(GetData, 'cookie'))
        try:
            pattern = '<img class=.*?\salt=(".*?").*?>'
            regular = re.search(pattern, res1.text, re.S)
            self.assertEqual('夏茂杰', eval(regular.group(1)), '进入我的界面失败')
        except Exception as e:
            print('错误是{}'.format(e))
            raise e

    def tearDown(self):
        pass


if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(TestHttp("test_Normal_login"))
    suite.addTest(TestHttp("test_mooc"))
    runner = unittest.TextTestRunner()
    runner.run(suite)

  

Guess you like

Origin www.cnblogs.com/xiamaojjie/p/12032199.html