Use GPT for automated script implementation - take it directly

First, the purpose

Testers may not know where to start when writing automated scripts. GPT can help testers get started with writing automated scripts

2. Address

GPT address: http://chat.yuting360.cn/

3. Use cases

1. Premise: We use chrome to convert the functions we need into shell scripts, and now we need to convert them into python language and combine them with the current automation framework

2. Throw our shell script to gpt, and ask him to help us output it in python format, as shown in the figure:
insert image description here

This is the code reply given by gpt

import requests

headers = {
‘Accept’: ‘application/json, text/plain, /’,
‘Accept-Language’: ‘zh-CN,zh;q=0.9,en;q=0.8’,
‘Cache-Control’: ‘no-cache’,
‘Connection’: ‘keep-alive’,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘Pragma’: ‘no-cache’,
‘Referer’: ‘https://192.168.190.222/infrastructure/compute/hostAndCluster’,
‘Sec-Fetch-Dest’: ‘empty’,
‘Sec-Fetch-Mode’: ‘cors’,
‘Sec-Fetch-Site’: ‘same-origin’,
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36’,
‘sec-ch-ua’: ‘“Not.A/Brand”;v=“8”, “Chromium”;v=“114”, “Google Chrome”;v=“114”’,
‘sec-ch-ua-mobile’: ‘?0’,
‘sec-ch-ua-platform’: ‘“macOS”’,
‘token’: ‘750ecff3-6e96-4c50-a594-474a9aaed2d3’,
}

params = (
(‘clusterId’, ‘9’),
(‘usageType’, ‘3’),
)

response = requests.get(‘https://192.168.190.222/hypercenter/statistics/getVmTopNUsageDataByClusterId’, headers=headers, params=params, verify=False)

print(response.json())
3. Then we combine it with our framework. Since our automation framework uses the form of pytest+alllure, the following figure shows the interaction with gpt:
insert image description here

The following is the code given by gpt:

Ok, here is a sample code using pytest and allure:

  1. Install pytest and allure-pytest
pip install pytest
pip install allure-pytest
  1. Create a test file test_api.py
import pytest
import requests
import allure
 
 
@pytest.fixture()
def headers():
    headers = {
    
    
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Pragma': 'no-cache',
        'Referer': 'https://192.168.190.222/infrastructure/compute/hostAndCluster',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"macOS"',
        'token': '750ecff3-6e96-4c50-a594-474a9aaed2d3',
    }
    return headers
 
 
@pytest.mark.parametrize("clusterId, usageType", [(9, 3), (10, 4)])
def test_api(headers, clusterId, usageType):
    with allure.step("发送请求"):
        params = {
    
    'clusterId': clusterId, 'usageType': usageType}
        response = requests.get(
            'https://192.168.190.222/hypercenter/statistics/getVmTopNUsageDataByClusterId',
            headers=headers,
            params=params,
            verify=False
        )
    with allure.step("断言响应状态码"):
        assert response.status_code == 200
    with allure.step("断言响应数据"):
        assert response.json()['code'] == 0
         
 
if __name__ == '__main__':
    pytest.main()
  1. Run tests and generate allure reports

Run the following command in a terminal:

pytest --alluredir=./allure-results

This will generate an allure-results folder in the current directory and save the test results in it.

  1. view allure report

Run the following command to open allure reports in your default browser:

allure serve ./allure-results

This will open the allure report in your browser where you can view the test results and test execution steps.
After that, we save the copy of the second step given as a py file and run it. Our automation script is initially realized, and we can change it according to the needs.

4. Some other requirements can also be assisted by GPT. For example, if we want to add some concurrent operations to our previous shell scripts, we can tell GPT, as shown in the figure:

insert image description here

The results returned by gpt:

You need a script to execute the command regularly, you can use crontab to achieve.

  1. Open the terminal, enter crontab -e, and press Enter. If it is the first time, you will be prompted to choose an editor.

  2. Add the following to the editor:

*/5 * * * * curl ‘https://192.168.190.222/api/openstack/regionone/cinder/v3/48f0dd9acba84b088f5b85b09305a3e8/types’
-H ‘Accept: application/json, text/plain, /
-H ‘Accept-Language: zh-CN,zh;q=0.9,en;q=0.8’
-H ‘Cache-Control: no-cache’
-H ‘Connection: keep-alive’
-H ‘Openstack-Api-Version: volume 3.59’
-H ‘Pragma: no-cache’
-H ‘Referer: https://192.168.190.222/base/overview-admin’
-H ‘Sec-Fetch-Dest: empty’
-H ‘Sec-Fetch-Mode: cors’
-H ‘Sec-Fetch-Site: same-origin’
-H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36’
-H ‘X-Auth-Token: gAAAAABkjmLu8CeOOTFDUIW7GEN074d4rgXk4eD5jtnWxqhAhaRnhNMv1jsikQpvPgJjUGy3kWLAb1P6NCDnYm67d6sM5g4au_eTnxQH-na0heReGOnezZaUQ7YpK0Wr5x-FVu3dd4b-NF4Ydhf0VYyYPVvQDbK2Vi-efoxY5ouqvgwPcUp9ZsbF_l_jTg92OhccxYoSR-w1’
-H ‘sec-ch-ua: “Not.A/Brand”;v=“8”, “Chromium”;v=“114”, “Google Chrome”;v=“114”’
-H ‘sec-ch-ua-mobile: ?0’
-H ‘sec-ch-ua-platform: “macOS”’
-H ‘token: 5106b47f-1bca-4e84-9baf-d7678273a8f8’
–compressed
–insecure

  1. Save the file and exit the editor.

In this way, the command is executed every 5 minutes and the result is output.

Guess you like

Origin blog.csdn.net/qq_41196999/article/details/131462389