Example #1
0
from selenium import webdriver
import requests

# 包和文件在同级目录下
from test.a import username
print(username)

# 同级别文件的相互导入
from day04demo import password
print(password) 

# 同级文件方法的相互导入
from dbtools import query
sql = 'select * from orders'
r = query(sql)
print(r)
Example #2
0
# 1.构造请求
u = "http://118.24.105.78:2333/login"  # 接口地址
h = {"Content-Type": "application/json"}  # 请求头
d = {"username": "******", "password": "******"}  # 请求参数
r = requests.post(url=u, headers=h, json=d)  # r 是返回值
print(r.text)  # r.text:响应值

# assert 条件语句,assert是python里用来断言的
# 2.判断结果
# print( r.status_code)
assert r.status_code == 200  # 判断状态码, 获取本次响应的状态码是否等于200
# print(r.json())                   # r.json():把返回值 r 转换成字典
assert r.json()["status"] == 200  # 判断结果码

# 3.数据库查询
sql = "select * from t_user where username ='******'".format(d["username"])
assert len(query(sql)) != 0  # 如果账号存在 > sql应该是有结果的 > query(sql)长度 != 0
print("登录接口测试用例执行通过!")

# 保存 token 到 user_name.txt 文件中
token = r.json()["data"]["token"]
write_file("./user_token.txt", token)  # 把 token 写入 user_token.txt 文件中

# 用户退出
nt = read_file("./user_token.txt")
u = "http://118.24.105.78:2333/logout"  # 接口地址
h = {"Content-Type": "application/json", "token": nt}  # 请求头
d = {"username": "******", "password": "******"}  # 请求参数
r = requests.get(url=u, headers=h)  # r 是返回值
print(r.text)
Example #3
0
import requests
from dbtools import query
from filetools import write_file,read_file
#接口1 用户登录
u = "http://192.144.148.91:2333/login"
d = {"username":"******", "password":"******"}
res = requests.post(url = u,json = d)

assert res.status_code == 200
assert res.json()["status"] == 200

sql = "select * from t_user where username='******'".format(d["username"])
r = query(sql)
assert len(r) != 0

print("登录接口测试通过!")

#token取出来
# print(res.text) 

#取出token并且保存
token = res.json()["data"]["token"]
write_file("user_token.txt",token)
user_token = read_file("user_token.txt")

#接口2:用户修改头像
u1 = "http://192.144.148.91:2333/updateuserheadpic"
h1 = {"Content-Type":"application/json","token":user_token} #请求头:字典
d1 = {"ximg":"chen.jpg"} #参数
r1 = requests.post(url = u1,json = d1,headers = h1) #hearders:指定请求头
Example #4
0
#导入requests : 固定的导入方法
import requests
from dbtools import query
#python调接口

# 1.构造请求
u = 'http://118.24.255.132:2333/get_title_img'
r = requests.get(u)
print(r.text)

# 2.判断结果:断言实现判断http状态码,和结果码
# assert 判断条件
#结果码:本次请求的结果是否正确; 状态码:接口的状态码
assert r.status_code == 200  #判断接口是不是正常的
# r.json() 把结果转换成字典
assert r.json()['status'] == 200  #判断结果码是否正确

#3. 数据库校验,本次要查询所有的轮播图id是否存在
data = r.json()['data']
for i in data:
    # 每次循环之后,都去查一下数据库
    sql = 'select * from t_title_img where id = {}'.format(i['id'])
    res = query(sql)
    print(res)
    assert len(res) != 0
Example #5
0
# # print(query(sql))
# assert len(query(sql)) != 0

token = r.json()['data']['token']
write_file('user_token.txt', token)
user_token = read_file('user_token.txt')


# u1 = 'http://192.144.148.91:2333/updateuserheadpic'
# d1 = {"ximg" :"头像.jpg"}
# h1 = {"Content-Type":"application/json", "token":user_token }
# res = requests.post(url=u1,json=d1,headers=h1)

# assert r.status_code == 200 
# assert r.json()['status'] == 200 

u2 = 'http://192.144.148.91:2333/article/new'
d2 = {"title":"为什么要学测试", "content":"111", "tags":"测试1254", "brief":"介绍", "ximg":"dsfsdf.jpg" }
h2 = {"Content-Type":"application/json", "token":user_token }
res = requests.post(url=u2,json=d2,headers=h2)
assert res.status_code == 200 
assert res.json()['status'] == 200 
# print(res.text)

articleid = res.json()['data']['articleid']
write_file('user_articleid.txt',str(articleid))
user_articleid = read_file('user_articleid.txt')

sql2 = 'select * from t_article where id="{}"'.format(user_articleid)
assert len(query(sql2)) != 0
print('发表成功!')  
Example #6
0
import requests
from dbtools import query
from filetools import write_file, read_file

uu = 'http://zzy.testgoup.com/api/login'
dd = {"username": "******", "password": "******"}

res1 = requests.post(url=uu, json=dd)  #指定内容传送
# print(res1.text)
# print(type(res1.text))
assert res1.status_code == 200
assert res1.json()["status"] == 200
# 数据库查询
sql = "select * from tb_user where username ='******' and password='******'".format(
    dd["username"], dd["password"])
r = query(sql)
assert len(r) != 0
# len(r) != 0 > 返回值有数据> sql查到了数据 > 账号和密码在数据库 # len(r) == 0 》 账号和密码不在数据库
print("登陆成功")

#python的关联
uid = res1.json()["data"]["id"]
token = res1.json()["data"]["token"]
# print(token)
write_file('./conf/user_token.txt', res1.json()["data"]["token"])
write_file('./conf/user_id.txt', str(res1.json()["data"]["id"]))
# 文件读写的方式,写入到txt中

print("------领取任务--------")
url2 = "http://zzy.testgoup.com/api/receive/taskinfo"
data2 = {"id": "4761"}
Example #7
0
from dbtools import query

username = input("请输入账号:")
password = input("请输入密码:")

sql = "select * from t_pymysql_account where username='******' and password='******'".format(
    username, password)
r = query(sql)
a = query(sql)
if len(r) != 0:
    print("登陆成功")
else:
    print("登陆失败")
Example #8
0
# 用户登录-用户新增文章-用户修改这篇文章
# 要求是在必要的时候必须做完整的校验过程,一定要做数据校验
# 文章表:t_article

import requests
from dbtools import query

# 用户登录
u = "http://118.24.105.78:2333/login"
h = {"Content-Type": "application/json"}
d = {"username": "******", "password": "******"}
res = requests.post(url=u, headers=h, json=d)
assert res.status_code == 200
assert res.json()["status"] == 200
sql = "select * from t_user where username = '******'".format(d["username"])
assert len(query(sql)) != 0
token = res.json()["data"]["token"]
print("用户登录成功!")

# 用户新增文章
u = "http://118.24.105.78:2333/article/new"
h = {"Content-Type": "application/json", "token": token}
d = {
    "title": "为11学习测试aaaa",  # 文章标题
    "content": "内容",  # 文章内容
    "tags": "测试测试测试",  # 文章分类
    "brief": "介绍",  # 文章的简介 
    "ximg": "测试jpg.jpg"  # 上传的图片
}
res = requests.post(url=u, headers=h, json=d)
#print(res.text)
Example #9
0
# 2.判断结果,断言实现判断http状态码,和结果码。
#  status:200
#  判断语句:> < = != is in
assert r.status_code == 200  # 判断接口是否正常,获取http状态码
# r.json()把结果转化为字典,获取字典格式的返回值
assert r.json()["status"] == 200  #判断结果码是否正确

# 3.数据库校验
# (本次要查询所有的轮播图 id是否存在)
data = r.json()["data"]
for i in data:
    #  print(i["id"])
    # 每次循环之后,都会查询一下数据库
    sql = "select * from t_title_img where id = {}".format(i["id"])
    res = query(sql)
    # print(res)
    assert len(res) != 0
    # print("id在数据库当中")

# 有参数的post接口
url = "http://118.24.105.78:2333/login"
data = {"username": "******", "password": "******"}
h = {"Content-Type": "application/json"}
res = requests.post(url=url, json=data, headers=h)
print(res.text)
assert res.status_code == 200
assert res.json()["status"] == 200
sql = "select * from t_user where username = '******'"
assert len(query(sql)) != 0
print("接口测试成功")
Example #10
0
# 模拟登录登录的完整过程
# 两个文件中在同一个模块相互导入
from dbtools import query

u = input("请求输入账号:")
p = input("请求输入密码:")

sql = "select * from t_pymysql_account where username = '******' and password = '******'".format(u, p)

res = query(sql)        # 调用dbtools里面导入的query方法
if len(res) != 0:       # 结果长度!=0> sql查出的数据不为空 > 数据表中存在这个账号和密码
    print("登录成功")
else:                   # 结果长度 ==0 > sql查询的数据为空 > 数据表中不存在这个账号和密码
    print("登录失败")
Example #11
0
#接口地址是字符串类型
# u = 'http://192.144.148.91:2333/getarticle?pagenum=1'
# r = requests.get(u)     #r是所得响应信息,requests。get(u)发送请求
# print(r.text)           #r.text:打印接口得返回值

u = 'http://192.144.148.91:2333/login'
d = {'username': '******', 'password': '******'}
r = requests.post(url=u, json=d)
print(r.text)
#判断结果
assert r.status_code == 200  #判断状态码
assert r.json()['status'] == 200  #判断结果码 r是返回值  通过r.json()转换成dict格式
#查询数据库:接口的作用来查询
sql = 'select * from t_user where username = "******"'.format(d['username'])
print(query(sql))
assert len(query(sql)) != 0
print('测试通过')

# u = 'http://192.144.148.91:2333/get_title_img'
# r = requests.get(u)
# print(r.text)

# us = 'http://192.144.148.91:2333/regist'
# ds = {
# "username":"******",
# "password":"******",
# "phone":"18212358234",
# "email":"*****@*****.**"
# }
# rs = requests.post(url=us,json=ds)
Example #12
0
import requests
from dbtools import query
#练习get自动化
#接口地址
# u="http://192.144.148.91:2333/get_title_img"
# res=requests.get(u)
# print(res.text)

#练习post接口请求
u = "http://192.144.148.91:2333//login"  #接口地址
d = {"username": "******", "password": "******"}  #接口参数
res = requests.post(url=u, json=d)
print(res.text)

#断言判断状态码和结果码
assert res.status_code == 200
assert res.json()["status"] == 200

#数据库操作 断言判断登入的账号存在数据库中
sql = "select * from t_user where username='******'".format(d["username"])
query(sql)