1.介绍
(1)测试模块:前台菜单栏进入兑换码页面进行输入校验。
(2)测试方法:利用脚本进行自动化的输入和校验,判断结果第一种为该兑换码已经被使用,前台文案提示;第二种为兑换成功,点击固定文案按钮跳转到其他页面,在返回到兑换码页面,再次重复第一种判断。
2.代码介绍
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from decimal import *
import time
start=time.clock()
dervice = webdriver.Chrome()
dervice.get('https://**************)#请求网址,需要网址关注评论可以给你
sleep(3)
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[1]/uni-view/uni-view[2]/uni-view[2]/uni-view').click() #点击切换手机号码按钮的元素定位
sleep(1)
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-button').click() ##点击登陆按钮的元素定位
sleep(1)
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-form/span/uni-view[1]/uni-input/div/input').click()#点击切输入框的元素定位
dervice.find_element_by_xpath("//*[contains(@class,'uni-input-input')]").send_keys(********) #手机号码输入的元素定位
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-form/span/uni-view[2]/uni-input/div/input').click()#点击密码输入框的元素定位
dervice.find_elements_by_xpath("//*[contains(@class,'uni-input-input')]")[1].send_keys(999999)#密码输入的元素定位,这里有个contains方法,直接取class的值,百度下就知道
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view[2]/uni-form/span/uni-view[3]').click()
##点击登陆按钮的元素定位
sleep(3)#等待3秒
dervice.find_element_by_xpath('//*[@id="cardCoupon"]').click()
#点击跳转到兑换码页面的元素定位
import pymysql #导入连接数据库的方法
conn=pymysql.connect(host="******",port=3306,user="*****",password="*****",db="dnls01")#host是主机ip地址,端口号,数据库账号,密码,db是库名
cursor=conn.cursor()
cursor.execute("SELECT coupon_code from md_coupon_code where active_id ='29'and id>'1100'")
output=cursor.fetchall()
out=output[0:31]#切片获取取值范围
count=0#统计执行多少次
for i in out:#out为数据库拿到的兑换码的数值进行遍历循环
count+=1
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view/uni-view[3]/uni-input/div/input').clear()#清空输入框的内容的元素定位
sleep(2)
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view/uni-view[3]/uni-input/div/input').send_keys(
i)#输入兑换码内容的元素定位
sleep(2)
dervice.find_element_by_xpath(
'/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view/uni-view[4]/uni-button').click()
sleep(2)##点击兑换的元素定位
try: #这步的判断是兑换码成功会跳出一个跳转弹窗,用来抓取判断的,如果有就跳转成功,然后在返回到之前的兑换码页面,再输入兑换码,重复判断
dervice.find_element_by_xpath('/html/body/uni-app/uni-modal/div[2]/div[3]').click()
dervice.back()
sleep(1)
dervice.find_element_by_xpath('//*[@id="cardCoupon"]').click()
except Exception as e:
print(e)
continue
end=time.clock()
print('Running time: %s Seconds'%(end-start))#统计执行时间
print('兑换码总次数:',count)
```python
在这里插入代码片
3.页面展示
4.结言
运用自动化完成完全脱手的跑脚本演示,连的数据库和前端页面都是测试版的,方便测试,因为正式环境中无法校验真实的兑换码,毕竟是线上的项目,如果开启兑换码,造成真实数据流失,不安全,这个功能可以结合很多页面的测试,非常实用,上面只是测试思路,测试思路很重要,你要明白你要做什么测试,这样的测试才有意义,前两天就是拿这个脚本测出一个bug出来,就是在兑换13次之后,一直提示校验失败,正常都是会显示兑换成功的提示,上面的sql语句都是基础知识,这个应该难道不了各位,就是字段取值而已,根据你自己公司的字段去数据库里面查找就可以,具体可以问开发!今天就这样吧,需要拿我的演示的朋友,记得私聊或者留言哦。。。