Flask Deepness in Initial Practice of Interface Automation

Preface

The previous section describes access to flask's simple get request, its combination with requests, how parameters passed in the request are returned, and how POST requests are implemented

1. get Passage

Get the last request.py

import requests

parm = {"username":"Great QA", "password":"123"}
url = "http://localhost:5000/login"

res = requests.get(url, params=parm).text
print(res)

Here you need to think about how to pass in the username value to the login interface and return the value with the parameter value passed in?
Get the server.py from the last article

from flask import Flask

app = Flask(__name__)

@app.route("/login", methods=["GET"])
def login():
    # Username =?Here we want to think about how to receive username
    data = {
        "errorCode":200,
        "errorMsg":"Success",
        "data":{"username":username}
    }
    return data
    
if __name__ == "__main__":
    app.run()

Obviously, in the comment line code, we operate directly like normal variable assignments

username = "Great QA"

If so, the code will be written to death. Our requirement is to dynamically return this username value based on the parameters I request, which is obviously not feasible and foolish.
So, can we pass him a value like a method preacher?

def login(username):
    username = username

At first glance that looks like that, but that doesn't work either. We're requesting access to a routing address. Although the routes and methods are named login, they're not the same thing. The address our browser accesses is that the routing address is not the method name. Moreover, simple py scripts, methods are passed in at the time of the call, so where is server.py direct?Call?
Therefore, a second flask knowledge point needs to be introduced

from flask import request

Note: The requests and requests libraries here are fundamentally different. They are also named differently, so don't confuse
After the package is quoted, the comment code can be modified to

@app.route("/login", methods=["GET"])
def login():
    username = request.args.get("username")

In the code, we use the request.args.get() method to get the value corresponding to the keyword, which is username in the get request. The value obtained is "amazing QA". If the keyword does not exist in the request, it is returned as null. We try running the server script and executing the request script to see what happens.

As shown in the figure, if there seems to be a problem in our Chinese language, make some modifications in the server script

import json

data = json.dumps({
        "errorCode":200,
        "errorMsg":"Success",
        "data":{"username":username}
    }, ensure_ascii=False)

Introduce the JSON library, serialize the return value data in the server using the json.dumps() method, parameter ensure_ascii=False.
Start the server again and run the request script

This way the Chinese language can be displayed properly.

So far, the get request in flask has been basically introduced and can basically handle some of the initial interface test code validation.
server.py completion code

from flask import Flask
from flask import request
import json

app = Flask(__name__)

@app.route("/login", methods=["GET"])
def login():
    # Username =?Here we want to think about how to receive username
    username = request.args.get("username")
    data = json.dumps({
        "errorCode":200,
        "errorMsg":"Success",
        "data":{"username":username}
    }, ensure_ascii=False)
    return data
    
if __name__ == "__main__":
    app.run()

You can try modifying the keywords in request.args.get() yourself to see if the return value is null.

II. POST Request

After introducing the get request, we are officially entering POST
Or that server script, we added a route for the POST request

@app.route("/register", methods=["POST"])
def register():
    data = json.dumps({
        "errorCode":200,
        "errorMsg":"Register Success"
        "data":{"username":"Great QA", "password":"123"}
    }, ensure_ascii=False)
    return data

Let's try to get access to this route in a browser and see what happens.

Obviously, POST's request mode, access in browser by get mode, is rejected. "Not allowed mode"
So how do you get the return value in your browser?
Here we need to write a simple html. Submit the form to this routing address. Then we can see the return value in the browser.
form.html

<html>

<body>
    <form action="http://127.0.0.1:5000/register" method="POST">
        <input type="text" name="username"><br>
        <input type="password" name="password"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Friends who know html should know that the form submission address and method attribute values in the form tag correspond to the form submission address. Write the route address of our flask here and request as POST

The result of such an html file is shown in the figure, so we click the Submit button to see if we can return the normal value

Browser display solved, how to change to script?
request_post.py

import requests

url = "http://localhost:5000/register"

data = {"username":"Great QA", "password":"123"}

res = requests.post(url, data=data).text
print(res)


The result of execution is shown in the figure.

The new question comes again. We get request parameters in get request mode, so how do we get them in post?
The POST and GET methods are slightly different, so we modify them in the register method
server.py

@app.route("/register", methods=["POST"])
def register():
    username = request.form.get("username")
    password = request.form.get("password")
    data = json.dumps({
        "errorCode":200,
        "errorMsg":"Register Success"
        "data":{"username":username, "password":password}
    }, ensure_ascii=False)
    return data

Note: There are two points to note here

  • POST gets parameter value request.form.get and GET gets parameter value request.args.get are different

  • The keyword in request.form.get() is the keyword in the data in the request_post.py script and needs to correspond to it, otherwise it gets null, which corresponds to the name attribute value of two tags in html. Otherwise it gets null

summary

Focus on the Public Number to learn more

Tags: Python software testing

Posted on Tue, 14 Sep 2021 14:56:36 -0400 by akravets