网易airtest录制web应用测试脚本2

airtest脚本增强

前言

目前的airtestIDE工具,已经能够实现常见的web页面操作录制生成脚本,但是在实际使用中,很多测试场景并不能实现支持,好在airtest支持python脚本,加之python语言相对简便,降低了测试人员学习成本,还实现对特殊场景的脚本增强。

几个常见的操作

  1. 生成唯一数
    在很多场景,特别是注册、新建时,大多都会限制唯一用户名、文件名,这时候我通过读取系统时间实现唯一数,因为airtest脚本为串行执行,不存在loadrunner中并发的情况,所以我认为时间轴是最稳定可靠的唯一数生成方法:
// An highlighted block
# -*- encoding=utf8 -*-
__author__ = "Administrator"
'''
新建文件夹,新建接口
通过时间函数,设置不重复唯一数
'''
from airtest.core.api import *
auto_setup(__file__)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
#获取时间函数,避免文件夹名重复
#import random
import time
#a = random.randint(0,1000)
a = time.strftime("%Y%m%d%H%M%S", time.localtime())
driver = WebChrome()
driver.implicitly_wait(20)

driver.get("~~~/")
driver.find_element_by_id("username").send_keys("airtest")
driver.find_element_by_id("password").send_keys("1234")
...()
#新建测试文件夹,文件夹不能重复
b = "airtestx"
str1 = b + str(a)
driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div[2]/div/div/input").send_keys(str1)
...

driver.quit()
  1. 读取文件
    在测试中需要依据现有的数据执行操作,例如登录操作中,需要已有的注册用户时,将已经编辑好的文档读取到脚本中执行。
# -*- encoding=utf8 -*-
'''
完成数据驱动测试试验
'''
from airtest.core.api import *

auto_setup(__file__)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome

#读取txt文件
ufile = open("G:\\UIAuto\\read.txt", 'r')
#读取数据行
lines = ufile.readlines()
#循环遍历
for line in lines:
    driver = WebChrome()
    driver.implicitly_wait(20)
    driver.get("https://www.baidu.com/")
    #每一行第一个和第二个元素
    seach = line.split(',')[0]
    seach1 = line.split(',')[1]
    #拼接输入
    driver.find_element_by_name("wd").send_keys(seach + seach1)
    driver.find_element_by_xpath("//input[@value='百度一下']").click()
	...
    
    driver.quit()
#关闭文件
ufile.close()
  1. 页面滑动
    当页面展示不完,需要滚动条向下或其他方向移动时:
# -*- encoding=utf8 -*-
__author__ = "xuezhi"
'''
光标移动试验
'''
	...
    #下拉至底部
    js = "window.scrollTo(100,1400);"
    driver.execute_script(js)
    sleep(2)

浏览器中滚动页面需要了解页面的大小,将光标移动到理想位置,可能需要多次调整

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Oxygen2Test

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值