自动化测试工具必备
1.Python解释器安装
官网地址:https://www.python.org
    2.PyCharm集成开发工具
官方地址:https://www.jetbrains.com/pycharm
   
Python中的对象,变量,函数
1.对象
所有的数据类型都是对象
1.字符串
2.数字:整数,小数
3.列表,字典
4.函数,类,模块
print(3)
print(3+1)
print('3+1')
print("3+1")
print('''3+1''')
运行结果
3
4
3+1
3+1
3+1
2.变量
变量的命名:
一般以字母(大写,小写)开头
后面可以使用下划线_或者使用数字
大小写敏感
不能与关键字相同
不要与内置函数相同
a=3
print(a)
a="hello world"
print(a)
a=100+34
print(a)
3.函数(function)
def playfootball():
print("拿起足球")
print("拿起足球1")
print("拿起足球2")
print("拿起足球3")
playfootball()
函数的参数
定义参数时的命名规则-和变量一样
可以是一个,也可以是多个
函数体里面像变量一样使用参数
调用函数时,传入对应参数的个数
def foot(x):
print(x+3)
foot(3)
def foot2(a,b):
print((a+b)*5)
foot2(3,4)
接口测试化自动化
1.使用python语言可以实现WEBAPI接口测试 2.根据测试用例
构造出相应的HTTP请求,发送给服务端
内置库:Httplib,urllib2
第三方库:urllib3,requests,pyCurl
接受并检查响应信息
 3.搭建测试环境 1.搭建springboot脚手架  构建项目包名称  导入maven包  创建数据库  建好后,将resources下的application.properties改名 application.yml 写上接口,数据库的连接和mybatis逻辑
server:
port: 8082
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/bnzj?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.bnz.pojo
创建*mapper.xml  UserMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bnz.dao.UserDao">
<select id="findAll" parameterType="com.bnz.pojo.User" resultType="com.bnz.pojo.User">
select * from user
</select>
<insert id="save" parameterType="com.bnz.pojo.User" keyProperty="id">
insert into user values (#{id},#{name},#{age})
</insert>
<update id="update" parameterType="com.bnz.pojo.User" keyProperty="id">
update user set id=#{id},name=#{name},age=#{age}
where id = #{id}
</update>
<delete id="delete" parameterType="com.bnz.pojo.User">
delete from user where id=#{id}
</delete>
</mapper>
pojo包下User类
package com.bnz.pojo;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
dao包下UserDao接口类
@Mapper
public interface UserDao {
List<User> findAll();
Integer save(User user);
Integer update(User user);
Integer delete(Integer id);
}
service下的UserService接口类
public interface UserService {
List<User> findAll();
Integer save(User user);
Integer update(User user);
Integer delete(Integer id);
}
service下的UserServiceImpl接口实现类
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public Integer save(User user) {
return userDao.save(user);
}
@Override
public Integer update(User user) {
return userDao.update(user);
}
@Override
public Integer delete(Integer id) {
return userDao.delete(id);
}
}
controller包下的UserController
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("findAll")
private List<User> findAll(){
return userService.findAll();
}
@PostMapping("save")
public String save(@RequestBody User user){
Integer save = userService.save(user);
if(save != 0){
return "保存成功";
}else {
return "保存失败";
}
}
@PostMapping("update")
public String update(@RequestBody User user){
Integer update = userService.update(user);
if(update != 0){
return "修改成功";
}else {
return "修改失败";
}
}
@DeleteMapping("delete/{id}")
public String Delete(@PathVariable Integer id){
Integer delete = userService.delete(id);
if(delete != 0){
return "删除成功";
}else {
return "删除失败";
}
}
}
2.编写自动化测试 1.findAll接口测试
import requests
from pprint import pprint
res=requests.get('http://localhost:8082/user/findAll')
print(res.status_code)
pprint(res.json())
 2.save保存json接口测试
a = {"id":"4","name":"yzh","age":"20"};
url = "http://localhost:8082/user/save/"
headers = {'Content-Type': 'application/json;charset=UTF-8'}
res = requests.request("post",url,json=a,headers=headers)
print(res.status_code)
print(res.text)
 3.update修改json接口测试 4.delete删除接口测试
Python自动化程序读写数据库
import MySQLdb
db = MySQLdb.connect("localhost", "root", "123456", "bnzj")
c=db.cursor()
c.execute("select * from user")
d=c.fetchone();
print(d)
d=c.fetchone();
print(d)
d=c.fetchall();
print(d)
a=c.execute("insert into user (id,'name',age) values (6,'yyy',10)")
db.commit();
db.close()
selenium自动化
Selenium WebDriver
提供了web自动化的各种语言(java,python)调用接口库
通过各种浏览器驱动来驱动浏览器
 
谷歌: http://npm.taobao.org/mirrors/chromedriver/
 自动化爬取前程无忧Java
from selenium import webdriver
driver=webdriver.Chrome(r"D:\浏览器驱动\chromedriver.exe")
driver.get("http://www.51job.com");
效果   找到元素,操作元素
from selenium import webdriver
driver=webdriver.Chrome(r"D:\浏览器驱动\chromedriver.exe")
driver.get("http://www.51job.com");
ele=driver.find_element_by_id("kwdselectid");
ele.send_keys("java");
 
from selenium import webdriver
driver=webdriver.Chrome(r"D:\浏览器驱动\chromedriver.exe")
driver.get("http://www.51job.com");
ele=driver.find_element_by_id("kwdselectid");
ele.send_keys("java");
ele=ele.find_element_by_id("work_position_input");
ele.click();
driver.find_element_by_css_selector()
效果图无
|